Previous describes the design pattern principles described below for commonly used design patterns
Create a schema: static factory pattern, factory method pattern, abstract factory pattern, singleton, builder mode
Structural model: bridge mode, the adapter mode, the decorator pattern, proxy mode, combination mode
Behavioral patterns: template method pattern, strategy pattern, observer pattern, the responsibility chain mode, command mode, mode of visitors
Simple factory pattern
Design Principles:
Creating a class to a class of unified management, the process of creating user classes of a class of transparency.
Examples
Fruit entity
public class Fruits {
private String name;
private String shape;
public Fruits(){
setName("水果");
setShape("无");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getShape() {
return shape;
}
public void setShape(String shape) {
this.shape = shape;
}
}
Apple entity
public class Apple extends Fruits { public Apple(){ setName("苹果"); setShape("圆的"); } }
Oranges entity
public class Orange the extends Fruits { public Orange () { the setName ( "orange" ); setShape ( "circle" ); } }
The former does not implement static factory that creates apples and oranges examples
public class FruitsController {
public static void main(String[] args) {
Fruits apple=new Apple();
System.out.println(apple.getName());
Fruits orange=new Orange();
System.out.println(orange.getName());
}
}
Create a static factory
public class FruitsFactory {
public final static int TYPE_APPLE=1;//苹果
public final static int TYPE_ORANGE=2;//橘子
public Fruits getFruit(int type){
if(TYPE_APPLE==type){
return new Apple();
}else if(TYPE_ORANGE==type){
return new Orange();
}
return new Fruits();
}
}
By passing the return type of fruit to create fruit entity - type is an Apple creation, creation of type 2 oranges
Fruits were created using a static factory
public class FruitsController { public static void main (String [] args) { FruitsFactory Factory = new new FruitsFactory ();
// Create Type fruit by fruit Fruits Apple = factory.getFruit (FruitsFactory.TYPE_APPLE); System.out.println (Apple. getName ());
// Create type orange by the orange Fruits Orange = factory.getFruit (FruitsFactory.TYPE_ORANGE); System.out.println (orange.getName ()); } }
Features:
1, unified entity created by the factory, easy to manage
2, need to know before creating fruit entity corresponding to the type of fruit
3, increasing the need to modify the new fruit factory code: add type, the logic determines an increase
Another alternative way to create an entity type by static factory class
public class FruitsFactory2 { public Fruits getApple(){ return new Apple(); } public Fruits getOrange(){ return new Orange(); } }
Entity created by the plant
public class FruitsController { public static void main(String[] args) { FruitsFactory2 factory=new FruitsFactory2(); Fruits apple=factory.getApple(); System.out.println(apple.getName()); Fruits orange=factory.getOrange(); System.out.println(orange.getName()); } }
Features:
1, does not need to judge the type, the method proceeds directly to call the corresponding entity to create
2, it can show the usefulness of the method of preventing Remarks
3, to add new types of fruit corresponding method
to sum up:
1, factory management to create static entity, you can create a unified management entity, allowing the user to focus the use of the class, the class creation process transparent.
2, static factory pattern with the new business needs to modify the code
3, which does not meet the design mode: Single Responsibility Principle - a fruit factory class entities created to manage multiple fruit; opening and closing principle - every new business must modify the code