Java language Implementation-created design pattern-Abstract Factory mode

Source: Internet
Author: User

First, describe

Abstract Factory mode is further abstracted from the pattern of factory methods, if the factory method pattern is for the creation of a product structure, then the abstract factory pattern is for multiple product structures, and it is used to create multiple different product objects at once.

We are going to create a motorcycle tire and a motorcycle handle, and to create a bicycle tyre and a bike handle, if we use the factory method mode we need four classes: Create a factory class for motorcycle tires, create a factory class of motorcycle handles, create a factory class of bicycle tires, and create a factory class of bicycle handles, But if we use the abstract factory approach, we need to create two factory classes: a factory class for creating motorcycle tires and handles, a factory class for creating bicycle tires and handles, and a further abstraction of the factory approach, all of which are grouped into one category and all bicycle products. If you also want to create a motorcycle seat and a bike seat, then we just have to add a car seat in the motorcycle and bicycle class.

Abstract Factory mode is suitable for the creation of multiple structures of different products, the use of abstract factory class is responsible for defining the interface to create objects, this series of objects are created by the implementation of the abstract factory specific factory class to complete, the abstract factory model is mainly composed of 4 parts: Abstract Factory class, the implementation of abstract factory class specific factory class, Abstract classes and concrete classes that implement abstract classes.


Ii. advantages and disadvantages of the abstract factory model

Advantage: In the abstract method pattern, the client is no longer responsible for the creation of the object, but the task to the specific factory class, thereby reducing the burden on the client, but also clear the responsibilities of each class. In this mode, multiple different parts of the same product are placed in a factory class, so that the client calls become very simple, and if you want to modify or add these parts simply modify the corresponding factory class and implement the specific subclass of the factory class.

Cons: Each time a new part is added, the design of the abstract factory class needs to be modified, and all the specific factory classes that implement this abstract factory class need to be modified, and additional coding is required to increase the amount of code. If the number of products created in each abstract factory class is very large, it can be complicated when the client assembles objects created by each specific factory, resulting in a very large client code.

Third, the source code

General: For the payroll system code that was written in the previous blog post using the factory method model, I used the abstract factory model, and we not only calculate the salary but also the social security, that is, there is an abstract factory class, there are two abstract methods, the calculation of salary and social security, Then there are two branch class factory classes used to create their own company's pay and social security objects.

1. Abstract Factory class

Package tong.day5_1.abstractfactory;/** * Abstract Factory interface, in this interface defines a two abstract methods, respectively, to create a payroll class object and Social Security class object, by implementing the specific class of the interface override the method, create their own class object * @ Author Tong * */public interface Abstractfactory {//abstract method for creating objects of various classes public abstract Salary createsalary ();p ublic Abstract Insurance createinsurance ();}
2. Specific Factory class

Package Tong.day5_1.abstractfactory;public class Guangdongfactory implements Abstractfactory {@Overridepublic Salary Createsalary () {return new guangdongsalary ();} @Overridepublic Insurance createinsurance () {return new guangdonginsurance ();}}
Package tong.day5_1.abstractfactory;/** * The specific factory class implements all methods in the abstract factory and returns the salary object and social security object of the class * @author Administrator * */public class Zhejiangfactory implements Abstractfactory {@Overridepublic Salary createsalary () {return new zhejiangsalary ();} @Overridepublic Insurance createinsurance () {return new zhejianginsurance ();}}
3, abstract salary category

Package tong.day5_1.abstractfactory;/** * Defines a payroll interface that implements this interface for all company-specific payroll calculations and overrides the method of calculating Payroll computesalary () * @author Tong * */ Public interface Salary {public void computesalary ();}
4, two company's specific salary category

Package tong.day5_1.abstractfactory;/** * Guangdong Branch Payroll calculation class implements the salary interface and overrides the method of calculating payroll in the interface Computesalary () * @author Tong * */ public class Guangdongsalary implements Salary {@Overridepublic void Computesalary () {System.out.println ("Guangdong branch payroll Calculation");}}
Package tong.day5_1.abstractfactory;/** * Zhejiang Branch the Payroll calculation class implements the salary interface and overrides the method of calculating payroll in the interface Computesalary () * @author Tong * */ public class Zhejiangsalary implements Salary {@Overridepublic void Computesalary () {System.out.println ("Zhejiang branch payroll calculation");}}
5, abstract Social Security category

Package tong.day5_1.abstractfactory;/** * Abstract Social Security class * @author Tong * */public interface Insurance {public abstract void Comput Einsurance ();}
6, two subsidiaries of the specific Social Security category

Package Tong.day5_1.abstractfactory;public class Guangdonginsurance implements Insurance {@Overridepublic void Computeinsurance () {System.out.println ("Guangdong branch Social Insurance Calculation");}}
Package Tong.day5_1.abstractfactory;public class Zhejianginsurance implements Insurance {@Overridepublic void Computeinsurance () {System.out.println ("Zhejiang branch Social Insurance Calculation");}}
7. Client Calls

Package Tong.day5_1.abstractfactory;public class Client {public static void main (string[] args) {//Create abstract factory class, use Polymorphic, First, Zhejiang branch of the factory class object Abstractfactory factory = new Zhejiangfactory ();//use of Zhejiang Branch Factory class to create the company's salary and social security objects salary salary = Factory.createsalary (); Insurance Insurance = Factory.createinsurance ();// The specific methods of calculating salary and calculating social security were invoked using the created salary and Social Security objects salary.computesalary (); Insurance.computeinsurance (); System.out.println ("----------------");//create Abstract factory class, use polymorphism, create factory class object of Guangdong Branch factory = new Guangdongfactory ();// Use the Guangdong Branch factory class to create the company's salary and social security objects salary = Factory.createsalary (); insurance = factory.createinsurance ();// Use the created pay and social security objects to invoke the specific method of calculating salary and calculating Social Security salary.computesalary (); Insurance.computeinsurance ();}}

iii. Results of Operation












Java language Implementation-created design pattern-Abstract Factory mode

Related Article

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.