Implementation of centralized factory-simple factory Model

Source: Internet
Author: User

Implementation of centralized factory-simple factory Model
Overview of simple factory models

First, the code of the various objects to be created is encapsulated into different classes. These classes are called specific product classes, abstract and extract their common code and encapsulate it in an abstract product class. Each specific product class is a subclass of the abstract product class. Then, a factory class is provided to create various products, A factory method is provided in the factory class to create a product. This method can create different product Objects Based on different input parameters; the client can obtain a product object by calling the factory method of the factory class and passing in corresponding parameters.

The simple factory mode defines a factory class, which can return instances of different classes according to different parameters. The created instances usually have a common parent class. The main point of the simple factory mode is: when you need something, you only need to pass in a correct parameter to get the object you need without knowing the creation details. Structure Design of simple factory Mode
The simple factory mode structure includes the following three roles:
(1) Factory (Factory role): The Factory class, which is the core of the simple Factory model and implements the internal logic for creating all product instances. The Factory class can be directly called by the outside, create the required Product object. A static factory method factoryMethod () is provided in the factory class, and its return type is abstract Product.
(2) Product (Abstract Product role): it is the parent class of all objects created by the factory class and encapsulates the public methods of various Product objects, its introduction will increase the flexibility of the system, so that you only need to define a general factory method in the factory class, because the specific product object created is its subclass object.
(3) a specific product role (ConcreteProduct): It is the creation target of the simple factory mode. All created Objects Act as instances of a specific class of this role.
A simple factory-based application software company wants to develop a chart Library Based on the Java language. This chart library can provide various charts with different appearances for the application system, such as bar charts, pie charts, and line charts. Sunny software company chart library designers want to develop a flexible and easy-to-use chart library, and more easily expand the chart library, so that some new types of charts can be added in the future.
The Code is as follows:
# Include
 
  
Using namespace std; // abstract Chart interface: Abstract product class Chart {public: virtual void display () = 0 ;}; // columnar Chart: specific product class HistogramChart: public Chart {public: HistogramChart () {cout <create a bar Chart <
  
   
Display (); return 0 ;}
  
 

Simple factory mode summary simple factory mode provides a dedicated factory class for creating objects and separating object creation from object usage. Primary Advantage 1. Simple factory mode achieves object creation and use separation. Main disadvantage 1. The factory class focuses on the Creation logic of all products and has heavy responsibilities. The entire system is affected if the product fails to work normally. 2. added the number of classes in the system, increased the complexity and difficulty of understanding the system. 3. It is difficult to expand the system. Once a product is added, the factory logic has to be modified. Applicable scenario 1. The client only knows the parameters of the input factory class and does not care about how to create objects.

 

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.