Factory model of Design Pattern

Source: Internet
Author: User

1. Simple Factory Pattern (Simple Factory Pattern) is an innovative class model, also called Static FactoryMethod Pattern ), A dedicated class is used to create instances of other classes. The created instances usually share the same parent simple factory mode. The core idea is: there is a dedicated category that is insufficient to create instances: Because the factory itself includes all the billing methods, the mall may change the discount quota and Rebate quota on a regular basis, every time you maintain or expand the billing method, you have to change this factory, so that the code needs to be re-compiled and deployed. The troublesome code is as follows: [csharp] // class CashFactory {public static CashSupercreateCashAccept (string type) {CashSuper cs = null; switch (type) {case "normal charge": cs = new CashNorm Al (); break; case "300 to 100": CashReturn cr1 = newCashReturn ("300", "100"); cs = cr1; break; case "off ": cashRebate cr2 = newCashRebate ("0.8"); cs = cr2; break;} return cs;} 2. The factory method mode defines an interface for creating objects, let the subclass decide which class to instantiate. The factory method delays the instantiation of a class to its subclass. The Code is as follows: [csharp] // interfaceifacloud {leifeng createleifeng ();} // classUndergraduateFactory: ifacloud {public leifeng createleifeng () {return new Undergraduate () ;}// community Volunteer factory classVolunteerFactory: ifacloud {public leifeng createleifeng () {return new Volunteer () ;}} factory method mode: advantages of creating a factory for each object: when implementing the factory method mode, the client needs to decide which factory to instantiate to implement the computing class, and select whether the problem exists, that is, the factory method moves the internal logic judgment of the simple factory to the client code. If you need to add a function, it is the factory class, but now it is modifying the client. 3. the abstract factory provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes. The Code is as follows: [csharp] // ifacloud interface ifacloud {Iuser createUser ();} // SQLServerFactory class to implement the ifacloud interface and instantiate SQLServerUser class SQLServerFactory: ifacloud {public Iuser createUser () {return new SQLServerUser () ;}// AccessFactory class to implement the IFactory interface. instantiate AccessUser class AccessFactory: ifacfactory {public Iuser createUser () {return new AccessUser ();}} <span style = "font-family: KaiTi_GB2312;"> </span> Like factory method: ifacloud is an abstract factory interface, which should include the Abstract METHODS created by all products. Advantages: 1. Easy to switch product series 2, which separates the process of creating an instance from the client. The client uses their abstract interfaces to manipulate the instance, the specific Class Name of the product is not separated by the specific factory implementation, and will not appear in the client code.

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.