Design Pattern 2 (abstract factory pattern) (Abstract Factory)

Source: Internet
Author: User
  1. Why the abstract factory model?
  2. In the factory model, a specific factory class is responsible for creating a separate product. If there are two different products, two different factory classes are required, even if the two products have some necessary relationships. Therefore, the abstract factory model should be used for multiple series products. The factory model is a product and a factory, and the second abstract factory model is a series of products and a factory.

  3. Definition: if the client needs to create some product structures that belong to different product categories, the abstract factory mode can be used. In the abstract factory mode, the abstract factory is responsible for creating object interfaces. the creation of these objects is implemented by the specific factory classes of the factory.
  4. Principle: four components: abstract factory class, specific factory class, abstract class and specific class.
  5. Code to implement the salary program:
  6. Salary. CS

    Namespace abstractfactorymethod <br/>{< br/> Public interface salary <br/>{< br/> void computersalary (); <br/>}</P> <p> public class beijingsalary: Salary <br/>{< br/> Public void computersalary () <br/>{< br/> system. console. writeline ("starting to calculate the salaries of Beijing subsidiaries"); <br/>}</P> <p> public class shandongsalary: salary <br/>{< br/> Public void computersalary () <br/>{< br/> system. console. writeline ("starting to calculate the salary of Shandong subsidiary"); <br/>}< br/>} 

    Insurance. CS

    Namespace abstractfactorymethod <br/>{< br/> Public interface insurance <br/>{< br/> void computerinsurance (); <br/>}</P> <p> public class beijinginsurance: Insurance <br/>{< br/> Public void computerinsurance () <br/> {<br/> console. writeline ("start to calculate social insurance for Beijing subsidiaries"); <br/>}</P> <p> public class shandonginsurance: insurance <br/>{< br/> Public void computerinsurance () <br/>{< br/> console. writeline ("start to calculate social insurance for Shandong subsidiaries"); <br/>}</P> <p>} 

    Tax. CS

    Namespace abstractfactorymethod <br/>{< br/> Public interface tax <br/>{< br/> void computertax (); <br/>}</P> <p> public class beijingtax: tax <br/>{< br/> Public void computertax () <br/> {<br/> console. writeline ("Income Tax calculation for Beijing subsidiaries"); <br/>}</P> <p> public class shandongtax: tax <br/>{< br/> Public void computertax () <br/>{< br/> console. writeline ("Income Tax calculation for Shandong subsidiaries"); <br/>}< br/>} 

    Factory. CS

    Namespace abstractfactorymethod <br/>{< br/> interface factory <br/>{< br/> salary createsalary (); <br/> Insurance createinsurance (); <br/> tax createtax (); <br/>}</P> <p> public class beijingfactory: factory <br/>{< br/> public salary createsalary () <br/>{< br/> return New beijingsalary (); <br/>}</P> <p> Public Insurance createinsurance () <br/>{< br/> return New beijinginsurance (); <br/>}</P> <p> Public Tax createtax () <br/>{< br/> return New beijingtax (); <br/>}</P> <p> public class shandongfactory: factory <br/>{< br/> public salary createsalary () <br/>{< br/> return New shandongsalary (); <br/>}</P> <p> Public Insurance createinsurance () <br/> {<br/> return New shandonginsurance (); <br/>}</P> <p> Public Tax createtax () <br/>{< br/> return New shandongtax (); <br/>}< br/>} 

    Programe. CS

    Namespace abstractfactorymethod <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> factory = new beijingfactory (); <br/> salary = factory. createsalary (); <br/> salary. computersalary (); <br/> insurance = factory. createinsurance (); <br/> Insurance. computerinsurance (); <br/> tax = factory. createtax (); <br/> tax. computertax (); </P> <p> factory = new shandongfactory (); <br/> salary = factory. createsalary (); <br/> salary. computersalary (); <br/> Insurance = factory. createinsurance (); <br/> Insurance. computerinsurance (); <br/> tax = factory. createtax (); <br/> tax. computertax (); <br/>}< br/>} 

  7. Advantage: In the abstract factory mode, the client is not responsible for object creation, but the responsibility is handed over to the specific factory class. The client is only responsible for object calling, so as to clarify the responsibilities of each class.
  8. After a series of related products are designed to be a factory class, the client call will be very simple, and if you want to replace this series of products, you only need to replace one factory class.

    Disadvantage: if a new product is added, You need to modify the design of the abstract factory class and modify the specific factory class that implements this abstract class at the same time. You need to write additional code to increase the 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.