His girlfriend Dodo gets angry and says, I want to eat ice cream. The author in the heart ah happy zi of, an ice cream can coax back, not yiyue? !

When the ice cream came back, she said, “I don’t want ice cream. I suddenly want pizza.” He said, “No problem,” and went to buy the pizza.

“The pizza is too hot. I’m going to have cheesecake.”





This is where the factory approach pattern comes in, perfect for addressing constant product change. What is the factory method pattern? How does the factory approach pattern address product change? Take a look…

1. Factory method mode

define

Define a factory interface to create an object. The factory method postpones instantiation to subclasses.

Definitions are always abstract, and a UML diagram of the factory method pattern is shown below:





Factory method pattern UML.png

As you can see from the figure above, the factory method pattern involves concepts of four roles:

– Abstract Factory role: The core of the factory method pattern, and the factory class that creates the product must implement this interface.

– Concrete Factory role: This role implements the abstract factory interface. The details of how to create the product class are done in this implementation class.

– Abstract product roles: Superclasses of all product classes responsible for implementing abstract definitions of product commonalities.

– Specific product role: This role implements abstract product interfaces and is responsible for specific business logic of different products.

Second, the actual combat

In the code

The abstract factory role code is as follows:

Product Public Product Factory (String productType); public interface Factory (String productType); }Copy the code

Here, the factory producing ice cream and pizza can only produce corresponding products with the factory. The specific role code of the factory is as follows:

// Ice cream factory, Public class implements Factory {@override public Product Factory () {return new IceCream(); }}Copy the code

See the abstract role Product above, look at the code:

public interface Product {

    public void product();

}
Copy the code

The specific role code is as follows:

Public class implements Product {@override public void Product () {/** * implements Product () {/** * implements Product (); !" ); }}Copy the code

OK, the factory mode system code is almost complete, let’s see how the client uses factory mode, the ice cream is ready:

Public class Client {public static void main(String[] args){// iceCreamFactory = new iceCreamFactory (); Product iceCream = iceCreamFactory.factory(); iceCream.product(); }}Copy the code

Running the client code results in the following:

Ice cream is ready!!

extension

The order of ice cream has been completed! Pizza instead of ice cream? OK, let’s make the pizza now. Again, implement the abstract factory and abstract product roles first.

Pizza factory implementation is as follows:

Public class PizzaFactory implements Factory {@override public Product Factory () {return new Pizza(); }}Copy the code

Next up is pizza:

Public class implements Product {@override public void Product () {/** ** / system.out.println (" Pizza is ready!! ") ); }}Copy the code

Now we implement the production of pizza in the client side, the code is as follows:

Public class Client {public static void main(String[] args){// iceCreamFactory = new iceCreamFactory (); Product iceCream = iceCreamFactory.factory(); iceCream.product(); PizzaFactory = new pizzaFactory (); Product pizza = pizzaFactory.factory(); pizza.product(); }}Copy the code

The running results are as follows:

Ice cream is ready!!

Pizza is ready!!

Advantages and disadvantages of factory method mode

advantages

1) Excellent scalability. If you don’t want to eat pizza and want to eat cheesecake, you just need to add the specific factory and specific product category of cheesecake, and it won’t affect the production of other products.

2) Greatly reduce the coupling degree of the code, the client end is the product itself, without knowing and operating the production process of the product.

disadvantages

1) When new products are added, specific factory categories and product categories need to be added. When there are a large number of products, the complexity of the system will increase, and it will also increase the difficulty of understanding the system.

Four, comparison,

Contrast this with the strategy model: the most asked question. What is the difference between the factory method pattern and the policy pattern? The difference is that the strategic pattern emphasizes providing different strategic alternatives, while the factory method pattern emphasizes the product itself.

The strategic pattern will also be discussed next. If you have a thorough understanding of the strategic pattern, you will understand it when you compare it with the factory method pattern.

conclusion

First of all, many people gave me good advice on the singleton pattern in the last article, and it has been improved in this article. The utilization rate of factory mode is also very high. Mastering it will improve the system architecture greatly. Next post: The Builder pattern





The development of AndroidJet