设计模式-抽象工厂模式
工厂模式区别:
抽象工厂类的实现可以实现多个产品,工厂类的实现只能实现单个产品
方法:
抽象产品
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();
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!