抽象工厂模式

  抽象工厂模式是对工厂模式的进一步封装和拓展,属于一种创建模式,用工厂创建工厂的一种模式。其原理是使用抽象的超级工厂创建工厂,然后使用创建的工厂创建对象。
  例如:使用多种不同的颜色画出多种不同的动物。

  • 创建抽象工厂超级类
abstract class AbstractFactory {
abstract Animal getAnimal(String type);
abstract Color getColor(String color);
}
  • 创建Animal工厂
public interface Animal {
    void print(Color color);
}

public class Cat implements Animal {
    @Override
    public void print(Color color) {
        System.out.println("cur print cat(" + color + ")!");
    }
}

public class Dog implements Animal {
    @Override
    public String toString() {
        return super.toString();
    }

    @Override
    public void print(Color color) {
        System.out.println("cur print dog(" + color + ")!");
    }
}

public class Pig implements Animal {
    @Override
    public String toString() {
        return super.toString();
    }

    @Override
    public void print(Color color) {
        System.out.println("cur print pig(" + color + ")!");
    }
}

public class AnimalFactory extends AbstractFactory {
    @Override
    Animal getAnimal(String type) {
        if (type == null) {
            return null;
        }
        if (type.equalsIgnoreCase("pig")) {
            return new Pig();
        } else if (type.equalsIgnoreCase("cat")) {
            return new Cat();
        } else if (type.equalsIgnoreCase("dog")) {
            return new Dog();
        }
        return null;
    }

    @Override
    Color getColor(String color) {
        return null;
    }
}
  • 创建Color工厂
public interface Color {
    void draw();
}

public class Red implements Color {
    @Override
    public void draw() {
        System.out.println("Into Red::draw()!");
    }

    @Override
    public String toString() {
        return Red.class.getSimpleName();
    }
}

public class Green implements Color {
    @Override
    public void draw() {
        System.out.println("Into Green::draw()!");
    }

    @Override
    public String toString() {
        return Green.class.getSimpleName();
    }
}

public class Blue implements Color {
    @Override
    public void draw() {
        System.out.println("Into Blue::draw()!");
    }

    @Override
    public String toString() {
        return Blue.class.getSimpleName();
    }
}

public class ColorFactory extends AbstractFactory {
    @Override
    Animal getAnimal(String type) {
        return null;
    }

    @Override
    Color getColor(String shape) {
        if (shape == null) {
            return null;
        }
        if (shape.equalsIgnoreCase("red")) {
            return new Red();
        } else if (shape.equalsIgnoreCase("green")) {
            return new Green();
        } else if (shape.equalsIgnoreCase("blue")) {
            return new Blue();
        }
        return null;
    }
}
  • 创建工厂的FactoryProducer类
public class FactoryProducer {
    public static AbstractFactory getFactory(String type) {
        if (type == null) {
            return null;
        }
        if (type.equalsIgnoreCase("color")) {
            return new ColorFactory();
        } else if (type.equalsIgnoreCase("animal")) {
            return new AnimalFactory();
        }
        return null;
    }
}
  • 测试
public static void main(String[] args) {
        AbstractFactory shapeFactory = FactoryProducer.getFactory("color");
        Color color1 = shapeFactory.getColor("red");
        color1.draw();

        Color color2 = shapeFactory.getColor("green");
        color2.draw();

        Color color3 = shapeFactory.getColor("blue");
        color3.draw();

        AbstractFactory printFactory = FactoryProducer.getFactory("animal");
        Animal animal1 = printFactory.getAnimal("cat");
        animal1.print(color1);

        Animal animal2 = printFactory.getAnimal("dog");
        animal2.print(color2);

        Animal animal3 = printFactory.getAnimal("pig");
        animal3.print(color3);
    }
  • 运行测试
Into Red::draw()!
Into Green::draw()!
Into Blue::draw()!
cur print cat(Red)!
cur print dog(Green)!
cur print pig(Blue)!
全部评论

相关推荐

11-02 09:49
已编辑
货拉拉_测试(实习员工)
热爱生活的仰泳鲈鱼求你们别卷了:没事楼主,有反转查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务