- Why the abstract factory model?
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.
- 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.
- Principle: four components: abstract factory class, specific factory class, abstract class and specific class.
- Code to implement the salary program:
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/>}
- 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.
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.