设计模式-抽象工厂模式

2023-12-24 07:35:58

工厂模式区别:
抽象工厂类的实现可以实现多个产品,工厂类的实现只能实现单个产品
方法:
抽象产品
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();

文章来源:https://blog.csdn.net/ks_13815436529/article/details/135165651
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。