Java simple factory mode, java factory Mode
Head First design pattern Learning
Category
- Simple Factory)
- Factory Method)
- Abstract Factory)
Introduction
A simple factory is not a design pattern, but rather a programming habit. It is often called a simple factory pattern because it is often used, you can regard it as a special case of the factory model, but it cannot be ignored because it is not a real design model.
An abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
Each factory class can only create one instance of a specific product class.
Multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
You can create multiple product instances for each specific factory class.
The factory method mode has only one abstract product class, while the abstract factory mode has multiple.
The factory method mode can only create one instance for a specific product type, while the abstract factory mode can create multiple instances.
Simple factory class diagram
Simple factory instance
Product
package com.Observer.model;public class food {public food(){}}
Product
Package com. Observer. model; public class ChickenPot extends food {public ChickenPot () {System. out. println ("Yellow Chicken ");}}
Package com. Observer. model; public class Noodles extends food {public Noodles () {System. out. println ("casserole instant Noodles ");}}
Factory
Package com. observer. model; public class SimpleFoodFactory {public food create (String type) {if (type. equals ("Yellow pheasant") {return new ChickenPot ();} else if (type. equals ("casserole instant Noodles") {return new Noodles () ;}return null ;}}
Test class
Package com. observer. model; public class Test {public static void main (String [] args) {SimpleFoodFactory factory = new SimpleFoodFactory (); food = factory. create ("casserole instant noodles"); food food1 = factory. create ("Yellow pheasant ");}}
Output result:
Braised Chicken in casserole
Advantages of a simple factory:
If you want to implement changes in the future, you only need to modify the factory class.
Factory mode instance:
The hotel opened in a simple factory is too popular, attracting two agents. Because our code is easy to use, they want to use our code to ensure quality. In this way, we can use the factory model.
Product Type:
package com.Observer.model;public abstract class food {public food(){}}
Product
Package com. Observer. model; public class ChickenPot extends food {public ChickenPot (String name) {System. out. println (name + ": ");}}
Package com. Observer. model; public class Noodles extends food {public Noodles (String name) {System. out. println (name + ": casserole instant noodle ");}}
Factory Interface
package com.Observer.model;public interface factoryFood {food create(String type);}
Factory implementation class
Package com. observer. model;/*** Tongzhou store * @ author Administrator **/public class TongFood implements factoryFood {@ Overridepublic food create (String type) {if (type. equals ("Yellow pheasant") {return new ChickenPot ("Tongzhou branch");} else if (type. equals ("casserole instant Noodles") {return new Noodles ("Tongzhou branch");} return null ;}}
Package com. observer. model;/*** Changping store * @ author Administrator **/public class ChangFood implements factoryFood {@ Overridepublic food create (String type) {if (type. equals ("Yellow Chicken") {return new ChickenPot ("Changping Branch");} else if (type. equals ("casserole instant Noodles") {return new Noodles ("Changping Branch");} return null ;}}
Test class
Package com. observer. model; public class Test {public static void main (String [] args) {factoryFood factory = new TongFood (); factory. create ("Yellow Chicken"); factoryFood factory1 = new ChangFood (); factory1.create ("casserole instant noodles ");}}
Output result:
Tongzhou branch: Huangpi chicken Changping Branch: casserole instant noodles
Factory method mode:
Defines an interface for object creation, but the subclass determines which class is to be instantiated. The factory method causes the class to postpone the Instantiation to the subclass.
Reference: http://blog.csdn.net/jason0539/article/details/23020989