The simple factory pattern is determined by the factory object which product to create. Although it does not belong to the 23 design patterns, it is also the origin of the advanced factory pattern.
simulation scene:
The summer vacation was too boring, so I planned to make an MP3 player at home, including the program design of the player. The following structure
//歌曲播放接口
public interface ISong {
void Play();
}
//流行歌曲播放
public class PopularISong implements ISong {
@Override
public void Play() {
System.out.println("接下来播放流行歌曲~");
}
}
//古典歌曲播放
public class ClassicalSongs implements ISong {
@Override
public void Play() {
System.out.println("接下来播放古典歌曲~");
}
}
//MP3播放操作
public class MP3 {
public enum SongType
{
PopularSongType,//流行歌曲
ClassicalSongsType//古典歌曲
//....其他类型
}
public ISong Pay(SongType type)
{
if(type==SongType.PopularSongType)
{
return new PopularISong();
}
else if(type==SongType.ClassicalSongsType)
{
return new ClassicalSongs();
}
return null;
}
}
public class Text {
public static void main(String[] args) {
//用户执行操作
MP3 mp3=new MP3();
mp3.Pay(MP3.SongType.PopularSongType).Play();
}
}
[](https://jq.qq.com/?_wv=1027&k=oO83gyrl)
The program is finally finished~ I played popular songs on the sofa comfortably using MP3, and my grandma came over and asked if I could play operas. I touched my head and said, "It's okay." But you have to wait for me.
One hour later, after re-dismantling the hardware and writing code, the category of opera was added, and the branch if else
else if(type==SongType.TraditionalOperaType)//戏曲
{
//.....
}
After giving it to my grandmother, I was very satisfied and told my dad to praise me. When my dad found out, he wanted to listen to the cross talk. I thought to myself that this is not going to write the code again... this obviously doesn't work.
[](https://jq.qq.com/?_wv=1027&k=oO83gyrl)
simple factory model contains the necessary logical judgments, and dynamically instantiates and generates related classes according to the conditions selected by the user, clearly distinguishing their respective responsibilities and powers. But here is also a violation of the open-closed principle. The factory class concentrates all the logical judgments. Once one is added, the logical judgment code must be modified.
Factory mode
With the above experience, I decided to rewrite the program to prevent them from wanting to listen to other types of songs, and I have to re-modify the code logic to judge.
//工厂类
public interface IFactory {
ISong CreateSong();
}
//产品歌曲类
public interface ISong {
void Play();
}
Specific factory class:
//具体工厂类 用于创建流行歌曲
public class PopularSongFactory implements IFactory{
@Override
public ISong CreateSong() {
return new PopularISong();
}
}
//具体工厂类 用于创建古典歌曲
public class ClassicalSongsFactory implements IFactory{
@Override
public ISong CreateSong() {
return new ClassicalSongs();
}
}
Specific song category:
//具体歌曲类:古典
public class ClassicalSongs implements ISong {
@Override
public void Play() {
System.out.println("接下来播放古典歌曲~");
}
}
//具体歌曲类:流行
public class PopularISong implements ISong {
@Override
public void Play() {
System.out.println("接下来播放流行歌曲~");
}
}
user operation level:
public static void main(String[] args) {
//用户执行操作
IFactory factory=new ClassicalSongsFactory();//古典工厂 只需要更改这里的类型,选择权交给用户去生成对应的实例
ClassicalSongs songs= (ClassicalSongs) factory.CreateSong();
songs.Play();
}
[](https://jq.qq.com/?_wv=1027&k=oO83gyrl)
[](https://jq.qq.com/?_wv=1027&k=oO83gyrl)
The factory mode puts the decision of which factory to instantiate to the client. It maintains the advantages of the simple factory mode and overcomes the shortcomings. It can be changed without major changes, which reduces the coupling of the program, but every time it needs to be increased When there is a type, a product and a factory need to be created. This can use reflection to solve the problem of branch judgment.
Finally, I wish you all an early success in learning, get satisfied offer , quickly promotion and salary increase , and embark on the peak of life.
If you can, please give me a three-line support for me?????? 【Information on prostitutes】
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。