Design Pattern series factory pattern

Source: Internet
Author: User

Design Pattern series factory pattern
Design patterns have always been the most important field in object-oriented software design, and there are countless topics around them. Among them, 23 design patterns represented by GOF are even more classic. In this series, I will explain the ideas, functions, and advantages and disadvantages of each design pattern from my own understanding. I. The concept of the design pattern must be explained. There are two problems first: 1. What is the design pattern? 2. Why design patterns? GOF defines the design pattern as follows: The Design Pattern describes the classes used to solve general design problems and objects that communicate with each other in specific scenarios. More specifically, to put it into object-oriented software design, the design pattern can be regarded as the design experience summarized in practice and will be repeated in the system. Therefore, the design pattern can help us reuse the successful design and architecture more easily and easily. More specifically, the key to maximizing reuse lies in the change of new and existing requirements, the system design capability can be improved accordingly to avoid the huge cost of redesign. Each design pattern is achieved by ensuring that the system changes in a specific way, and each design pattern allows an aspect of the system structure to be independent from other aspects, this makes it adaptable to a special change. This leads to the method of selecting a design pattern: first, consider which aspects of the system have variability, and the selected design pattern will not be re-designed due to these variables. GOF divides the Task Type completed in the design mode into three categories: Creation type, structure type, and behavior type. The creation mode is used for object creation, and the structure mode is used for processing the combination of classes or objects; A behavior pattern describes how a class or object interacts and assigns responsibilities. At the same time, the range mode is divided into the class mode and the object mode. The class mode processes the relationship between the class and the subclass. It is created through inheritance and is static, and the compilation period is determined; the object mode processes the relationship between objects, which is dynamic and variable during runtime. The specific classification is shown in the table: create schema behavior schema Factory method Adapter Interpreter template method Object Schema Abstract factory builder prototype singleton Adapter bridge composite decorator facade flyweight proxy Chain of responsibility command iterator mediator memento observer state strategy visitor creation Schema class Mode delays object creation to subclass, the creation object mode is delayed to another object. The structure class mode uses the Inheritance Mechanism to combine classes. The structure Object Mode describes the combination of objects. Behavior patterns use inheritance to describe the algorithm and control flow, and behavior object patterns use a group of objects to collaborate to complete the task. In the partial classification mode, you can avoid creating subclass by introducing parameters or template types. This series starts with the factory method mode in the Creation Mode and introduces 23 GOF design modes one by one. Ii. factory method mode if a class does not know the type of the object to be created, if you want the subclass to specify the type of the object to be created, if you want to delegate the responsibility of creating an object to a subclass and localize this agent process, you can consider using the factory method mode. The following is an example, which will be mentioned many times in the subsequent design pattern. Please remember it first. Now we need to develop a software for Chip Design. We can design different graphics on the mask. Different masks have different parameters, such as the exposure mode and angle, and there may be many types of images. Without affecting understanding, we assume that there are only three types of images: Circle, rectangle, and triangle, the effects of drawing the same image on different masks are different. For the same mask, we need to get an object that can be drawn. As for the circle, rectangle, or triangle drawn, the subclass can know that the mask can only get the interface. The class design diagram is as follows: the specific code implementation is as follows: copy the code // mask. hpp # ifndef MASK_HPP # define MASK_HPP class MaskFigure {public: virtual ~ MaskFigure () = 0; protected: MaskFigure () ;}; class MaskRound: public MaskFigure {public: MaskRound ();~ MaskRound () ;}; class MaskRec: public MaskFigure {public: MaskRec ();~ MaskRec () ;}; class MaskTri: public MaskFigure {public: MaskTri ();~ MaskTri () ;};# endif // mask. cpp # include <iostream> # include "mask. hpp "using std: cout; using std: endl; MaskFigure: MaskFigure () {} MaskFigure ::~ MaskFigure () {} MaskRound: MaskRound () {cout <"Draw roundness on Mask" <endl;} MaskRound ::~ MaskRound () {} MaskRec: MaskRec () {cout <"Draw rectangle on Mask" <endl;} MaskRec ::~ MaskRec () {} MaskTri: MaskTri () {cout <"Draw triangle on Mask" <endl;} MaskTri ::~ MaskTri () {}// factory. hpp # ifndef MASKFACTORY_HPP # define MASKFACTORY_HPP # include "mask. hpp" class FigureFactory {public: virtual ~ FigureFactory () = 0; virtual MaskFigure * CreateFigure () = 0; protected: FigureFactory () ;}; class RoundFactory: public FigureFactory {public: RoundFactory ();~ RoundFactory (); MaskRound * CreateFigure () ;}; class RecFactory: public FigureFactory {public: RecFactory ();~ RecFactory (); MaskRec * CreateFigure () ;}; class TriFactory: public FigureFactory {public: TriFactory ();~ TriFactory (); MaskTri * CreateFigure () ;};# endif // factory. cpp # include <iostream> # include "factory. hpp "using std: cout; using std: endl; FigureFactory: FigureFactory () {} FigureFactory ::~ FigureFactory () {} RoundFactory: RoundFactory () {cout <"Init RoundFactory" <endl;} RoundFactory ::~ RoundFactory () {} MaskRound * RoundFactory: CreateFigure () {return new MaskRound ();} RecFactory: RecFactory () {cout <"Init RecFactory" <endl ;} recFactory ::~ RecFactory () {} MaskRec * RecFactory: CreateFigure () {return new MaskRec ();} TriFactory: TriFactory () {cout <"Init TriFactory" <endl ;} triFactory ::~ TriFactory () {} MaskTri * TriFactory: CreateFigure () {return new MaskTri ();} // main. cc # include <memory> # include <iostream> # include "factory. hpp "# include" factorytml. hpp "using std: shared_ptr; int main () {shared_ptr <RoundFactory> roundfac (new RoundFactory (); shared_ptr <MaskRound> mrd (roundfac-> CreateFigure ()); shared_ptr <RecFactory> recfac (new RecFactory (); shared_ptr <MaskRec> mrc (recfac-> CreateF Igure (); shared_ptr <TriFactory> trifac (new TriFactory (); shared_ptr <MaskTri> mti (trifac-> CreateFigure (); return 0;} copy the code roundness, the construction of rectangle and triangle is delayed to RoundFactory, RecFactory, TriFactory, and other derived classes respectively. Therefore, the factory method mode becomes a virtual constructor. It is not difficult to find that MaskFigure has several sub-classes, and FigureFactory also needs to correspond to the number of sub-classes. When MaskFigure adds a graph, FigureFactory must add the corresponding factory method. Sometimes there will be too many sub-classes, which is not easy to maintain, templates or parameterization can be introduced in c ++ to avoid creating subclasses. An example of the factory method implemented by a template is as follows: copy the code // factorytml. hpp # ifndef FACTORYTML_HPP # define FACTORYTML_HPP # include <iostream> using std: cout; using std: endl; class FigureFactoryTml {public: FigureFactoryTml () {cout <"Init FigureFactoryTml" <endl ;}~ FigureFactoryTml () {}template <typename T> T * CreateFigure () {return new T () ;};# endif // main. cc... Shared_ptr <FigureFactoryTml> fft (new FigureFactoryTml (); shared_ptr <MaskRound> mrdp (fft-> CreateFigure <MaskRound> ()); shared_ptr <MaskRec> MHD (fft-> CreateFigure <MaskRec> (); shared_ptr <MaskTri> mtip (fft-> CreateFigure <MaskTri> ());...

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.