Create a new pose for the instance, using factory mode instead of the constructor

1, Simple engineering model

Simple factory mode is also called static factory method mode

Content: it is up to a factory object to decide which product class instance to create.

Feature: a factory object only produces one corresponding instance.

Advantage: very simple to use

Disadvantages: it is difficult to expand and does not conform to the principle of opening and closing

For example, the following example: Nuwa produces all kinds of human beings through the Bagua stove, which is considered to be realized in a simple factory mode. The class diagram is as follows:

        

 

public class HumanFactory {
     
    public static <T extends Human> T createHuman(Class<T> c) {
             
        //Define a produced race             
        Human human = null;
             
        try {
                     
            //Produce a human species                    
            human = (Human)Class.forName(c.getName()).newInstance();
             
        } catch (Exception e) {
                    
            System.out.println("Race generation error!");
             
        }
            
        return (T)human;
     
    }

}

The method to generate a product object is very simple. You can directly call the corresponding method in a simple factory.

Human whiteHuman = HumanFactory.createHuman(WhiteHuman.class);


 

2, Factory method mode

The factory method mode makes up for the deficiency of the simple factory mode, which conforms to the principle of opening and closing.

Content: create the factory interface of the product object, and postpone the actual creation to the subclass.

Features: delay the class instantiation (creation of specific products) to the subclass (concrete factory) of the factory class, that is, the subclass decides which class should be instantiated (created).

Advantages:

  • Good encapsulation and clear code structure

The caller needs to create a product object and only need to know the class name of the product. And the caller doesn't care about how the product class changes, only the interface of the product. As long as the interface remains unchanged, the upper module of the system will not change.

  • Excellent scalability

You only need to modify the specific factory class or extend a factory class to add products.

Note: the factory method complies with the opening and closing principle, Dimitar's law, dependency reversal principle and Li type replacement principle

In the above example, consider using the factory method pattern. The class diagram is as follows:

        

 

public abstract class AbstractHumanFactory {
     
    public abstract <T extends Human> T createHuman(Class<T> c);

}

public class HumanFactory extends AbstractHumanFactory {
     
    public <T extends Human> T createHuman(Class<T> c) {
             
        //Define a productive race             
        Human human=null;
             
        try {
                      
            //Produce a human species                      
            human = (T)Class.forName(c.getName()).newInstance();

        } catch (Exception e) {
                  
            System.out.println("Race generation error!");
             
        }
             
        return (T)human;
     
    }

}

When you need to produce a product object, you only need to create the corresponding factory first, and then reproduce the product through the factory.

AbstractHumanFactory factory = new HumanFactory();

Human whiteHuman = factory.createHuman(WhiteHuman.class);


 

3, Abstract factory pattern

Abstract factory pattern is an upgraded version of factory method pattern.

Content: create the factory interface of the product object without specifying the specific class of the created product.

Feature: there is no need to specify the specific implementation class of the created product.

Advantages:

  • Good encapsulation

High level modules do not need to care about the specific implementation classes of each product, only need to know who the factory class is, and can create a required object.

  • Constraints within the product family are non-public

Constraints within a specific product family are implemented in the factory.

Disadvantages:

The expansion is very difficult and does not conform to the opening and closing principle.

As in the previous example, this time I will extend it a little, add a gender attribute, and consider using the abstract factory pattern. The class diagram is as follows:

        

 

public abstract class AbstractWhiteHuman implements Human {
     
    //The skin color of white people is white     
    public void getColor() {
             
        System.out.println("White people's skin color is white!");
     
    }
     
    //White speaking     
    public void talk() {
             
        System.out.println("White people can speak, generally speaking, a single byte.");
     
    }
}

AbstractBlackHuman,AbstractYellowHuman slightly  
public class MaleWhiteHuman extends AbstractWhiteHuman {
     
    //White male     
    public void getSex() {
             
        System.out.println("White male");
     
    }

}

FemaleBlackHuman,MaleBlackHuman,FemaleYellowHuman,MaleYellowHuman slightly
public interface HumanFactory {
     
    //Create a yellow race     
    public Human createYellowHuman();
     
    //Create a white race     
    public Human createWhiteHuman();
     
    //Create a black race     
    public Human createBlackHuman();

}

public class MaleFactory implements HumanFactory {
     
    //Producing black men     
    public Human createBlackHuman() {
             
        return new MaleBlackHuman();
     
    }
     
    //Producing white men     
    public Human createWhiteHuman() {
             
        return new MaleWhiteHuman();
     
    }
     
    //Produce a yellow man     
    public Human createYellowHuman() {
             
        return new MaleYellowHuman();
     
    }
}

FeMaleFactory slightly

In the end, we take the production of a white male as an example. The production process is as follows:

HumanFactory maleHumanFactory = new MaleFactory();

Human maleWhiteHuman = maleHumanFactory.createWhiteHuman();

 

58 original articles published, 61 praised, 20000 visitors+
Private letter follow

Keywords: Attribute

Added by slyte33 on Sun, 16 Feb 2020 05:57:37 +0200