工厂模式区别:
抽象工厂类的实现可以实现多个产品,工厂类的实现只能实现单个产品
方法:
抽象产品
public Interface Shape{void draw();};
public Interface Color{void fill();};
具体产品
public class Circle implements Shape{void draw()};
public class Retangle implements Shape{void draw()};
public class Bull implements Color{void fill();};
public class Red implements Color{void fill();};
抽象工厂
public interface AbstractFactory{Shape getShape(string shape); Color getColor(string color)};
具体工厂
public class AFactory implements AbstractFactory{
Shape getShape(string shape){if(shape.equel(“Circle”)) return new Circle;};
Color getColor(string color){if(color.equel(“Bull”)) return new Bull;}}
public class BFactory implements AbstractFactory{
Shape getShape(string shape){if(shape.equel(“Retangle”)) return new Retangle;};
Color getColor(string color){if(color.equel(“Red”)) return new Red;}}
调用
AFactory shapeFactory = new AFactory ();
shapeFactory.getShape(“Circle”).draw();
BFactory colorFactory = new BFactory ();
colorFactory.getColor(“Red”).fill();