Java language Implementation-creation mode-Simple Factory mode

Source: Internet
Author: User

First, describe

The simple factory model, also known as the static factory method pattern, is the simplest of all factory patterns, defining a specific factory class to be responsible for creating objects of all classes and initializing the created objects, which consist mainly of 3 parts: factory classes, abstract classes, concrete classes that implement abstract classes, The factory class is then called by the client to create the appropriate object.

Note: The simple factory model does not belong to the Gof summary of the 23 design patterns, it is equivalent to beginners Java HelloWorld case, to help beginners understand the design pattern, the growth of confidence in a design pattern.


Ii. advantages and disadvantages of the simple factory model

Pros: Compared to creating objects directly on the client and initializing the object's values, the Simple Factory mode gives the factory class the creation of objects and initialization tasks, and the client is only responsible for invoking the objects, thus clarifying the responsibilities of each class.

Disadvantage: Only use a factory class to create all the characteristics of all classes, when the type of specific products continue to increase, the way to create a change in the mode of the factory needs to be constantly changing, increase the corresponding judgment, increase the corresponding class of the creation of logic, resulting in factory class code more and more large, not conducive to the maintenance of the later. In addition, the simple factory pattern uses static methods to create objects, so that the static method of creating the object cannot be inherited, and can only become more and more bloated in this class.

Summary: Although the simple factory model separates the creation and initialization of objects from the client, it implements the responsibility allocation strategy, but all object creation and initialization work is concentrated in a specific factory class, if the object to create a wide variety of objects, this factory class will be very large, not conducive to post-maintenance.


Third, the source code

Package tong.day4_30.introduce;/** * 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 ();}

Package tong.day4_30.introduce;/** * 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.introduce;/** * Hebei Company's payroll calculation class implements the salary interface and overrides the method of calculating payroll in the interface Computesalary () * @author Tong * */public Class Hebeisalary implements salary{@Overridepublic void Computesalary () {System.out.println ("Hebei branch payroll Calculation");}}

Package tong.day4_30.introduce;/** * 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") ;}}

Package tong.day4_30.introduce;/** * The Simple factory class is responsible for the creation of all objects and the initialization of objects, where the reflection mechanism in Java is used to create objects of classes based on the parameters passed in * @author Tong * */public Class Simplefactory {//Creates an object from a parameter, which is a static method that can call public static Salary Createsalary (String name) directly using the class name {class<salary> Clazz = null; Salary Salary = null;try {//Use the reflection mechanism to get the object of the class corresponding to this parameter Clazz = (class<salary>) class.forname (name);//Use such objects to create the corresponding pay class object, Here, the parent pointer is used to point to the subclass object, and the polymorphic Salary = (Salary) clazz.newinstance () is implemented;} catch (ClassNotFoundException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} catch (Instantiationexception | Illegalaccessexception e) {e.printstacktrace ();} return salary;}}

Package tong.day4_30.introduce;/** * The method of creating payroll on the client call factory class, and output the result; * If you want to create more company pay, create a corresponding class and create the appropriate object in the client object by passing in the corresponding parameter. * @author Tong * */public class Client {public static void main (string[] args) {//Create Client object, call the Computesalary () method in this object to pass in to the type parameter Number new Client (). Computesalary ("Tong.day4_30.introduce. Hebeisalary "); new Client (). Computesalary (" Tong.day4_30.introduce. Zhejiangsalary "); new Client (). Computesalary (" Tong.day4_30.introduce. Guangdongsalary ");} /** * Call the Factory class's create payroll method based on the parameters passed in to create a different payroll object * @param name */public void computesalary (String name) {//Use polymorphism here, depending on the parameters passed in, Generate Zhejiangsalary or Hebeisalary object Salary salary = simplefactory.createsalary (name);// A method that dynamically invokes a concrete class object using the Generated object Salary.computesalary ();}}

iv. Results of Operation


Java language Implementation-creation mode-Simple 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.