Java设计模式 《工厂模式》

 Java设计模式——工厂模式


 简单工厂模式(Simple Factory Pattern):
 它是为了让程序有一个更好地封装,降低程序模块之间的耦合程度。对于简单的工厂模式,其实也可以将其理解成为一个创建对象的工具类:

 
 java code:
 public class SimpleFactory {  
  
    public Object create(Class<?> clazz) {  
        if (clazz.getName().equals(Plane.class.getName())) {  
            return createPlane();  
        } else if (clazz.getName().equals(Broom.class.getName())) {  
            return createBroom();  
        }  
          
        return null;  
    }  
      
    private Broom createBroom() {  
        return new Broom();  
    }  
      
    private Plane createPlane() {  
        return new Plane();  
    }  
}  

test code:
public class FactoryTest {  
  
    public static void main(String[] args) {  
        SimpleFactory simpleFactory = new SimpleFactory();  
        Broom broom = (Broom) simpleFactory.create(Broom.class);  
        broom.run();  
    }  
}  



工厂方法模式(Factory Method Pattern):
工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化哪一个


java code:
/*抽象工厂:*/
public abstract class VehicleFactory {  
    public abstract Moveable create();  
}  
/*具体工厂:*/
public class BroomFactory extends VehicleFactory {  
  
    @Override  
    public Moveable create() {  
        return new Broom();  
    }  
  
}  

/*抽象产品接口:*/
public interface Moveable {  
    public void run();  
}  
/*具体产品:*/
public class Broom implements Moveable {  
  
    @Override  
    public void run() {  
        System.out.println("Broom function");  
    }  
  
} 
test code:
VehicleFactory factory = new BroomFactory();  
Moveable moveable = factory.create();  
moveable.run();  




抽象工厂模式(Abstract Factory Pattern):

java code:

/*抽象工厂:*/
public abstract class AbstractFactory {  
  
    public abstract Flyable createFlyable();  
      
    public abstract Moveable createMoveable();  
      
    public abstract Writeable createWriteable();  
}  
/*具体工厂:*/
public class Factory1 extends AbstractFactory {  
  
    @Override  
    public Flyable createFlyable() {  
        return new Aircraft();  
    }  
  
    @Override  
    public Moveable createMoveable() {  
        return new Car();  
    }  
  
    @Override  
    public Writeable createWriteable() {  
        return new Pen();  
    }  
  
}  
/*抽象产品接口:*/
public interface Flyable {  
    public void fly(int i);  
} 
public interface Moveable {  
    public void move(int i);  
}  
public interface Writeable {  
    public void write(String i);  
}   


public class Aircraft implements Flyable {  
    @Override  
    public void fly(int i) {  
        System.out.println("fly"+i);  
    }  
}  
public class Car implements Moveable {  
    @Override  
    public void move(int i) {  
        System.out.println("move"+i);  
    }  
}  
public class Pen implements Writeable {  
    @Override  
    public void write(String i) {  
        System.out.println("write"+i);  
    }  
}  

test code:
public class FactoryTest {  
  
    public static void main(String[] args) {  
        AbstractFactory factory = new Factory1();  
        Flyable flyable = factory.createFlyable();  
        flyable.fly(150);  
          
        Moveable moveable = factory.createMoveable();  
        moveable.move(10);  
          
        Writeable writeable = factory.createWriteable();  
        writeable.write("Hello");  
    }  
}  

Java设计模式 《工厂模式》_第1张图片









你可能感兴趣的:(java)