Author: Yi Yu Tian (http://blog.csdn.net/dylgsy ). This article can be ressed casually, but please keep this information
What is the design pattern? In fact, the design pattern is some code writing methods, mainly for Object-Oriented. The design pattern is usually used in the design stage. The analysis stage has layered the system, and then some classes are extracted for each layer. At this time, how to organize and use these classes is the question of our design pattern.
For our programmers, a thousand words is nothing more than a simple code. Let's take a look at the code to implement some design patterns:
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////////////////
Adapter mode:
# Include <stdio. h>
// Existing class, to be adapted
Class xcircle
{
Public:
Xcircle ()
{
Printf ("I am xcircle/N ");
}
Void displayit ()
{
Printf ("show a circle/N ");
}
};
Class cshape
{
Public:
Virtual void display () = 0;
};
// Because xcircle has implemented the circle display operation, we do not need to implement it again.
Class ccircle: Public cshape
{
Public:
Ccircle ()
{
Printf ("I am ccircle/N ");
}
Virtual void display ()
{
Printf ("ccircle:/N ");
_ XC. displayit ();
}
PRIVATE:
Xcircle _ XC;
};
Int main ()
{
Ccircle circle;
Circle. Display ();
Return 0;
}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////////////////
Builder mode:
/*************************************** **************************************** ***************************************/
* Builder mode:
* We can understand Builder as a rice cooker and put the builder in rice and water. After builder build, we can take out the fragrant rice.
* Intention: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
* Applicability: when creating complex objects, algorithms should be independent of the components of the objects and their assembly methods.
* When the constructor must allow different representations of the constructed object.
/*************************************** **************************************** ***************************************/
# Include <stdio. h>
# Include <string>
// Automobile. Do you want to build a car? Is the project big? Therefore, the builder mode is used to separate object construction.
Class ccar
{
Public:
Void what ()
{
Printf ("% s", szwhat );
}
Void drive ()
{
Printf ("driving! /N ");
}
Char szwhat [2, 100];
};
// Car builder
Class ccarbuilder
{
Public:
Virtual void buildcar () = 0;
Virtual ccar * getresult () = 0;
Protected:
Ccarbuider ();
};
Class cbenchbuilder: Public ccarbuilder
{
Public:
Cbenchbuilder ()
{
_ Car = NULL;
}
Virtual void buildcar ()
{
If (_ car = NULL)
_ Car = new ccar;
Strcpy (_ car-> szwhat, "I'm a Mercedes-Benz! ");
};
Virtual ccar * getresult () {return _ car ;}
PRIVATE:
Ccar * _ car;
};
/*************************************** **************************************** ***************************************/
* If I want to drive a different car, just upload a different builder.
/*************************************** **************************************** ***************************************/
Void drive (ccarbuilder & builder)
{
Ccar * car;
Builder. buildcar ();
Car = builder. getresult ();
Car-> what ();
Car-> drive ();
}
Void main ()
{
Cbenchbuilder builder;
Drive (builder );
}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////////////////////////////
Factory mode:
# Include <stdio. h>
Class cfruit
{
Public:
Virtual void what () = 0;
};
Class capple: Public cfruit
{
Public:
Virtual void what ()
{
Printf ("I am an apple/N ");
}
};
Class cpear: Public cfruit
{
Public:
Virtual void what ()
{
Printf ("I Am a Sydney/N ");
}
};
Class cbanana: Public cfruit
{
Public:
Virtual void what ()
{
Printf ("I Am a banana/N ");
}
};
Enum efruittype {Apple = 1, pear, banana };
Class cfruitfactory
{
Public:
Cfruit * getfruitinstance (efruittype)
{
Printf ("the factory is producing fruit.../N ");
Switch (type)
{
Case Apple:
Return new capple ();
Case pear:
Return new cpear ();
Case banana:
Return new cbanana ();
Default:
Return NULL;
}
}
};
Int main ()
{
Cfruitfactory factory;
Cfruit * fruit;
Printf ("what kind of fruit do you want to eat? /N ");
Printf ("1 Apple, 2 Sydney, 3 bananas/N ");
Int ntype = 0;
Scanf ("% d", & ntype );
Fruit = factory. getfruitinstance (efruittype (ntype ));
If (fruit = NULL)
{
Printf ("No such fruit. ");
Return 0;
}
Fruit-> what ();
Return 0;
}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////////////////////////////
Observer mode:
/*************************************** **************************************** ***************************************/
* Observer Mode
* [Description]
* In a document processing system, the developer defines an officedoc document class, which defines the attributes of the document and the corresponding methods for processing the document. When the content or
* When the status changes, the corresponding docexplorer objects of the officedoc class must update their own status. An officedoc object can be associated with a group
* Docexplorer object. When the content or status of the officedoc object changes, all associated docexplorer objects will be notified. This application is called
* Indicates the observer mode.
*
* For example, the doc and view architectures of MFC are all based on this observer mode.
/*************************************** **************************************** ***************************************/
/*************************************** **************************************** ***************************************/
* This Code also uses a label method to identify objects at runtime.
* Rtti can only recognize types at runtime. It is impossible to recognize objects.
/*************************************** **************************************** ***************************************/
# Include <stdio. h>
# Include <typeinfo>
// Maximum number of docexplorer objects associated with officedoc
Const int obs_max = 20;
Class cofficedoc;
// Follow the officedoc Document Object Class
Class cdocexplorer
{
Public:
Cdocexplorer (cofficedoc * DOC );
Void updateself (cofficedoc * DOC );
// Add an identifier here for Object Recognition
Int nexplorerid;
};
Class cofficedoc
{
Public:
Cofficedoc ()
{
Index = 0;
}
Void attach (cdocexplorer * E)
{
If (index> = obs_max | E = NULL)
{
Return;
}
Mybs [Index] = E;
Index ++;
E-> nexplorerid = index;
}
Void detach (cdocexplorer * E)
{
For (INT I = 0; I <index; I ++)
{
If (mybs [I] = E)
{
// Exchange the observer object at the current position with the last object and subtract the index value
If (I <= index-2)
Mybs [I] = mybs [index-1];
Mybs [index-1] = NULL;
Index --;
Break;
}
}
}
// Modify the data and notify the observer to update the data
Void modifydata (INT data)
{
Printf ("changing the official document data to: % d/N", data );
M_ndata = data;
Notifyobs ();
}
PRIVATE:
Cdocexplorer * mybs [obs_max]; // an array of class object pointers of the observer
Int index;
// Notify all observer objects to change their statuses
Void required yobs ()
{
For (INT I = 0; I <index; I ++)
{
Mybs [I]-> updateself (this );
}
}
Public:
// Document data
Int m_ndata;
};
Cdocexplorer: cdocexplorer (cofficedoc * DOC)
{
Doc-> attach (this );
}
Void cdocexplorer: updateself (cofficedoc * DOC)
{
// Open the comments of the following two rows and you can see the usage of typeid and type_info.
// You will find that typeid can only recognize types
// Const type_info & t = typeid (* DOC );
// Printf ("I'm observing % s/n", T. Name ());
Printf ("I am the observer % d,", nexplorerid );
Printf ("update data: % d/N", doc-> m_ndata );
}
Int main ()
{
Cofficedoc officedoc;
Cdocexplorer docexplorer0 (& officedoc );
Cdocexplorer docexplorer1 (& officedoc );
Cdocexplorer docexplorer2 (& officedoc );
Cdocexplorer doc127er3 (& officedoc );
Cdocexplorer docexplorer4 (& officedoc );
// Update document data
Officedoc. modifydata (123 );
Printf ("/N ");
Officedoc. modifydata (567 );
Return 0;
}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////////////////////////////
Singleton mode:
/*************************************** **************************************** **************************************** ***********************/
* [Description]
* Generally, you can configure the application system and save the configuration information in the configuration file. When the application system starts, it first loads the configuration file into the memory,
* The memory configuration information must have only one copy.
* The following Code applies Singleton to ensure that the configure class can only have one instance. In this way, users of the configure class cannot define multiple instances of this class,
* Otherwise, a compilation error occurs.
/*************************************** **************************************** **************************************** ***********************/
# Include "stdio. H"
# Include <assert. h>
Class cconfigfile
{
PRIVATE:
Cconfigfile (){}
Public:
Static cconfigfile * instance ();
Void getconfigdata (Int & data );
Void setconfigdata (INT data );
PRIVATE:
Static cconfigfile * _ instance;
Int _ data;
};
// Note: the initialization format of static variables
Cconfigfile * cconfigfile: _ instance = NULL;
Void cconfigfile: setconfigdata (INT data)
{
_ DATA = data;
}
Void cconfigfile: getconfigdata (Int & Data)
{
Data = _ data;
}
Cconfigfile * cconfigfile: instance ()
{
If (_ instance = NULL)
{
_ Instance = new cconfigfile ();
// Load the configuration file and set the memory configuration information, which is omitted here
}
Return _ instance;
}
Int main (INT argc, char * argv [])
{
Cconfigfile * configfile = NULL;
Configfile = cconfigfile: instance ();
Configfile-> setconfigdata (3 );
Int TMP = 0;
Configfile-> getconfigdata (TMP );
// Obtain the configuration information and perform other work. This parameter is omitted.
Assert (TMP = 3 );
Return 0;
}
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ////////////////////////////////////