Java design pattern-Simple Factory mode (simplefactory)

Source: Internet
Author: User

First, what is the Simple Factory mode

A simple factory pattern is a class-created pattern, also called a static factory method pattern. By specifically defining a class to be responsible for creating instances of other classes, the instances that are created typically have a common parent class.

Ii. roles contained in the model and their responsibilities

1. Factory (Creator) role the core of the simple factory model, which is responsible for implementing the internal logic of creating all instances. The factory class can be called directly by the outside world to create the desired product object.
2. Abstract (Product) The parent class of all objects created by the simple Factory mode, which is responsible for describing common interfaces common to all instances.
3. Specific products (concrete product) role Simple Factory mode create specific instance objects

Third, specific examples

There are now two classes, apple and banana, each with a Get method:

Apple.java

Package Cc.dewdrop;public class Apple{public void Get () {System.out.println ("Capture Apple");}}

Banana.java

Package Cc.dewdrop;public class Banana{public void Get () {System.out.println ("collect Bananas");}}

Instantiate and invoke individual methods in the Main method individually:

Package Cc.dewdrop;public class Simplefactory {public static void main (string[] args) {Apple Apple = new Apple (); Banana Banana = new Banana (); Apple.get (); Banana.get ();}    }

Found that two classes have a common get method, then we can abstract an interface, and then Apple and banana two classes implement this interface:

Fruit.java

Package Cc.dewdrop;public interface Fruit {public void Get ();}

Apple.java

Package Cc.dewdrop;public class Apple implements Fruit{public void Get () {System.out.println ("Capture Apple");}}

Banana.java

Package Cc.dewdrop;public class Banana implements Fruit{public void Get () {System.out.println ("collect Bananas");}}

In the main method, you can create an object in the following way:

public class Simplefactory {public static void main (string[] args) {Fruit Apple = new Apple (); Fruit banana = new banana (); Apple.get (); Banana.get ();    }}

Depending on the characteristics of the simple factory model, you now need to create a class to create the object of the class:

Fruitfactory.java

Package Cc.dewdrop;public class Fruitfactory {/** * get apple instance */public Fruit getapple () {return new Apple ()}/** * get Banana instance */Public Fruit Getbanana () {return new Banana ();}}

In the main method, the call can be made in the following way:

public class Simplefactory {public static void main (string[] args) {Fruit Apple = new Fruitfactory (). Getapple (); Fruit banana = new Fruitfactory (). Getbanana (); Apple.get (); Banana.get ();    }}

Further, we can modify the factory class and use static methods to create objects:

Fruitfactory.java

Package Cc.dewdrop;public class Fruitfactory {/** * get apple instance */public static Fruit Getapple () {return new Apple ();}/ * * Get Banana instance */public static Fruit Getbanana () {return new Banana ();}}

You can use the class name directly in the main method to invoke:

public class Simplefactory {public static void main (string[] args) {Fruit Apple = fruitfactory.getapple (); Fruit banana = Fruitfactory.getbanana (); Apple.get (); Banana.get ();}    }

To further improve the factory class, there is a common way to create objects for all products:

Fruitfactory.java

Package Cc.dewdrop;public class Fruitfactory {public static Fruit Getfruit (String type) throws Instantiationexception, Il legalaccessexception {if (Type.equalsignorecase ("Apple")) {return Apple.class.newInstance ();} else if ( Type.equalsignorecase ("banana")) {return Banana.class.newInstance ();} else {System.out.println ("Class not Found");} return null;}}

Called in the main method using the following method:

public class Simplefactory {public static void main (string[] args) {Fruit Apple = fruitfactory.getfruit ("Apple"); Fruit banana = fruitfactory.getfruit ("banana"); Apple.get (); Banana.get ();}    }

This makes it much easier to invoke the factory class to create objects, but one drawback is that you must modify the factory class if you add a new class. At this point the factory class can make the following modifications:

Fruitfactory.java

Package Cc.dewdrop;public class Fruitfactory {public static Fruit Getfruit (String type) throws Instantiationexception, Illegalaccessexception, ClassNotFoundException {return (Fruit) class.forname (type). newinstance ();}}

It is important to note that the full path of the class (including the package name) needs to be used when calling:

public class Simplefactory {public static void main (string[] args) {Fruit Apple = fruitfactory.getfruit ("Cc.dewdrop.Apple "); Fruit banana = fruitfactory.getfruit ("Cc.dewdrop.Banana"); Apple.get (); Banana.get ();}    }
Iv. advantages and disadvantages of the simple factory model

In this mode, the factory class is the key to the entire pattern. It contains the necessary judgment logic to determine which specific class of objects should be created based on the information given by the outside world. Users can create the required instances directly from the factory class, without having to know how they are created and how they are organized. facilitates the optimization of the entire software architecture
It is not difficult to find that the shortcomings of the simple factory model are also reflected in its factory class, because the factory class centralizes all instances of the creation logic, so "high cohesion" does not do well. In addition, when the specific product classes in the system are increasing, it may be necessary to require the factory class to make corresponding changes, extensibility is not very good.

Java design mode-Simple Factory mode (simplefactory)

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.