Java Design Pattern learning 2 -- factory pattern [original]

Source: Internet
Author: User
Tags pear

Yesterday I spoke about the singleton model. Today I will talk about the factory model.
In object-oriented programming, the factory mode is a frequently used mode. In the factory mode, there is a common abstract parent class. The parent class has a group of subclass instances and a factory class. When creating different subclasses, the task is handed over to the factory class, you only need to pass necessary parameters to the factory class.
There are three factory models:
1. Simple factory mode;
2. The factory method mode, also known as the polymorphic factory mode;
3. Abstract Factory mode, also known as toolkit or toolkit mode.
  
Let's talk about the simple factory model first.
A simple factory mode returns a subclass instance of a parent class based on the provided data.
For ease of understanding, I wrote the following example:
// Parent class
Abstract Class fruit {
 
Protected string fruitname;
Protected int price;

}

// Subclass
Public class Apple extends fruit {
 
Public Apple ()
{
Fruitname = "apple ";
Price = 2;
}

}

// Subclass
Public class pear extends fruit {
 
Public pear ()
{
Fruitname = "Pear ";
Price = 1;
}

}

// Factory type
Public class gardener {
 
Public static fruit getfruit (string fruitname)
{
If (fruitname. Equals ("apple "))
{
Return new Apple ();
}
Else if (fruitname. Equals ("Pear "))
{
Return New Pear ();
}
Else
{
System. Out. println ("not have the fruit! ");
System. Exit (0 );
}
Return NULL;
}
}
// Test class
Public class test {

Public static void main (string [] ARGs ){

Fruit F1 = gardener. getfruit ("apple ");
Fruit F2 = gardener. getfruit ("Pear ");
System. Out. println ("the price is :");
System. Out. println (f1.fruitname + ":" + f1.price );
System. Out. println (f2.fruitname + ":" + f2.price );
}
}

The result is as follows:
The price is:
Apple: 2
Pear: 1

See? If you want to find a fruit, you only need to tell the gardener the name of the fruit to be found. You don't have to worry about how to find it. The gardener will help you find it.
  
In simple factory mode, a factory class is in the center of product class (parent class) instantiation. It knows every product (subclass) and determines which product class should be instantiated. This mode allows the client to be relatively independent from the product creation process. However, if a new product is added to the system, you also need to modify the factory class and add the necessary logic to the factory class.

Next is the factory method.
In the simple factory model, it is big and complete, just like a big and complete gardener to deal with all the fruits. In the factory method mode, it is fine and many, satisfying the requirement that "one encapsulation can introduce at most one changeable factor", that is, an abstract factory class can derive multiple specific factory classes, each factory class can only create one instance of a specific product class.
The factory method is further abstracted and promoted by the simple factory model. In the factory method model, no longer only one factory class determines which product class should be instantiated, it enables the factory to implement Abstract Factory interfaces, and puts how to produce a specific thing in a specific factory for implementation. The so-called "delay to implementation in the subclass ", this decision is handed over to the subclass. Classes at the center of the factory method mode do not even touch the details of which product class should be instantiated.
In the factory base class, you can also encapsulate the parameter variables required to generate the target object. You can set these parameter variables when generating the factory subclass object to prepare parameters for generating the target object. Different factory subclasses are generated by providing different parameter variable values. This can be achieved through a simple factory approach, and decision points can be implemented in this simple factory class, this is only a factory subclass returned by a simple factory. This factory subclass can then generate the target object.
Taking the above example as an example, there was a gardener manager who trained a group of small gardeners below, each of which had only one kind of fruit. If you look for fruit, you can directly ask the gardener who cares about the fruit to go. The gardener manager may not even know which gardener is going to find the fruit.
The following is some explanatory code:
Public interface gardenerdirector {
Public fruit getfruit ();
}

Public class gardenerofapple implements gardenerdirector
{
Public fruit getfruit ()
{
Return new Apple ();
}
}

Public class gardenerofpear implements gardenerdirector
{
Public fruit getfruit ()
{
Return New Pear ();
}
}

I will not write any further details.
The factory method mode allows the system to add new products without modifying the factory class (whether the factory parent class or factory subclass), that is, to comply with the on-off principle, as long as there is no application that includes the simple factory model.
    
Finally, let's talk about the abstract factory model.
Abstract Factory mode is the most abstract and general form in all forms of factory mode. It is mainly used to solve the problem of several product families for specific products, it refers to a family of products with different product levels and functions.

 

The figure shows four product families distributed in three different product levels. You only need to specify the product family of a product and its hierarchical structure.
The understanding of the figure above is as follows: different products have different levels in a hierarchical structure, so there are three levels on them. Different products at the same level in the hierarchical structure constitute a product family, so there are four product families above.
Abstract Factory refers to all objects in a product family that can be created by a factory level structure into different product levels. If you use a diagram to describe it, for example:

Our understanding is as follows: an abstract factory class generates n (n> = 1) specific factory classes in a hierarchical structure, each specific factory class can create all objects in a product family of the same level.
Abstract Factory mode is the further abstraction and promotion of factory method mode. In the case of only one product family, the abstract factory mode actually degrades to the factory method mode.
There are more than one product family in a system product. When products belonging to the same product family are designed and used together, we need to use the abstract factory model.
In the abstract factory mode, define the methods for creating products in an abstract factory as factory methods to conform to the open-close principle.

Let's talk about the factory model. What kind of experience will be added next time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.