Design Patterns factory pattern a simple factory pattern
The origin of the factory pattern:
When we create objects of use, the use of new operations. If you are using multiple objects to create a new conference. Therefore, a model is used to model the factory new operating agent. According to the origin of the factory model, we can see that the factory model belongs create schema. To provide a best way to create an object.
Source: Kaige Java (kaigejava)
The main factory pattern classification:
Simple factory pattern, factory method pattern, abstract factory pattern
Simple factory pattern:
Simple factory pattern does not belong in the 23 design patterns. Also known as a static factory method, a simple factory pattern is the factory model family is the most simple to use as a model. It can also be understood as a special mode of realization of different plants.
How to understand?
For example: a factory class, a product of abstract class.
Real life example: a noodle shop, we can produce more than the noodles.
Such as: noodle which can make tomato egg noodles, beef noodles, noodle, whistle face, biangbiang surface and dry noodles.
We have to simulate this scenario:
Let's look at the project structure:
Description:
1: Noodles interface class
2: Specific implementation class noodles
3: simple factory class (noodle objects)
4: Test Type
Let's take a look at noodles interface class:
INoodlesInterface
We take a look, noodles implementation class - tomato and egg noodles objects inside:
public class TomatoAndEggNoodles implements INoodlesInterface {
private String noodlesName;
public TomatoAndEggNoodles(){}
public TomatoAndEggNoodles(String noodlesName){
this.noodlesName = noodlesName;
}
@Override
public void noodlesdescription() {
System.out.println("大爷,您的"+this.noodlesName+"价格为:10元。");
}
}
其他具体面条实现类里面类似。这里就不一一贴出来了。
我们来看看面馆对象:
里面由三部分:
菜单部分、根据菜单序号制作面条、根据面条名称制作面条。
我们来看看根据菜单序号制作面条的:
public static INoodlesInterface makeNoodlesByMenuType(int noodletType){
switch (noodletType){
case NOODLES_TYPE_1:
return new TomatoAndEggNoodles("西红柿鸡蛋面");
case NOODLES_TYPE_2:
return new HandPulledNoodleSoupWithBeef("牛肉拉面");
case NOODLES_TYPE_3:
return new HandPulledNoodleSoupWithBeef("刀削面");
case NOODLES_TYPE_4:
return new NoodleWithPork("哨子面");
case NOODLES_TYPE_5:
return new BiangBiangNoodles("biangbiang面");
case NOODLES_TYPE_6:
return new HotAndDryNoodles("热干面");
default:
return new HotAndDryNoodles("热干面");
}
}
另一个方法类似。
查看测试类:
查看运行结果:
运行结果,达到我们预期的。
现在我们来总结下简单工厂模式:
简单工厂模式几个角色:
1:共有的抽象父类或者接口。如:司小司面馆的面条接口对象
2:继承或是实现该接口的具体子类。如:西红柿鸡蛋面、biangbiang面等
3:创建对象的简单工厂类。如:司小司的面馆
trigger event:
Trigger class factory object produced by the number of menu or noodles name.
Take a look at a simple factory pattern features:
Through a class. Concrete classes is an important way to create the abstract class interface or not, using the if statement switch statement is said to be judged, and thus create the corresponding products.
Contact Kaige - "Kaige Java (kaigejava)
Personal blog: www.kaigejava.com
Among them, the creation of this important methods are usually static. So, a simple factory pattern is also known as a static factory.