Factory pattern of design pattern, abstract factory pattern, Singleton pattern (C ++ code)

Source: Internet
Author: User

The importance of the design pattern is unnecessary. Various design patterns are widely used in actual projects.

I. Simple factory mode and factory Mode

I know a little about UML. When I look at this class diagram, I know what the factory model is for. To put it bluntly, it is to provide an abstract base class interface, so that you do not need to remember the name of the new class to be generated.

See the following section.Code:

 
# Include <iostream> # include <string> # include <vector> using namespace STD; Class product {public: product (){}~ Product () {};}; class producta: Public Product {public: producta () {cout <"producta" <Endl ;}~ Producta () {}}; class factory {public: Factory (){}~ Factory () {} producta * createproducta () {return New producta () ;}}; int main () {factory F; // producta * pA = f in simple factory mode. createproducta (); System ("pause"); Return 0 ;}

Only one product is listed here. You can expand multiple products on your own, such as bananas, apples, and oranges. When a factory needs to be notified, the factory will call functions such as banana production, apple production, and orange production, and the customer does not need to know the specific process of producing these fruits. Just sit and eat.

The class diagram of the factory mode is as follows. The specific code is saved ..

Ii. Abstract Factory Model

The factory model mentioned above is not abstract enough, because only products are abstract and factories are not abstract. The above is a factory that produces all the products that are needed, suppose HTC manufacturers make HTC phones, HTC batteries, and Nokia phones... Do you think this is strange? At this time, we need to abstract the factory model to solve this problem. HTC should produce products of HTC and Apple products of apple series, which is convenient to manage.

Let's look at the code example:

# Include <iostream> # include <string> # include <vector> using namespace STD; Class abstractpruduct {public: abstractpruduct (){}~ Abstractpruduct () {}virtual void print () = 0 ;}; class producta: Public abstractpruduct {public: producta () {cout <"producta" <Endl ;}~ Producta () {} void print () {cout <"I'm producta" <Endl ;}}; class productb: Public abstractpruduct {public: productb () {cout <"productb" <Endl ;}~ Productb () {} void print () {cout <"I'm productb" <Endl ;}}; class abstractfactory {public: abstractfactory (){}~ Abstractfactory () {}}; classfactorya: Public abstractfactory {public: factorya () {cout <"factorya" <Endl ;}~ Factorya () {}producta * createproduct () {return New producta () ;}}; class factoryb: Public abstractfactory {public: factoryb () {cout <"factoryb" <Endl ;}~ Factoryb () {} productb * createproduct () {return New productb () ;}}; int main () {factoryb fa; // Abstract Factory mode abstractpruduct * ap = fa. createproduct (); ap-> Print (); System ("pause"); Return 0 ;}

In actual work, you need to configure an xml configuration file, so that you can achieve a good interaction with the customer, the customer does not need to know the specific logic implementation, just need to configure the text file, you can achieve the expected results.


Iii. Singleton Mode

The Singleton mode is very common. For example, the task manager on our computer uses the singleton mode.

When we need to share data or synchronize data, we can consider using the singleton mode.

The Code is as follows:

# Include <iostream> # include <string> # include <vector> using namespace STD; Class Singleton {public :~ Singleton () {} static Singleton * instance (); Private: Singleton () {cout <"Singleton initialization" <Endl;} static Singleton * st ;}; singleton * singleton: St = NULL; Singleton * singleton: instance () {If (ST = NULL) {ST = new Singleton ();} return st ;} int main () {Singleton * Ss = singleton: instance (); // Singleton * SSS = singleton: instance (); // The above pointer system ("pause"); Return 0;} Will be used here ;}

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.