抽象工厂模式

来源:互联网 发布:unity3d语言 编辑:程序博客网 时间:2024/06/05 16:54
/** * @version 0.1 */public interface Type {    void show();}


/** * @version 0.1 */public class Circle implements Type {    @Override    public void show() {        System.out.println("This is a circle type.");    }}

/** * @version 0.1 */public class Rectangle implements Type {    @Override    public void show() {        System.out.println("This is a rectangle type.");    }}

/** * 抽象工厂模式:设计接口,对不同的生产实例的需求创建不同的工厂, *      这种设计在新加功能时只需要添加新的工厂来生产实例, *      避免了对工厂类的修改 * @version 0.1 */public interface TypeFactory {     Type getType();}

/** * @version 0.1 */public class CircleFactory implements TypeFactory {    @Override    public Type getType() {        return new Circle();    }}

/** * @version 0.1 */public class RectangleFactory implements TypeFactory {    @Override    public Type getType() {        return new Rectangle();    }}

/** * @version 0.1 */public class Test {    public static void main(String[] args) {        TypeFactory circleFactory = new CircleFactory();        TypeFactory rectangleFactory = new RectangleFactory();        Type circle =  circleFactory.getType();        Type rectangle = rectangleFactory.getType();        circle.show();        rectangle.show();    }}


原创粉丝点击