Simple factory, factory, Abstract Factory

Source: Internet
Author: User

Simple factory mode:A dedicated class is defined to create instances of other classes. The created instance usually has a common parent class. It is also called the static factory method mode and belongs to the class creation mode.

The essence of the simple factory mode is that a factory class dynamically determines the product class to be created (these product classes inherit from a parent class or interface) based on the input parameters.

Intention: Provides a class for creating instances of a specific class according to certain conditions.

Roles and responsibilities:

  • Creator: the Core of the simple factory model. It is responsible for implementing the internal logic for creating all instances. The factory class can be directly called by the outside world to create the required product objects.
  • Abstract (product) role: the parent class of all objects created in simple factory mode, which describes the common public interfaces of all instances.
  • Concrete Product role: the creation target of the simple factory model. All created objects are instances of a specific class that acts as the role. Generally, it is a subclass of an abstract product class and implements all interface methods defined in the abstract product class.

Features:

  • Advantage: In simple factory mode, all created objects are instances of a specific class that acts as the role. In this mode, the factory class is the key to the entire model. It contains the necessary judgment logic to determine the specific class object to be created based on the information given by the outside world. You can directly create required instances based on the factory class during use without understanding how these objects are created and organized. It is conducive to the optimization of the entire software architecture.
  • Disadvantage: it is reflected in its factory class. Because the factory class has centralized the creation logic of all instances, it is not easy to do "high cohesion. In addition, when the number of product categories in the system increases, the factory category may be required to be modified accordingly, and the scalability is not very good.

 

 

Factory method mode: The Factory method mode abstracts the simple factory mode. There is an abstract factory class (which can be an abstract class and an interface). This class will not be responsible for specific product production, but will only formulate some specifications, the specific production work is postponed to its subclass.

Intention: Defines an interface for users to create objects, so that the subclass decides which class to instantiate. The factory method mode delays the instantiation of a class to its subclass.

Advantages: The principle of opening and closing is realized, and new products can be added without changing the factory.

Implementation points:

  • Two situations of the factory method mode: first, the Creator class is an abstract class and it does not provide implementation of the factory method it declares; second, creator is a specific class that provides the default implementation of a factory method.
  • The factory method can contain parameters.
  • The function of the factory is not only to create an object, but also to initialize the object and set parameters.

Effect:

  • Using factory methods to create objects within a class is usually more flexible than creating objects directly.
  • The factory method mode delays the creation of the specific object to be created to the subclass through the object-oriented method, thus providing an extension policy, this tightly coupled relationship is better solved.

Applicability:

  • When a class does not know the class of the object it must create.
  • When a class wants its subclass to specify the object it creates.
  • When a class delegates the responsibility of creating an object to one of multiple help sub-classes, and you want to localize the information of which help sub-classes are proxies.

Notes:

  • The base class is an abstract class. The mode must return a complete and workable class.
  • The base class contains default methods, and the subclass method is called unless these default methods are not competent.
  • The parameter can be passed to the factory to tell the factory which class is returned.

 

 

Abstract Factory Model: Provides interfaces for creating objects. Similar to the factory method, but a series of related products are returned here. The implementation process is also extended to sub-series classes. The difference from the factory method lies in their hierarchical model. The abstract base class of the factory method only has a son, but the abstract factory model has a son, and each son's son has a dependency relationship.

Intention: Provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.

Roles and responsibilities:

  • Abstract Factory: Declares the methods for generating a series of abstract products.
  • Concrete Factory: execute a series of abstract product generation methods to generate a series of specific products
  • Abstract product: declares an interface for a series of products.
  • Product: defines the product objects generated by a specific factory to implement product interfaces.
  • Client: Our application client (not an adult) uses Abstract products and abstract factories to generate objects.

 

Notes: Simple factory, factory method, abstract factory comparison

Simple factories, factory methods, and abstract factories all belong to the Creation Mode in the design mode. Its main function is to help us extract the instantiation part of the object, optimize the system architecture, and enhance the scalability of the system.

  • Simple Factory: factory classes in simple factory mode generally use static methods to return different object instances by receiving different parameters. Without modifying the code, it cannot be expanded.
  • Factory method: The Factory method provides a factory class for each product. Use different factory instances to create different product instances. Any product can be added to the same level structure.
  • Abstract Factory: Abstract Factory is designed for Product Family Concepts. For example, every automobile company may want to produce cars, trucks, and buses at the same time. Every factory must create cars, trucks, and buses. It is easy to add new product lines, but new products cannot be added.

Summary:

  • In the factory model, the most important thing is the factory class, not the product class. Product classes can be in multiple forms, multi-level inheritance, or a single class. But it should be clear that only one type of instance is returned for the interface in the factory mode. This is worth noting when designing the product class. It is best to have a parent class or a commonly implemented interface. In factory mode, the returned instance must be created by the factory, rather than obtained from other objects. The instances returned in factory mode can not be newly created, and the returned instances created by the factory can also be.

 

Differences:

  • Simple Factory: used to produce any product in the same level structure. (Unable to add new products)
  • Factory model: used to produce fixed products in the same level structure. (Any product can be added)
  • Abstract Factory: used to produce all products of different product families. (There is nothing to do with adding new products; Support for adding product families)

The above three factory methods have different levels of support in the hierarchical structure and product family. Therefore, you should consider which method should be used based on the actual situation.

 

Reposted from http://www.cnblogs.com/bloodmaster/archive/2010/03/01/1675856.html. Thank you for your hard work.

If you have any different opinions, please leave a message. If you are still breathing, you will be given a reply as soon as possible.

 

Sample Code

Class to be generated

enum CoreType{coreA = 0, coreB = 1};class CSinglecore{private:CSinglecore();};class CSinglecoreA:public CSinglecore{public:CSinglecoreA();};class CSinglecoreB:public CSinglecore{public:CSinglecoreB();};class CMulticore{private:CMulticore();};class CMulticoreA:public CMulticore{public:CMulticoreA();};class CMulticoreB:public CMulticore{public:CMulticoreB();};

 

Simple factory Mode

class CFactory{public:CSinglecore* CreateSinglecore(CoreType type){CSinglecore* pCore = NULL;if (type == CoreType::coreA)pCore = new CSinglecoreA;else if (type == CoreType::coreB)pCore = new CSinglecoreB;}};

Factory Model

class CFactory{public:virtual CSinglecore* CreateSinglecore() = 0;};class CFactoryA:public CFactory{public:virtual CSinglecore* CreateSinglecore(){CSinglecore* pCore = new CSinglecoreA;return pCore;}};class CFactoryB:public CFactory{public:virtual CSinglecore* CreateSinglecore(){CSinglecore* pCore = new CSinglecoreB;return pCore;}};

 

Abstract Factory Model

class CFactory{public:virtual CSinglecore* CreateSinglecore() = 0;virtual CMulticore* CreateMulticore() = 0;};class CFactoryA:public CFactory{public:virtual CSinglecore* CreateSinglecore(){CSinglecore* pCore = new CSinglecoreA;return pCore;}virtual CMulticore* CreateMulticore(){CMulticore* pCore = new CMulticoreA;return pCore;}};class CFactoryB:public CFactory{public:virtual CSinglecore* CreateSinglecore(){CSinglecore* pCore = new CSinglecoreB;return pCore;}virtual CMulticore* CreateMulticore(){CMulticore* pCore = new CMulticoreB;return pCore;}};

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.