Create-mode ———— Factory method mode __ Factory mode

Source: Internet
Author: User
1. Origin

In simple Factory mode, each button is packed by the Button factory class to unify all the product creation, now we put this creation to the specialized factory subclass to complete, we first define an abstract button factory class, in the definition of a factory class to generate round button, rectangular button, square button, etc.                                                                                                                                           They implement the methods defined in the abstract button, This abstraction makes this structure possible to introduce new products without modifying specific factory classes, and if New button types are available, the new button can be obtained simply by creating a specific factory class for this new type of button, which undoubtedly makes The factory method pattern has the superiority which surpasses the simple factory pattern, conforms to "the opening and closing principle" more. 2. Define

The factory-mode model (Factory method pattern) is also known as the factory pattern, or the virtual constructor or the Polymorphic factory (polymorphic Factory) pattern. In factory method mode, the factory parent class is responsible for defining a common interface for creating product objects. The factory subclass is responsible for generating specific product objects, so that the purpose is to defer the instantiation of the product class to the factory subclass, that is, through the factory subclass to determine which particular product class should be instantiated.
3. Implementation of the Code

Public abstract class Factory {
	public  abstract Factory getfactory ();
}
Class Afactory extends factory{

	@Override public
	Factory getfactory () {return
		new afactory ();
	}
	
}

(Your own example looks a bit low, but looks good understanding, the following is the implementation of C + + form)

#include "ConcreteFactory.h"
#include "ConcreteProduct.h"

product* Concretefactory::factorymethod () {

	return  new Concreteproduct ();
}

#include "Factory.h"
#include "ConcreteFactory.h"
#include "Product.h"
#include <iostream>
using namespace std;

int main (int argc, char *argv[])
{
	Factory * fc = new Concretefactory ();
	Product * prod = Fc->factorymethod ();
	Prod->use ();
	
	Delete FC;
	Delete prod;
	
	return 0;
}



4. Advantages of the Model

In the factory method mode, the factory method is used to create the product that the customer needs, while also hiding the details of which specific product class will be instantiated, and the user needs to be concerned with the corresponding factory of the desired product, regardless of the creation details or even the class name of the specific product class. The design of polymorphism based on factory role and product role is the key of factory method model. It allows the factory to determine which product objects to create, and the details of how to create the object are fully encapsulated within the factory. The factory method pattern is also called the Polymorphic factory pattern because all the specific factory classes have the same abstract parent class. Another advantage of using the factory method pattern is that when new products are added to the system, there is no need to modify the interfaces provided by the abstract factory and abstract products, no need to modify the client, no need to modify other specific factories and specific products, as long as the addition of a specific factory and specific products. In this way, the scalability of the system has become very good, in full compliance with the "open-closing principle."

5. Shortcomings of the pattern

When you add a new product, need to write new specific product classes, but also to provide corresponding to the specific factory class, the number of classes in the system will increase, to some extent, increase the complexity of the system, there are more classes need to compile and run, will bring some additional cost to the system. Considering the scalability of the system, it is necessary to introduce the abstraction layer, in the client code, the abstraction layer is used for the definition, which increases the abstraction and difficulty of the system, and it may need to use the technology of DOM and reflection to increase the difficulty of realizing the system.

6. Application of the scene a class does not know the object it needs: In the Factory method mode, the client does not need to know the class name of the specific product class, only needs to know the corresponding factory, the specific product object is created by the concrete factory class, the client needs to know the factory class that creates the specific product. A class specifies, through its subclasses, which object to create: In Factory method mode, the abstract factory class only needs to provide an interface to create the product, and its subclasses to determine the object to be created, using the object-oriented polymorphism and the Richter substitution principle, when the program runs, the subclass object overwrites the parent class object. Which makes the system easier to expand. Delegate the task of creating an object to one of several factory subclasses that the client can use without concern about which factory subclass creates the product subclass, which is dynamically specified when required, and stores the class name of the specific factory class in the configuration file or database.
References: Http://design-patterns.readthedocs.io/zh_CN/latest/creational_patterns/factory_method.html#id7


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.