Simple factory Mode,Factory method modeAndAbstract Factory ModelAll belong to the creation design mode, and you do not need to know the specific classes for the three creation modes. We master the idea that when creating an object, we need to encapsulate the areas that are easy to change to control the changes (where to change, where to encapsulate ), to adapt to customer changes and expand projects.
Simple factory Mode: There is no abstract class in a simple factory. There is only one specific factory class such as myfactory, and a factory method createproduct in myfactory returns a base product, the specific instance to be returned is determined by passing in parameters and then using case.
For example, the product is a mobile phone, which has two sub-categories: Nokia mobile phone and Apple mobile phone. Both types of mobile phones are controlled by the cellphoneproducer class.
package designpattern;interface Cellphone {void call();}class AppleCellphone implements Cellphone {public void call() {System.out.println("Apple calls.");}}class NokiaCellphone implements Cellphone {public void call() {System.out.println("Nokia calls.");}}class CellphoneProducer {public static Cellphone produceNokia() {return new NokiaCellphone();}public static Cellphone produceApple() {return new AppleCellphone();}}public class SimpleFactory {public static void main(String[] args) {Cellphone phone1 = CellphoneProducer.produceNokia();phone1.call();Cellphone phone2 = CellphoneProducer.produceApple();phone2.call();}}
Factory method mode: This mode has an abstract base class and several derived factory classes. The base class defines a virtual factory method to return the base class of the specified product class, the derived class needs to implement this virtual method and create a specific product class to return. Note that each specific factory of the factory method is responsible for returning only one product class.
For example, in the following example, the factory class is a base class that produces different products through different subclasses.
package designpattern;interface CellphoneFactory {Cellphone produce();}class NokiaFactory implements CellphoneFactory {public Cellphone produce() {return new NokiaCellphone();}}class AppleFactory implements CellphoneFactory {public Cellphone produce() {return new AppleCellphone();}}public class FactoryMethod {public static void main(String[] args) {Cellphone phone1 = new NokiaFactory().produce();phone1.call();Cellphone phone2 = new AppleFactory().produce();phone2.call();}}
Abstract Factory Model: This mode is similar to the factory method mode. It is also an abstract base class and several specific factory classes. The difference is that the factory base class of the abstract factory defines multiple virtual factory methods, each Virtual Factory method returns one product, multiple factory methods return multiple products, and these products have certain connections.
For example, in the following example, the factory should not only produce mobile phones, but also charger, and there is a certain correlation between the two (Apple phones must use an apple Charger ). Therefore, each subclass of the factory class defines a product family.
package designpattern;interface Charger {void charge();}class NokiaCharger implements Charger {public void charge() {System.out.println("Nokia charges.");}}class AppleCharger implements Charger {public void charge() {System.out.println("Apple charges.");}}interface abstractFactory {Cellphone producePhone();Charger produceCharger();}class NokiaAbsFactory implements abstractFactory {@Overridepublic Cellphone producePhone() {return new NokiaCellphone();}@Overridepublic Charger produceCharger() {return new NokiaCharger();}}class AppleAbsFactory implements abstractFactory {@Overridepublic Cellphone producePhone() {return new AppleCellphone();}@Overridepublic Charger produceCharger() {return new AppleCharger();}}public class AbstractFactory {public static void main(String[] args) {NokiaAbsFactory naf = new NokiaAbsFactory();Cellphone phone1 = naf.producePhone();phone1.call();Charger charger1 = naf.produceCharger();charger1.charge();AppleAbsFactory aaf = new AppleAbsFactory();Cellphone phone2 = aaf.producePhone();phone2.call();Charger charger2 = aaf.produceCharger();charger2.charge();}}
Summary:
Simple Factory: Used to produce any product of the same level structure. (Unable to add new products)
Factory method: Used to produce fixed products in the same level structure. (Any product can be added)
Abstract Factory: Used to produce all products of different product families. (There is nothing to do with adding new products; Support for adding product families)
The above three factory methods have different levels of support in the hierarchical structure and product family. Therefore, you should consider which method should be used based on the actual situation.