Design Pattern factory pattern exercises, design pattern factory exercises

Source: Internet
Author: User

Design Pattern factory pattern exercises, design pattern factory exercises

The most basic factory model in Design Patterns

It is determined by the type of input and the type of operation to be selected.

The idea is basically the same as that of input 1 in the process orientation to execute func1 (); input 2 to execute func2 ().

 

# Include <iostream> using namespace std; enum eShoeType {leather = 0, rubber}; class CShoe {public: virtual void What () = 0 ;}; class CLeatherShoe: public CShoe {public: void What () {cout <"I am leather shoe. "<endl ;}}; class CRubberShoe: public CShoe {public: void What () {cout <" I am rubber shoe. "<endl ;}}; class CShoeFactory {public: CShoe * GetShowInstance (eShoeType type) {cout <" factory showinstance... "<endl; Switch (type) {case leather: return new CLeatherShoe (); case rubber: return new CRubberShoe (); default: return NULL ;}}; void TestWithCpp () {CShoeFactory factory; CShoe * pShoe = NULL; pShoe = factory. getShowInstance (leather); if (NULL! = PShoe) {pShoe-> What (); delete pShoe; pShoe = NULL;} pShoe = factory. GetShowInstance (rubber); if (NULL! = PShoe) {pShoe-> What (); delete pShoe; pShoe = NULL ;}} //////////////////////////////////////// ////////////// typedef struct SHOE {int type; void (* print_shoe) (struct SHOE *);} Shoe; void PrintLeatherShoe (Shoe * pShoe) {if (NULL! = PShoe) printf ("I am leather shoe \ n");} void PrintRubberShoe (Shoe * pShoe) {if (NULL! = PShoe) printf ("I am rubber shoe \ n");} Shoe * FactoryShoe (int type) {Shoe * pShoe = NULL; pShoe = (Shoe *) malloc (sizeof (Shoe); if (NULL = pShoe) return NULL; memset (pShoe, 0, sizeof (Shoe); if (leather = type) {pShoe-> type = type; pShoe-> print_shoe = PrintLeatherShoe;} else if (rubber = type) {pShoe-> type = type; pShoe-> print_shoe = PrintRubberShoe ;} else {free (pShoe); pShoe = NULL; return NULL;} return pShoe;} void T EstWithC () {printf ("\ n test C version factory mode \ n"); Shoe * pShoe = FactoryShoe (leather); if (NULL! = PShoe) {pShoe-> print_shoe (pShoe); free (pShoe); pShoe = NULL;} pShoe = FactoryShoe (rubber); if (NULL! = PShoe) {pShoe-> print_shoe (pShoe); free (pShoe); pShoe = NULL;} pShoe = FactoryShoe (994); if (NULL! = PShoe) {pShoe-> print_shoe (pShoe); free (pShoe); pShoe = NULL ;}} //////////////////////////////////////// //// // int _ tmain (int argc, _ TCHAR * argv []) {TestWithCpp (); TestWithC (); return 0 ;}

 


[Reward] understanding the design model-the factory model and the factory Method

Factory mode definition: Provides interfaces for object creation.

Why?
The factory mode is our most commonly used model. The famous Jive forum has used the factory mode extensively. The factory mode can be seen everywhere in Java program systems.
Why is the factory model so commonly used? Because the factory mode is equivalent to creating the new instance object, we often need to generate instance objects according to Class, for example, A a = new A () Factory mode is also used to create instance objects, so in the future, there will be multiple eyes. Can you consider using the factory model? Although doing so may do more work, it will bring more scalability and as few modifications as possible to your system.
Let's take the Sample class as an example. If we want to create an instance object for the Sample:
Sample sample = new Sample ();
However, we usually perform vertex initialization when creating a sample instance, such as assigning values to query databases.
First, we can use the Sample constructor to generate an instance and write it as follows:
Sample sample = new Sample (parameter );
However, if Initialization is not as simple as assigning values when creating a sample instance, it may be a long piece of code. If it is also written to the constructor, then your code is ugly (Refactor reorganization is required ).
The reason is that the Code is ugly. Beginners may not feel this way. We will analyze the following. If the initialization work is a long piece of code, it means a lot of work to be done and a lot of work is loaded into a method, it is very dangerous to put a lot of eggs in one basket. This is also based on Java's object-oriented principle. The object-oriented Encapsulation (Encapsulation) and dispatch (Delegation) tell us that, assign long codes as much as possible to "cut" each segment, and then "encapsulate" each segment (reducing the coupling and association between segments). In this way, risks are dispersed, if you need to modify each segment in the future, nothing will happen again.
In this example, we need to separate the instance creation and use, that is, separate the large amount of initialization work required to create an instance from the constructor of the Sample.
In this case, we need the Factory mode to generate the object, and we cannot use the simple new Sample (parameter) above ). In addition, if the Sample has an inheritance such as MySample, We need to abstract the Sample into an interface according to the interface-oriented programming. sample is an interface with two subclasses: MySample and HisSample. we want to instantiate them as follows:
Sample mysample = new MySample ();
Sample hissample = new HisSample ();
As the project goes deeper, the Sample may "give birth to many sons", so we need to instantiate these sons one by one. Even worse, we may need to modify the previous Code: add an instance that later gave birth to a son. this is unavoidable in traditional programs.
But if you consciously use the factory model from the very beginning, these troubles will be gone.
Factory method
You will create a factory dedicated to producing Sample instances:
Public class Factory {
Public static Sample creator (int which ){
// GetClass generates samples. Generally, dynamic classes can be used to load the loaded classes.
If (which = 1)
Return new SampleA ();
Else if (which = 2)
Return new SampleB ();
}
}
In your program, if you want to instantiate the Sample, use
Sample sampleA = Factory. creator (1 );
In this way, the entire article will not be... the remaining full text>

Design Mode simple factory Mode

A factory is the place where production instances are located. It is simple to name it createInstance () directly (). This method is usually static and includes parameters and return values. For example, if cattle, sheep, horses, and dogs all inherit the animal class, then the return value of createInstance () should be an animal (because the factory is used to produce animals, so the return value should also be an animal), and the parameter should be an animal name (so that the factory knows which animal you want to produce based on the name ). In this way, an animal instance can be generated based on the input animal name. CreateInstance implementation: switch (animal name) case BULL: return new ox (); case goat: return new goat (); case horse: return new horse (); case dog: return new dog ();

Related Article

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.