当前位置: 首页 > news >正文

gof23 设计模式 各个模式代码demo

Gof23 设计模式,也叫Gang of Four(GoF)设计模式,是由四位设计模式大师(Erich Gamma、Richard Helm、Ralph Johnson 和 John Vlissides)撰写的一本书——《设计模式:可复用面向对象软件的基础》所引起的热潮,它提出了23种软件设计模式,这些模式可以帮助开发人员更好地理解、设计和实现一个软件系统。

这23种模式分为三类:创建型模式(Creational Patterns)、结构型模式(Structural Patterns)和行为型模式(Behavioral Patterns)。

创建型模式:

1. 工厂方法模式(Factory Method)

2. 抽象工厂模式(Abstract Factory)

3. 单例模式(Singleton)

4. 建造者模式(Builder)

5. 原型模式(Prototype)

结构型模式:

1. 适配器模式(Adapter)

2. 桥接模式(Bridge)

3. 组合模式(Composite)

4. 装饰器模式(Decorator)

5. 外观模式(Facade)

6. 享元模式(Flyweight)

7. 代理模式(Proxy)

行为型模式:

1. 责任链模式(Chain of Responsibility)

2. 命令模式(Command)

3. 解释器模式(Interpreter)

4. 迭代器模式(Iterator)

5. 中介者模式(Mediator)

6. 备忘录模式(Memento)

7. 观察者模式(Observer)

8. 状态模式(State)

9. 策略模式(Strategy)

10. 模板方法模式(Template Method)

11. 访问者模式(Visitor)

以下是各个模式的代码 Demo:

工厂方法模式:

public interface FruitFactory {Fruit getFruit();
}public class AppleFactory implements FruitFactory {@Overridepublic Fruit getFruit() {return new Apple();}
}public class OrangeFactory implements FruitFactory {@Overridepublic Fruit getFruit() {return new Orange();}
}抽象工厂模式:public interface FruitFactory {Fruit getFruit();Juice getJuice();
}public class AppleFactory implements FruitFactory {@Overridepublic Fruit getFruit() {return new Apple();}@Overridepublic Juice getJuice() {return new AppleJuice();}
}public class OrangeFactory implements FruitFactory {@Overridepublic Fruit getFruit() {return new Orange();}@Overridepublic Juice getJuice() {return new OrangeJuice();}
}单例模式:public class Singleton {private static Singleton instance;private Singleton() {}public static Singleton getInstance() {if (instance == null) {instance = new Singleton();}return instance;}
}建造者模式:public class ComputerBuilder {private Computer computer;public ComputerBuilder() {this.computer = new Computer();}public ComputerBuilder setCpu(String cpu) {this.computer.setCpu(cpu);return this;}public ComputerBuilder setRam(String ram) {this.computer.setRam(ram);return this;}public ComputerBuilder setStorage(String storage) {this.computer.setStorage(storage);return this;}public Computer build() {return this.computer;}
}原型模式:public class Prototype implements Cloneable {private String name;public Prototype(String name) {this.name = name;}@Overridepublic Object clone() throws CloneNotSupportedException {return super.clone();}public String getName() {return name;}public void setName(String name) {this.name = name;}
}
http://www.lryc.cn/news/20094.html

相关文章:

  • 0 初识Kotlin
  • 阿里云服务器部署SpringBoot+Vue项目(宝塔面板傻瓜式操作)
  • 27. 移除元素 26. 删除有序数组中的重复项 88. 合并两个有序数组(双指针遍历)
  • 什么时候用std::move()?
  • 建立做机器学习项目的范式
  • 搭建k8s高可用集群—20230225
  • Java 修饰符和多态
  • 学了一年Java的我,想转嵌入式了
  • 【Git】Git冲突与解决方法
  • 深度剖析数据在内存的存储
  • 身高排序(绝对值大的排后面,小的排前面)
  • 高频前端面试题之HTML篇(三)
  • 使用DG备份恢复测试库的流程以及可能出现的问题
  • Springboot注释解析
  • C语言之通讯录(动态 存储文件版)
  • Linux 工具
  • Java知识复习(七)常见的设计模式(装饰、代理、观察、策略、建造)
  • Linux系统看门狗应用编程
  • Spring MVC 源码- LocaleResolver 组件
  • Servlet
  • 简单的周总结
  • Elasticsearch7.8.0版本进阶——IK中文分词器
  • 一个阿里P6的说不会接口自动化测试,他不会是自己评的吧...
  • 规则引擎与风控系统04:风控系统实例(下)
  • 我为什么选择Linux mint 21.1 “Vera“ ? Mint安装优化调教指南(分辨率DPI)
  • 雅思经验(十四)
  • 刚来的薪资20k,是我的2倍,我是真的卷不过,真的太变态了
  • 五、DeepWalk、Node2Vec论文精读与代码实战【CS224W】(Datawhale组队学习)
  • 学习 Python 之 Pygame 开发魂斗罗(四)
  • Linux 基础知识:指令与shell