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