抽象工厂模式
代码实现
- 创建一个形状接口
public interface Shape { void draw();}复制代码
-创建实现接口的实体类
//长方形public class Rectangle implements Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); }}复制代码
//正方形public class Square implements Shape { @Override public void draw() { System.out.println("Inside Square::draw() method."); }}复制代码
//圆形public class Circle implements Shape { @Override public void draw() { System.out.println("Inside Circle::draw() method."); }}复制代码
- 创建一个颜色接口
public interface Color { void fill();}复制代码
- 创建接口的实现类
//红色public class Red implements Color { @Override public void fill() { System.out.println("Inside Red::fill() method."); }}复制代码
//绿色public class Green implements Color { @Override public void fill() { System.out.println("Inside Green::fill() method."); }}复制代码
- 为颜色和形状创建抽象类(接口)来获取工厂
public abstract class AbstractFactory { public abstract Color getColor(String color); public abstract Shape getShape(String shape) ;}复制代码
- 创建扩展了抽象工厂的工厂类,根据给定信息生产指定产品
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("RECTANGLE")){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } @Override public Color getColor(String color) { return null; }}复制代码
public class ColorFactory extends AbstractFactory { @Override public Shape getShape(String shapeType){ return null; } @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; }}复制代码
创建一个工厂生成器
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; }}复制代码
- 测试
public class AbstractFactoryPatternDemo { public static void main(String[] args) { //获取形状工厂 AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE"); //获取形状为 Circle 的对象 Shape shape1 = shapeFactory.getShape("CIRCLE"); //调用 Circle 的 draw 方法 shape1.draw(); //获取形状为 Rectangle 的对象 Shape shape2 = shapeFactory.getShape("RECTANGLE"); //调用 Rectangle 的 draw 方法 shape2.draw(); //获取形状为 Square 的对象 Shape shape3 = shapeFactory.getShape("SQUARE"); //调用 Square 的 draw 方法 shape3.draw(); //获取颜色工厂 AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR"); //获取颜色为 Red 的对象 Color color1 = colorFactory.getColor("RED"); //调用 Red 的 fill 方法 color1.fill(); //获取颜色为 Green 的对象 Color color2 = colorFactory.getColor("Green"); //调用 Green 的 fill 方法 color2.fill(); //获取颜色为 Blue 的对象 Color color3 = colorFactory.getColor("BLUE"); //调用 Blue 的 fill 方法 color3.fill(); }}复制代码
抽象工厂
public abstract class AbstractFactory{ //不允许外部访问 protected abstract Car getCar(); public Car getCar(String name){ if("BMW".equals(name)){ return new BMWfactory().getCar(); }else if("Benz".equals(name)){ return new Benzfactory().getCar(); }else if("Audi".equals(name)){ return new Audifactory().getCar(); }else{ return null; } }}public class DefaultFactory extends AbstractFactory{private AudiFactory defaultFactory = new AudiFactory(); @Override protected abstract Car getCar(){ return defaultFactory.getCar(); }}public class AudiFactory extends AbstractFactory{ public Car getCar(){ return new Audi(); }}public class BMWFactory extends AbstractFactory{ public Car getCar(){ return new BMW(); }}public class BenzFactory extends AbstractFactory{ public Car getCar(){ return new Benz(); }}复制代码
- 测试
public static void main(String []args){ DefaultFactory factory = new DefaultFactory(); factory.get("Benz");}复制代码