Big talk design model-factory method model (study notes)

Source: Internet
Author: User

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.



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.