Design Patterns factory pattern - factory method pattern


Design Patterns factory pattern - factory method pattern

Hello everyone, welcome to the dirty dirty bomb, today a small Division Division has received a new live - Pizza Project.

Source: Kaige Java (kaigejava)

demand:

Pizza Project:

To facilitate the expansion pizza varieties to be easy to maintain, easy to extend the time to be able to run

Take a look at pizza family design:

After analyzing small Division Division to obtain: Pizza require baking (bake), cutting (cut), packaging (box) There is a pizza raw material preparation (prepare). Where the change is ready to prepare raw materials. Different pizza different raw materials, but need to be baked, cutting and packaging. So, we are here to do an abstract method of raw materials, not concrete reality. After class diagram gave the following:

document_image_rId9.png

The first design: the use of simple factory pattern (refer to the Department on a small Division noodle items) not found. why?

With the pizza more and more fire, a lot of places have to JM. If JM words, every place must create a simple factory pattern as the same code, it is necessary to create a lot of copies, when maintenance is very troublesome.

The second design choice: the factory method pattern.

Let's look at the structure of the project:

document_image_rId10.png

Description:

1: domain: pojo specific object. They are inherited from class PizzaAbstract

Which begins with BeiJin, are Beijing chain. Starting with ShanHai are Shanghai chain.

2 and 3 are pizza and pizza orders abstract class

4: Order chain class specific point. They are inherited from class PiazzOrderAbstract

5: Test class

Let's look at two abstract classes.

PizzaAbstract categories:

document_image_rId11.png


PizzaOrderAbstract categories:

document_image_rId12.png

In view of a Beijing greekpizza:

document_image_rId13.png

Conventional Mimi BeiJinOrderPizza:

document_image_rId14.png

Test categories:

document_image_rId15.png

View the results:

document_image_rId16.png

Through the above code shows, we get the following:

Factory Method Pattern design:

The project pizza in the pizza object instance of functional abstraction to abstract methods, to achieve specific functions in different JM shop.

Factory Method pattern:

Defines an abstract method to create the object, it determines the class to be instantiated by subclasses. From this statement, we can know that the factory method pattern is instantiated (created) object to the postponement of the subclass.


Factory method comparison mode and a simple factory pattern:

Simple factory pattern:

The class identifier, the factory object can be obtained by the object instance of a particular class. Inside the factory object, an object instance of the class is as follows:

document_image_rId17.png

Instantiating the object, the object is inside the factory.

Factory Method pattern:

It is to create objects, extract out. The method is to create pizza, drawn to a specific class.

We take a look at pizza order object:

document_image_rId18.png

In the concrete call:

PizzaOrderAbstract bjOder = new BeiJinOrderPizza (); call concrete subclasses.

document_image_rId19.png

And the object of instantiation is performed in the subclass.

Contact Kaige - "Kaige Java (kaigejava)

Kaige personal blog: www.kaigejava.com. Personal blog can be private letter Kaige oh ~

Factory Method pattern to create the plant through an abstract factory class, then from the factory to create a certain object through a certain way, so that if there is new capacity C appears no need to modify existing code as long as a new product from the factory and subclasses subclasses can be friends.


Guess you like

Origin blog.51cto.com/kaigejava/2432808