Simple factory (Simple Factory)

xiaoxiao2021-03-06  37

Mouse reading

Simple factory model is a class creation mode and is also called Static Factory Method mode. These are seen from the book, but the theory is theory is theory, and it is necessary to become a truly useful thing in our hands! Or do you want to learn this?

Introduction of simple factory model

For example, another farm company specializes in selling all kinds of fruits to the market. The following fruits are required in this system:

Grape grape

Strawberry strawberry

Apple Apple

It naturally thought of objects here, all of these three of these three are fruit, practical fruit, then we build a wide range of applications to separately separate from other plants in the farm.

The fruit interface specifies the interfaces that all fruits must be implemented, including any fruits must have: Plant (), growing grow () and harvest harvest (), from the above class chart, the following three fruits All methods defined in the interface are implemented. The Applet class is a kind of fruit, but because it is a perennial plant, there is a total of Treeage's nature. Describe the aramid of the apple tree. The code is actually the same as the expression of the class diagram. The grape has scored two kinds of seeds and seeds, so there is more nature of SEEDLESS. Strawberry is also a kind of fruit.

The gardener of the farm is also part of the system, naturally to be represented by a suitable class. This class is the Fruitgardener class. The structural diagram is as follows:

When using the client, the client only needs to call the static method of Fruitgardener Factory (). You can refer to the code below:

Try {Fruitgardener.Factory ("grape"); Fruitgardener.Factory ("apple"); Fruitgardener.Factory ("strawberry"); ....} catch (badfruitExcection e) {....

Through the example above this book, we can understand this design method very clearly. Let's learn the structure of simple factory model.

Simple factory model is a class creation mode, assuming that there are three elements of factories, specific products, and abstract products. The simple factory model is an instance of which product class is created by the factory class according to the incoming parameters. The following uses a schematic implementation as an example of the structure of the simple factory model:

It can be seen that simple factory models involve three roles such as factory roles, abstract product roles, and specific product roles:

Factory (CREATOR) Role: The role is the core of factory method model, with commercial logic related to applications. The factory class creates a product object under the direct call of the client, which is often implemented by a specific Java class.

Abstract Product Role: The class that serves this role is the parent class of the object created by the factory method mode, or the interface they share. Abstract product roles can be implemented with an interface or abstract class.

Concreate Product Role: Any object created by the factory method mode is an instance of this role, and the specific product role is implemented by a specific Java class.

In this schematic, only one product is displayed, but in particular, it will generally encounter a number of specific products, and we can consider the implementation of the implementation in this case.

Simple factory model is also related to other models, such as single example mode, multi-case model, memo mode, MVC mode, etc.

The advantages of simple factory model, the core of the model is factory class. This class contains the necessary judgment logic, which can determine when to create which shovel level. The client can exempt the responsibility of directly creating product objects, but only responsible for "consumption" products, simple factory model achieves the separation of responsibility through this practice. Disadvantages of simple factory model: When the product has a complex multi-level hierarchy, the factory class only has itself. It is the shortcomings of the model to not change. This factory class concentrates all products creation logic, forming an omnipotent all-round class, some people call this kind of "God". If this all-around class represents a specific gardener of the farm, then this gardener needs to be responsible for all products, become a critical task of the farm. When is it possible to work properly, the entire farm has to be affected. These disadvantages can be overcome in the factory method mode, so I decided to look at it.

鼹 鼹 2005-1-26 in Xi'an

转载请注明原文地址:https://www.9cbs.com/read-63973.html

New Post(0)