计划模式-(单例,简朴工厂,工厂,抽象工厂)

打印 上一主题 下一主题

主题 844|帖子 844|积分 2532

单例模式

概念:

确保一个类只有一个实例,而且自行实例化并向震哥哥系统提供这个实例
应用:

无状态的,一般以工具类情势,进行提供
代码:

懒汉式,双重查抄锁
  1. class Singleton {
  2.         private static volatile Singleton instance = null;
  3.         private Singleton() {}
  4.         public static Singleton getInstance() {
  5.                 if (instance == null) {
  6.                         synchronized (Singleton.class) {
  7.                                 if (instance == null) {
  8.                                         instance = new Singleton();
  9.                                 }
  10.                         }
  11.                 }
  12.                 return instance;
  13.         }
  14. }
复制代码
备注:使用volatile修饰,是由于new Singleton()可以拆解为3步:
1、分配内存
2、初始化对象
3、指向刚分配的地址
若发生重排序,假设A线程执行了1和3,还没有执行2,B线程来到判断NULL时,不为NULL,B线程就会直接返回还没有初始化的instance了。volatile可以避免重排序


简朴工厂模式

概念:

又称为静态工厂方法模式,它属于类创建型模式。在简朴工厂模式中,可以根据参数的不同,返回不同类的实例,简朴工厂模式专门界说一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。
应用:

不敷机动,新增产品需要修改工厂类;
长处在于实现对象的创建和使用分离
代码:

  1. public class SimpleFactory {
  2.     public static Product createProduct(String type){
  3.         if (type.equals("A")){
  4.             return new ProductA();
  5.         } else {
  6.             return new ProductB();
  7.         }
  8.     }
  9.     public static void main(String[] args) {
  10.         Product p = SimpleFactory.createProduct("A");
  11.         p.print();
  12.     }
  13. }
  14. abstract class Product{
  15.     public abstract void print();
  16. }
  17. class ProductA extends Product{
  18.     @Override
  19.     public void print(){
  20.         System.out.println("A");
  21.     }
  22. }
  23. class ProductB extends Product{
  24.     @Override
  25.     public void print(){
  26.         System.out.println("B");
  27.     }
  28. }
复制代码
工厂模式

概念:

界说一个用于创建对象的接口,让子类决定实例化哪个类,工厂方法使一个类的实例化延迟到其子类
应用:


代码:

  1. // 定义产品接口
  2. interface Shape {
  3.     void draw();
  4. }
  5. // 具体产品1
  6. class Circle implements Shape {
  7.     @Override
  8.     public void draw() {
  9.         System.out.println("Drawing a Circle");
  10.     }
  11. }
  12. // 具体产品2
  13. class Square implements Shape {
  14.     @Override
  15.     public void draw() {
  16.         System.out.println("Drawing a Square");
  17.     }
  18. }
  19. // 定义工厂接口
  20. interface ShapeFactory {
  21.     Shape createShape();
  22. }
  23. // 具体工厂1
  24. class CircleFactory implements ShapeFactory {
  25.     @Override
  26.     public Shape createShape() {
  27.         return new Circle();
  28.     }
  29. }
  30. // 具体工厂2
  31. class SquareFactory implements ShapeFactory {
  32.     @Override
  33.     public Shape createShape() {
  34.         return new Square();
  35.     }
  36. }
  37. // 客户端代码
  38. public class FactoryMethodDemo {
  39.     public static void main(String[] args) {
  40.         ShapeFactory circleFactory = new CircleFactory();
  41.         Shape circle = circleFactory.createShape();
  42.         circle.draw();
  43.         ShapeFactory squareFactory = new SquareFactory();
  44.         Shape square = squareFactory.createShape();
  45.         square.draw();
  46.     }
  47. }
复制代码
抽象工厂模式

概念:

一种更高级的工厂模式,用于创建相关或依靠对象的家族,而无需明确指定具体类

代码:

  1. // 定义产品接口1
  2. interface Shape {
  3.     void draw();
  4. }
  5. // 定义产品接口2
  6. interface Color {
  7.     void fill();
  8. }
  9. // 具体产品1
  10. class Circle implements Shape {
  11.     @Override
  12.     public void draw() {
  13.         System.out.println("Drawing a Circle");
  14.     }
  15. }
  16. // 具体产品2
  17. class Square implements Shape {
  18.     @Override
  19.     public void draw() {
  20.         System.out.println("Drawing a Square");
  21.     }
  22. }
  23. // 具体产品3
  24. class RedColor implements Color {
  25.     @Override
  26.     public void fill() {
  27.         System.out.println("Filling with Red Color");
  28.     }
  29. }
  30. // 具体产品4
  31. class BlueColor implements Color {
  32.     @Override
  33.     public void fill() {
  34.         System.out.println("Filling with Blue Color");
  35.     }
  36. }
  37. // 定义抽象工厂接口
  38. interface AbstractFactory {
  39.     Shape getShape(String shapeType);
  40.     Color getColor(String colorType);
  41. }
  42. // 具体工厂1
  43. class ShapeFactory implements AbstractFactory {
  44.     @Override
  45.     public Shape getShape(String shapeType) {
  46.         if (shapeType == null) {
  47.             return null;
  48.         }
  49.         if (shapeType.equalsIgnoreCase("CIRCLE")) {
  50.             return new Circle();
  51.         } else if (shapeType.equalsIgnoreCase("SQUARE")) {
  52.             return new Square();
  53.         }
  54.         return null;
  55.     }
  56.     @Override
  57.     public Color getColor(String colorType) {
  58.         return null;
  59.     }
  60. }
  61. // 具体工厂2
  62. class ColorFactory implements AbstractFactory {
  63.     @Override
  64.     public Shape getShape(String shapeType) {
  65.         return null;
  66.     }
  67.     @Override
  68.     public Color getColor(String colorType) {
  69.         if (colorType == null) {
  70.             return null;
  71.         }
  72.         if (colorType.equalsIgnoreCase("RED")) {
  73.             return new RedColor();
  74.         } else if (colorType.equalsIgnoreCase("BLUE")) {
  75.             return new BlueColor();
  76.         }
  77.         return null;
  78.     }
  79. }
  80. // 工厂生成器
  81. class FactoryProducer {
  82.     public static AbstractFactory getFactory(String choice) {
  83.         if (choice == null) {
  84.             return null;
  85.         }
  86.         if (choice.equalsIgnoreCase("SHAPE")) {
  87.             return new ShapeFactory();
  88.         } else if (choice.equalsIgnoreCase("COLOR")) {
  89.             return new ColorFactory();
  90.         }
  91.         return null;
  92.     }
  93. }
  94. // 客户端代码
  95. public class AbstractFactoryDemo {
  96.     public static void main(String[] args) {
  97.         AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
  98.         Shape shape1 = shapeFactory.getShape("CIRCLE");
  99.         shape1.draw();
  100.         Shape shape2 = shapeFactory.getShape("SQUARE");
  101.         shape2.draw();
  102.         AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
  103.         Color color1 = colorFactory.getColor("RED");
  104.         color1.fill();
  105.         Color color2 = colorFactory.getColor("BLUE");
  106.         color2.fill();
  107.     }
  108. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

八卦阵

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表