您的位置:首页 > 编程语言 > Java开发

java设计模式之---------------抽象工厂模式

2017-05-08 09:33 337 查看
抽象工厂模式是基于工厂模式的扩展,有一个专门的工厂生产类来生产工厂。比如形状类工厂,颜色类工厂。

/***

 * 创建一个形状接口

 * @author long

 *

 */

public interface Shape {

    void draw();

}

/***

 * 创建一个正方形类,实现了Shape接口

 * @author long

 *

 */

public class Square implements Shape {

    @Override

    public void draw() {

        System.out.println("Inside Square::draw() method.");

    }

}

/***

 * 创建一个圆形类,实现Shape接口

 * @author long

 *

 */

public class Circle implements Shape {

    @Override

    public void draw() {

        System.out.println("Inside Circle::draw() method.");

    }

}

/***

 * 创建一个矩形类,实现Shape接口

 * @author long

 *

 */

public class Rectangle implements Shape {

    @Override

    public void draw() {

        System.out.println("Inside Rectangle::draw() method.");

    }

}

/***

 * 为颜色创建一个接口

 * @author long

 *

 */

public interface Color {

    void fill();

}

/***

 * 创建Blue类,实现Color接口

 * @author long

 *

 */

public class Blue implements Color {

    @Override

    public void fill() {

        System.out.println("Inside Blue::fill() method.");

    }

}

/***

 * 创建Green类,实现了Color接口

 * @author long

 *

 */

public class Green implements Color {

    @Override

    public void fill() {

        System.out.println("Inside Green::fill() method.");

    }

}

/***

 * 创建Red类,实现了Color接口

 * @author long

 *

 */

public class Red implements Color {

    @Override

    public void fill() {

        System.out.println("Inside Red::fill() method.");

    }

}

/***

 * 为Color和Shape对象创建抽象类来获取工厂

 * @author long

 *

 */

public abstract class AbstractFactory {

    abstract Color getColor(String color);

    abstract Shape getShape(String shape);

}

/***

a7d3

 * 创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。

 * @author long

 *

 */

public class ColorFactory extends AbstractFactory {

    @Override

    public Color getColor(String color) {

        if(color == null){

            return null;

        }

        if(color.equalsIgnoreCase("RED")){

            return new Red();

        }else if(color.equalsIgnoreCase("GREEN")){

            return new Green();

        }else if(color.equalsIgnoreCase("BLUE")){

            return new Blue();

        }

        return null;

    }

    @Override

    Shape getShape(String shape) {

        return null;

    }

}

/***

 * 创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。

 * @author long

 *

 */

public class ShapeFactory extends AbstractFactory{

    @Override

    public Shape getShape(String shapeType) {

        if(shapeType == null){

            return null;

        }

        if(shapeType.equalsIgnoreCase("CIRCLE")){

            return new Circle();

        }else if(shapeType.equalsIgnoreCase("SQUARE")){

            return new Square();

        }else if(shapeType.equalsIgnoreCase("RECTANGLE")){

            return new Rectangle();

        }

        return null;

    }

    @Override

    Color getColor(String color) {

        return null;

    }

}

/***

 * 创建一个工厂生成器,生成具体的工厂

 * @author long

 *

 */

public class FactoryProducer {

    public static AbstractFactory getFactory(String choice){

        if(choice.equalsIgnoreCase("SHAPE")){

            return new ShapeFactory();

        }else if(choice.equalsIgnoreCase("COLOR")){

            return new ColorFactory();

        }

        return null;

    }

}

/***

 * 具体的测试类

 * @author long

 *

 */

public class Test {

    public static void main(String[] args) {

        //获取形状工厂

        AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");

        //获取颜色工厂

        AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");

        

        //获取形状

        Shape shape1 = shapeFactory.getShape("circle");

        Shape shape2 = shapeFactory.getShape("square");

        Shape shape3 = shapeFactory.getShape("rectangle");

        shape1.draw();

        shape2.draw();

        shape3.draw();

        

        Color red = colorFactory.getColor("red");

        Color blue = colorFactory.getColor("blue");

        Color green = colorFactory.getColor("green");

        red.fill();

        blue.fill();

        green.fill();

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: