Simple factory mode (calculator) structure:
Factory method mode (calculator) structure:
The above figure shows the differences between the simple factory mode and the factory method mode.
Instance (): 1. Build the leifeng class as the base class for specific operations in a simple factory
Class LeiFeng
{
Public void Sweep (){
Console. WriteLine ("sweep ");
}
Public void Wash (){
Console. WriteLine ("laundry ");
}
Public void BuyRice (){
Console. WriteLine ("buy rice ");
}
}
// Learns from Lei Feng's college students and inherits from Lei Feng
Class Undergraduate: LeiFeng {
}
// Community volunteers
Class Volunteer: LeiFeng {
}
2. Create a simple factory class
// Simple factory type
Class SimpleFactory
{
Public static LeiFeng CreateLeiFeng (string type ){
LeiFeng result = null;
Switch (type)
{
Case "Lei Feng's college students ":
Result = new Undergraduate ();
Break;
Case "community volunteers ":
Result = new Volunteer ();
Break;
}
Return result;
}
}
3. abstract classes that implement factory Methods and Their Derived factory method classes
// Implement the same function in the factory Method
Interface IFactory {
LeiFeng CreateLeiFeng ();
}
// Learn from Lei Feng College Students' Factory
Class UndergraduateFactory: ifacloud {
Public LeiFeng CreateLeiFeng (){
Return new Undergraduate ();
}
}
// Community volunteer Factory
Class VolunteerFactory: ifacloud {
Public LeiFeng CreateLeiFeng (){
Return new Volunteer ();
}
} 4. Implement the front-end code using simple factory and factory method classes.
Static void Main (string [] args)
{
// Simple factory Mode
LeiFeng studentA = SimpleFactory. CreateLeiFeng (" ");
StudentA. BuyRice ();
StudentA. Sweep ();
StudentA. Wash ();
//
// Factory method mode
IFactory factory = new UndergraduateFactory ();
LeiFeng studentB = factory. CreateLeiFeng ();
StudentB. BuyRice ();
StudentB. Sweep ();
StudentB. Wash ();
Console. Read ();
}
Conclusion: compared with the two methods, there is no difference at the beginning of the period. Instead, it is easier for a simple factory to adopt this model. After careful research, we found that the factory method model, as described in the book, better reflects the principle of open and closed.
The simple factory mode controls the factory to generate different instances through different parameters. The factory method produces different instances by instantiating different factory classes.
Comparison between the two modes in the book: 1. The factory method overcomes the simple factory ViolationOpen and closed PrincipleTo maintain the advantages of the encapsulated object creation process. 2. They all encapsulate the creation of objects in a centralized manner, so that the object can be changed without major changes, reducing the coupling between customers and product objects. The factory method mode is further abstract and promoted by the simple factory mode. Due to the use of polymorphism, the factory method pattern maintains the advantages of the simple pattern and overcomes its shortcomings. However, the disadvantage is that each added product requires a product factory class, which increases the additional development workload.