Java language Implementation-created design pattern-factory method mode

Source: Internet
Author: User

First, describe

Based on the problem of placing all class creation and initialization in a factory class in a simple factory pattern, we introduced the factory method pattern, the first of the 23 design patterns summarized by GOF, which splits a factory class into specific factory classes, Each specific factory class is responsible for the creation of the corresponding class object.

In the factory method pattern, the abstract factory class is responsible for defining the interface that creates the object, and the creation of the object is done by the specific factory class that implements the abstract factory, which consists of four parts: Abstract factory class, concrete factory class for implementing abstract factory class, abstract class, concrete class for implementing abstract class.


Ii. advantages and disadvantages of the factory method model

Advantage: In factory method mode, the task of creating objects is the responsibility of the specific factory class, a specific class corresponds to a specific factory class, and then the abstract factory class defines all the concrete factory classes to create the object's uniform interface, the client is only responsible for invoking the specific class to create the corresponding object.

If there is a new product to join, then only need to add a specific class and create the specific class of the factory class, do not need to modify the original code, the same class of code will not become larger, more conducive to later maintenance, enhanced the system's scalability.

Cons: Adding two classes per new product requires additional coding and increased effort.

Third, the source code

1. Abstract Method Class:

Package tong.day4_30.factorymethod;/** * Abstract Factory interface, which defines a createsalary () method in this interface, which is overridden by the specific class implementing the interface, creating the object of its own class individually * @author Tong * */public Interface Factory {///This is a common abstract method for creating objects of various classes public abstract Salary createsalary ();}

2. Concrete Method Class

Package tong.day4_30.factorymethod;/** * Specific factory method for creating objects of a specific class * @author Tong * */public class Guangdongsalaryfactory Implem Ents Factory {@Overridepublic Salary createsalary () {//Return object of Guangdongsalary class return new Guangdongsalary ();}

Package tong.day4_30.factorymethod;/** * Specific factory method for creating objects of a specific class * @author Tong * */public class Zhejiangsalaryfactory Impleme NTS Factory {@Overridepublic Salary createsalary () {//Return object of Zhejiangsalary class return new Zhejiangsalary ();}

3. Abstract class

Package tong.day4_30.factorymethod;/** * 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. Concrete Class

Package tong.day4_30.factorymethod;/** * Guangdong Branch Payroll calculation class implements the salary interface and overrides the method of calculating payroll in the interface Computesalary () * @author Administrator * */public class Guangdongsalary implements Salary {@Overridepublic void computesalary () { SYSTEM.OUT.PRINTLN ("Guangdong branch payroll Calculation");}}

Package tong.day4_30.factorymethod;/** * Zhejiang Branch the Payroll calculation class implements the salary interface and overrides the method of calculating payroll in the interface Computesalary () * @author Administrator * */public class Zhejiangsalary implements Salary {@Overridepublic void computesalary () { SYSTEM.OUT.PRINTLN ("Zhejiang branch payroll calculation");}}

5. Client calls

Package tong.day4_30.factorymethod;/** * Calls the factory method on the client to create each specific factory class object, uses the specific factory class object to create different payroll objects, and the Payroll object calls Computesalary () to calculate the salary; If you also want to create more company salaries, create a corresponding payroll class, and then create a specific factory class for the corresponding payroll calculation. * @author Tong * */public class Client {public static void main (string[] args) {//Create factory object, where polymorphism is used to create different specific factory class objects factory F Actory = new Guangdongsalaryfactory ();//Use Specific factory class objects to create different payroll objects to calculate payroll salary salary = factory.createsalary (); Salary.computesalary (); factory = new Zhejiangsalaryfactory (); salary = Factory.createsalary (); Salary.computesalary () ;}}


Java language Implementation-created design pattern-factory method mode

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.