Factory pattern: Mainly used to instantiate classes with a common interface. The factory pattern can dynamically determine which class should be instantiated.
Forms of factory pattern
Factory pattern mainly uses the following forms:
1: Simple Factory.
2: Factory Method.
3: Abstract Factory.
Simple Factory
also called static factory, is the simplest structure among the three states of the factory pattern. There is mainly a static method that accepts parameters and decides based on the parameters to return instances of different classes that implement the same interface. Let’s look at a specific example:
Suppose a factory produces washing machines, refrigerators, air conditioners, etc.
We first define a common product interface for all products
public interface Product{}
Then let’s make this factory All products must implement this interface
public class Washer implements Product{ public Washer(){ System.out.println("洗衣机被制造了"); } } public class Icebox implements Product{ public Icebox(){ System.out.println("冰箱被制造了"); } } public class AirCondition implements Product{ public Icebox(){ System.out.println("空调被制造了"); } }
Next we will write a factory class, which will be responsible for producing the above products
public class SimpleFactory { public static Product factory(String productName) throws Exception{ if(productName.equals("Washer")){ return new Washer(); }else if(productName.equals("Icebox")){ return new Icebox(); }else if(productName.equals("AirCondition")){ return new AirCondition(); }else{ throw new Exception("没有该产品"); } } }
Okay, with this factory class, we can start placing orders , SimpleFactory will decide what products to produce based on different order types.
public static void main(String[] args) { try { SimpleFactory.factory("Washer"); SimpleFactory.factory("Icebox"); SimpleFactory.factory("AirCondition"); } catch (Exception e) { e.printStackTrace(); } }
As can be seen from the above code, the core of the Simple Factory is a SimpleFactory class. It has the necessary logical judgment capabilities and the right to create all products. We only need to give him the order to get what we want. products you want. This seems very convenient to use.
But, in fact, this SimpleFactory has many limitations. First of all, every time we want to add a new product, we must modify the original code of SimpleFactory. Secondly, when we have many, many products, and there are complex hierarchical relationships between the products, this class must have complex logical judgment capabilities, and the amount of its code will continue to increase, which is simply a burden on future maintenance. The word horror...
Also, the entire system relies heavily on the SimpleFactory class. As soon as there is a problem with the SimpleFactory class, the system will enter a state that cannot work. This is also the most fatal point...
The above shortcomings will Resolved in two other states of factory pattern.
Factory Method
The above code tells us that the simple factory is not simple. It is the core of the entire model. Once something goes wrong with it, the entire model will be affected and cannot work. In order to reduce risks and prepare for the future To prepare for maintenance and expansion, we need to reconstruct it and introduce factory methods.
The factory method defines an interface for the factory class, using polymorphism to weaken the function of the factory class. The following is the definition of the factory interface:
public interface Factory{ public Product create(); } 我们再来定义一个产品接口 public interface Product{} 一下是实现了产品接口的产品类 public class Washer implements Product{ public Washer(){ System.out.println("洗衣机被制造了"); } } public class Icebox implements Product{ public Icebox(){ System.out.println("冰箱被制造了"); } } public class AirCondition implements Product{ public Icebox(){ System.out.println("空调被制造了"); } } 接下来,就是工厂方法的核心部分,也就是具体创建产品对象的具体工厂类, //创建洗衣机的工厂 public class CreateWasher implements Factory{ public Product create(){ return new Washer(); } } //创建冰箱的工厂 public class CreateIcebox implements Factory{ public Product create(){ return new Icebox(); } } //创建空调的工厂 public class CreateAirCondition implements Factory{ public Product create(){ return new AirCondition(); } }
As can be seen from the code for creating product objects above, the factory method and the simple factory The main difference is that a simple factory puts all the functions of creating products in one class, while the factory method puts different products in different factory classes that implement the factory interface. In this way, even if there is a problem with one of the factory classes, the other Factory classes can also work normally without affecting each other. When adding new products in the future, you only need to add a factory class that implements the factory interface to achieve this without modifying the existing code. But the factory method also has its limitations, that is, when the products we face have complex hierarchical structures. For example, in addition to home appliances, the factory also produces mobile phone products. In this way, home appliances and mobile phones are two major product families. , these two families contain a large number of products, and each product has multiple models, thus forming a complex product tree. If you use the factory method to design this product family system, you must create a corresponding factory class for each model of product. When there are hundreds or even thousands of products, there must also be hundreds or thousands of corresponding ones. Factory class, this led to the legendary class explosion, which was simply a disaster for future maintenance...
Abstract Factory (Factory Method)
Abstract Factory: The intention is to create a series of interrelated or interdependent objects. <
I personally think that the abstract factory introduces the concept of category management based on the factory method....
The factory method is used to create a product, and it does not have the concept of category. The abstract factory is used to create a series of products, so product classification has become the focus of the abstract factory.
We continue to use the above example to illustrate:
All products produced by the factory use capital letters to indicate their models, such as refrigerators , there are "refrigerator-A" and "refrigerator-B". Similarly, other products also follow this numbering rule, so there is a product family tree
refrigerator:
refrigerator-A
refrigerator- B
Washing machine:
Washing machine-A
Washing machine-B
We can define two product interfaces for refrigerators and washing machines to classify them,
//洗衣机接口 public interface Washer{ } //冰箱接口 public interface Icebox{ } 接着,我们分别创建这两个接口的具体产品 //洗衣机-A public class WasherA implements Washer{ public WasherA(){ System.out.println("洗衣机-A被制造了"); } } //洗衣机-B public class WasherB implements Washer{ public WasherB(){ System.out.println("洗衣机-B被制造了"); } } //冰箱-A public class IceboxA implements Icebox{ public IceboxA(){ System.out.println("冰箱-A被制造了"); } } //冰箱-B public class IceboxB implements Icebox{ public IceboxB(){ System.out.println("冰箱-B被制造了"); } } 到此,产品部分我们准备好了,接下来我们来处理工厂部分,我们先来定义工厂行为接口 public interface Factory{ public Washer createWasher(); public Icebox createIcebox(); }
Next I create specific factories Class, based on the interface of the above product, we divide the product of model A into a class, which is managed by one factory, and the product of model B is managed by another factory. According to this classification, we can implement the following two specific factories Class
//创建型号为A的产品工厂 public class FactoryA implements Factory{ //创建洗衣机-A public Washer createWasher(){ return new WasherA(); } //创建冰箱-A public Icebox createIcebox(){ return new IceboxA(); } } //创建型号为B的产品工厂 public class FactoryB implements Factory{ //创建洗衣机-B public Washer createWasher(){ return new WasherB(); } //创建冰箱-B public Icebox createIcebox(){ return new IceboxB(); } }
这样,我们的抽象工厂就完成了。有上面可以看出,在运用上我觉得工厂方法和抽象工厂,都有自己的应用场景,并没有什么优劣之分,但在应用抽象工厂之前,要先对创建的对象进行系统的分类,这点很重要,好的产品分类规则能为具体工厂类的选择调用和以后的扩展提供清晰的思路.
更多Java设计模式—工厂设计模式相关文章请关注PHP中文网!