C + + design mode 8 (Factory method factory methods)

Source: Internet
Author: User

5. "Object Creation" class mode

Bypassing new with the object creation class mode avoids the tight coupling (dependency on the specific class) that results from the object creation (new) process, which supports the stability of object creation. It is the first step after the interface abstraction.

5.1 Factory method

Motivation:

In software systems, it is often the work of creating objects, and the specific types of objects that need to be created often change due to changes in requirements.

How to deal with this change? How do you bypass the general object creation method (new) and provide a "encapsulation mechanism" to avoid tight coupling between client programs and this "concrete object creation work"?

code example:

Still consider the file splitter case, regardless of the creation object-independent part of the code, temporarily ignoring memory management, focusing on the application of factory method mode.

In implementation code 1, different types of file splitter classes (File,pic,video, etc.) inherit the file splitter abstract base class.

In MainForm, the MainForm class dependency (compile-time dependent) has a specific class binaryspitter, which violates the dependency inversion principle. In this case, there is a tight coupling resulting from the creation of the object described in the motive.

For a solution with a factory method pattern, see Code 2, consider the following process:

1. Objects created with the new method must be dependent on a specific class and not be taken, so consider creating a class that takes the return value of the method as the result of creating the object;

2. However, simply returning an object of a specific class still does not solve the problem of dependency inversion, so consider designing the class as an abstract class, and the specific return is determined by its subclasses.

3. In accordance with the above ideas to create a factory-based class Splitterfactory (abstract Class), specific factories (binarysplitterfactory, etc.) return to specific objects.

4.MainForm using the process, declare a factory field, in the concrete creation of the process with a unified dependency on the abstract creation method, that is,

Isplitter * splitter= factory->createsplitter (); Equivalent to polymorphic new

The object type that factory specifically refers to is passed in by the MainForm constructor through the outside world.

Note: As can be seen from the above analysis and code, the use of factory method patterns is not intended to eliminate changes (in fact, changes cannot be eliminated), but rather to exclude changes from mainform (the analogy will change to a small area), making it more controllable, Thus, the design and use of the file splitter satisfies the principle of object-oriented design and shows advantages in coping with the change.

1//filespiltter1.cpp 2 class isplitter{3 public:4     virtual void split () =0; 5     Virtual ~isplitter () {} 6}; 7  8 Class Binarysplitter:public isplitter{9     };11 class Txtsplitter:public isplitter{13};15     class Pic  Turesplitter:public isplitter{17/     };19 class Videosplitter:public isplitter{21};23     Class Mainform:public Form26 {     textbox* txtfilepath;28     textbox* txtfilenumber;29 progressbar*     progressbar;30 public:32     void button1_click () {         isplitter * splitter=37             New Binarysplitter ();//dependent on the specific type of         splitter->split ();     }42};

 1//ispiltterfactory.cpp 2 3//abstract Class 4 class isplitter{5 public:6 virtual void split () =0; 7 Virtual ~isplitter () {} 8}; 9 10 11//Factory base class splitterfactory{13 public:14 virtual isplitter* createsplitter () =0;15 virtual ~splitterfact Ory () {}16};17//filespiltter2.cpp19 20//Specific class Binarysplitter:public isplitter{22,};24 class Txtspli Tter:public isplitter{26};28 class Picturesplitter:public isplitter{30};32 class Videosplitter:pu Blic isplitter{34 35};36 37//Specific factory class Binarysplitterfactory:public splitterfactory{39 public:40 virtual ISp litter* Createsplitter () {return new Binarysplitter ();}43};44 class Txtsplitterfactory:public Splitt erfactory{46 public:47 Virtual isplitter* createsplitter () {return new Txtsplitter ();}50};51-Clas s picturesplitterfactory:public splitterfactory{53 public:54 virtual isplitter* createsplitter () {return NE W PicturesplitteR ();}57};58 class videosplitterfactory:public splitterfactory{60 public:61 virtual isplitter* Createsplitt ER () {videosplitter return new ();}64};65//mainform2.cpp67 class Mainform:public Form68 {. Spl itterfactory* factory;//Factory public:72 mainform (splitterfactory* Factory) {The This->factory=fact ory;75}76 button1_click () {Isplitter * splitter=81 FACTORY->CR Eatesplitter (); Polymorphic new82 Splitter->split (); 84 85}86};

Schema Definition:

Define an interface for creating an object, and let subclasses decide which class to instantiate, Factory method causes the instantiation delay of a class (purpose: Decoupling, means: virtual function) to subclasses.

Class Diagram:

Summarize:

The Factory method pattern is used to isolate the coupling between the consumer of a class object and a specific type. In the face of a specific type that is constantly changing, tight coupling relationships (new) can lead to software fragility.

The Factory method mode, by means of object-oriented approach, defers all the concrete objects created to subclasses, thus implementing a strategy of extension (not change), which solves this tight coupling relationship better.

Factory method mode solves the requirement change of "single object". The disadvantage is that the creation method/parameter is required to be identical.

C + + design mode 8 (Factory method factory methods)

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.