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;}
}
gof23 设计模式 各个模式代码demo相关推荐
- Python设计模式-建造者模式
Python设计模式-建造者模式 代码基于3.5.2,代码如下; #coding:utf-8 #建造者模式 class Burger():name = ""price = 0.0d ...
- Python设计模式-状态模式
Python设计模式-状态模式 代码基于3.5.2,代码如下; #coding:utf-8 #状态模式class state():def writeProgram(self,work):raise N ...
- Python设计模式-备忘录模式
Python设计模式-备忘录模式 代码基于3.5.2,代码如下; #coding:utf-8 #备忘录模式 import randomclass gameCharacter():vitality = ...
- Python设计模式-解释器模式
Python设计模式-解释器模式 代码基于3.5.2,代码如下; #coding:utf-8 #解释器模式class PlayContext():play_text = Noneclass Expre ...
- Python设计模式-命令模式
Python设计模式-命令模式 代码基于3.5.2,代码如下; #coding:utf-8 #命令模式class barbecuer():def bakeButton(self):print(&quo ...
- Python设计模式-策略模式
Python设计模式-策略模式 代码基于3.5.2,代码如下; #coding:utf-8 #策略模式class sendInterface():def send(self,value):raise ...
- Python设计模式-外观模式
Python设计模式-外观模式 代码基于3.5.2,代码如下; #coding:utf-8 # 外观模式class AlarmSensor:def run(self):print("Alar ...
- GOF23设计模式(创建型模式)工厂模式
目录: 一:工厂模式的核心本质 二:关于面向对象的六大基本原则 三:工厂模式的三大类详解(代码示例,详细分析) 首先,上咱本GOF23所有工厂模式的分类表格!!! 创建型模式 单例模式.工厂模式.抽象 ...
- 【GOF23设计模式】原型模式
[GOF23设计模式]原型模式 来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_原型模式.prototype.浅复制.深复制.Cloneable接口 浅复制 1 pa ...
最新文章
- 软定时器的原理与创建
- 2017年本博客知识体系引导(更新至2017.8.11)
- CodeForces - 706D Vasiliy's Multiset(字典树删除操作)
- 【测评】想买投影仪,预算又不多,该怎么选?——三款高性价比投影仪PK测评
- Windows 下的批处理脚本基础——网络相关命令(用户操作命令、用户组操作命令)
- IT人员看待和预防癌症十大建议
- 零基础学python数据分析_Python学习指南:使用Python学习数据分析
- 微信小程序保存canvas绘制的图片到本地,拒绝图片授权后继续授权
- 『编程题全队』Alpha 阶段冲刺博客集合
- [转]Java jdbc数据库连接池总结!
- static在实例Extends、Overload中理解
- 清华大学刘知远:在深度学习时代用HowNet搞事情
- 【转】极品免费网站空间申请:000webhost.com免费1.5G美国空间PHP+MySQL
- 【作业分享】Reverse Polish Notation | 数据结构·stack
- 4 个方法养成大神级 “反内耗“ 体质
- PVM and MPI 比较
- Android 反编译资料整理
- 鸿蒙系统教程,麒麟9000+鸿蒙操作系统,华为新平板有点牛
- 多项式(带余)除法学习笔记
- python京东抢购手机攻略_Python实现自动上京东抢手机