5. Simple factory Model
Zookeeper
1 The core content of the simple factory model is:
A: independent methods
B: The factory does not. Instead, it uses the switch method to determine which method to use.
C: Understanding: Bring the same girl to the same place, and the girl can choose different dishes.
2. Role of the simple factory mode: Use a simple switch and a method to control which method to use.
3. Simple factory model description
Factory model: the customer class and the factory class are separated.
A consumer needs a product at any time and only requests from the factory.
Consumers can accept new products without modification. The disadvantage is that when the product is modified,
The factory class must also be modified accordingly. For example, how to create and provide data to clients.
It's easy to catch up with MM. McDonald's chicken wings and KFC's chicken wings are what MM loves to eat,
Although the taste is different, whether you take MM to McDonald's or KFC,
Just say "four chicken wings" to the waiter. McDonald's and KFC are factories that produce chicken wings.
First, the base class stores data.
Second, there are many derived classes that store data.
Third, implement interface classes, and use static functions to call various derived classes
4. Simple factory pattern class diagram
5. Code: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vcd4kcjxwiggfsawdupq = "left"> # include
# Include
Usingnamespacestd;
// Factory mode: the customer class and the factory class are separated.
// The consumer needs a product at any time and only requests from the factory.
// The consumer can accept new products without modification. The disadvantage is that when the product is modified,
// Modify the factory class. For example, how to create and provide data to clients.
// Try to catch up with MM. The chicken wings of McDonald's and KFC are what MM loves to eat,
// Although the taste is different, whether you take MM to McDonald's or KFC,
// Just say "four chicken wings" to the waiter. McDonald's and KFC are factories that produce chicken wings.
// First, the base class stores data.
// Second, there are many derived classes. The Derived classes store data.
// The third implementation interface class, using static functions to call various derived classes
// The base class stores data.
ClassOperation
{
Public:
DoublenumberA, numberB; // two numbers
Virtualdouble getResult () // obtain the result
{
Return 0;
}
};
// Operation on storing Derived classes
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;
}
};
// For my understanding, this controller controls the operation to be called by passing tags.
ClassoperFactory
{
Public:
StaticOperation * createOperation (charc)
{
Switch (c)
{
Case '+ ':
ReturnnewaddOperation;
Break;
Case '-':
ReturnnewsubOperation;
Break;
Case '*':
ReturnnewmulOperation;
Case '/':
ReturnnewdivOperation;
Break;
}
}
};
Intmain ()
{
Operation * operator = operFactory: createOperation ('-');
Latency-> numberA = 9;
Accept-> numberB = 99;
Cout < GetResult () <endl;
Cin. get ();
Return 0;
}
The running result is as follows:
-90