4. Factory method mode

Source: Internet
Author: User

4. Factory method mode
Zookeeper

1 The core content of the factory method model is:

A: each method is represented by an independent class.

B: Create an instantiation method through a factory

C: Understanding: bringing the same girl to different places (McDonald's, KFC ....) Go to dinner.

2. Role of the factory method mode: The program accesses different factories to allow the factory to create the desired operation.

3. Detailed description of the factory method mode

Factory method mode: the Core factory category is no longer responsible for the creation of all products,

Instead, the specific work is handed over to the Child class to become an abstract factory role.

Only provides interfaces required for specific factory classes,

But do not touch the details of which product class should be instantiated.

Ask MM to go to McDonald's for a hamburger. Different MM tastes different,

Remember that it is an annoying thing. I usually use the Factory Method mode,

Take the MM to the waiter and say "I want a hamburger". What kind of hamburger do you want,

Let MM talk to the waiter directly.

4. Factory method pattern class diagram

5. Code:

# Include

# Include

Usingnamespacestd;

// Factory method mode: the Core factory category is no longer responsible for the creation of all products,

// Instead, assign the created work to the Child class to become an abstract factory role.

// Only provides the interface that must be implemented by the specific factory class,

// Do not touch the details of which product class should be instantiated.

// Ask MM to go to McDonald's for a hamburger. Different MM tastes different,

// Remember that it is an annoying thing. I usually use the Factory Method mode,

// Take the MM to the waiter and say "I want a hamburger". What kind of hamburger do you want,

// Let MM directly talk to the waiter.

// Operation class

ClassOperation

{

Public:

DoublenumberA, numberB;

Virtualdouble getResult ()//

{

Return 0;

}

};

// Add operation

ClassaddOperation: publicOperation

{

DoublegetResult ()

{

ReturnnumberA + numberB;

}

};

// Subtraction

ClasssubOperation: publicOperation

{

DoublegetResult ()

{

ReturnnumberA-numberB;

}

};

// Multiplication operation

ClassmulOperation: publicOperation

{

DoublegetResult ()

{

ReturnnumberA * numberB;

}

};

// Except operations

ClassdivOperation: publicOperation

{

DoublegetResult ()

{

ReturnnumberA/numberB;

}

};

// Factory

ClassIFactory

{

Public:

VirtualOperation * createOperation () = 0;

};

// Add Factory

ClassAddFactory: publicifacloud

{

Public:

StaticOperation * createOperation ()

{

ReturnnewaddOperation ();

}

};

// Reduce the factory

ClassSubFactory: publicifacloud

{

Public:

StaticOperation * createOperation ()

{

ReturnnewsubOperation ();

}

};

// Take factory

ClassMulFactory: publicifacloud

{

Public:

StaticOperation * createOperation ()

{

ReturnnewmulOperation ();

}

};

// Except operations

ClassDivFactory: publicifacloud

{

Public:

StaticOperation * createOperation ()

{

ReturnnewdivOperation ();

}

};

Intmain ()

{

Operation * operator = MulFactory: createOperation ();

Latency-> numberA = 9;

Accept-> numberB = 99;

Cout < GetResult () <endl;

Cin. get ();

Return 0;

}

The running result is as follows:

891

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.