23设计模式
来源:互联网 发布:nginx添加模块 编辑:程序博客网 时间:2024/06/04 17:49
总体来说设计模式分为三大类:
创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
其实还有两类:并发型模式和线程池模式。
关系模式(11种)
先来张图,看看这11中模式的关系:
第一类:通过父类与子类的关系进行实现。
第二类:两个类之间。
第三类:类的状态。
第四类:通过中间类
设计模式 :宗旨 共性的抽出来,不改。各自的抽出来,扩展。
减少client环境下, new 对象() 死代码。不容易切换
继承属性 多
复写属性 没有多态。只认自己属性。访问父类属性,super.父属性。
继承方法 模板方法( 调用 复写方法()) 多
复写方法 多态。 访问父类方法,super.父方法() 多
个性方法 可以在复写的方法里调用。 多
和父类重的属性和方法必须super.调用。构造方法除外。
构造方法 调用父类 super.(..)
对象的创建模式: 单例模式 工厂模式 建造者模式 原型模式
同事对象的关系 :中介者模式
父子对象关系:模板模式
衍生类 : 工厂类 指导者 建造者 中介者
1 单例模式
饿汉式 并发有问题
懒汉式 推荐
2 工厂模式 : 子类的创建架构
把相似的多个类的对象的创建抽出来形成新的工厂类,扩展。 其他类中使用 ,对象父类.方法抽出来,不改
工厂类 Factory 各自的扩展
public Fu getZi( 参数 ){
Fu f=null;
if(){
f=new ZiA();
}else {
f=new ZiB();
}
return f;
}
父类
子类1
子类2
Clinent 共性的 不用改。如果需要改,只改工厂,client不用改。
父类 fu=工厂.get对象方法(..);
fu.共性方法(..);
4 建造者模式 复杂对象的创建架构 类似工厂模式
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。.
指导者类 Director 选择组装 并返回产品
抽象建造者类 Builder
具体建造者类 ConcreteBuilder 给产品设置各个组件
产品类 Product
可以省略 指导者或者抽象建造者类
指导者指导建造者组装,并返回产品
PersonBuilder
void buildHead();
void buildBody();
void buildFoot();
Person buildPerson();
ManBuilder
Person person;
public ManBuilder() {
person = new Person();
}
public void buildbody() {
person.setBody("建造男人的身体"); // person.setBody("建造女人的身体");
}
public void buildFoot() {
person.setFoot("建造男人的脚");
}
public void buildHead() {
person.setHead("建造男人的头");
}
public Person buildPerson() {
return person;
}
PersonDirector
public Person constructPerson(PersonBuilder pb) {
pb.buildHead();
pb.buildBody(); // pb.buildBody();
pb.buildFoot();
return pb.buildPerson();
}
client类
PersonDirector pd = new PersonDirector();
Person person = pd.constructPerson(new ManBuilder());
5 原型模式 创建对象架构
实现Cloneable接口, 拷贝二进制,创建新的对象
class Prototype implements Cloneable {
public Prototype clone(){
Prototype prototype = null;
try{
prototype = (Prototype)super.clone();
}catch(CloneNotSupportedException e){
}
return prototype;
}
}
使用原型模式复制对象不会调用类的构造方法。因为对象的复制是通过调用Object类的clone方法来完成
的,它直接在内存中复制数据,因此不会调用到类的构造方法。不但构造方法中的代码不会执行,甚至连访
问权限都对原型模式无效。还记得单例模式吗?单例模式中,只要将构造方法的访问权限设置为private
型,就可以实现单例。但是clone方法直接无视构造方法的权限,所以,单例模式与原型模式是冲突的,在使
用时要特别注意。
深拷贝与浅拷贝。Object类的clone方法只会拷贝对象中的基本的数据类型 string,对于数组、容器对象、引用对
象等都不会拷贝,这就是浅拷贝。如果要实现深拷贝,必须将原型模式中的数组、容器对象、引用对象等另
行拷贝
5 中介者模式
同事类相互关系太多3个以上,一个对象影响其他对象。使用中介者
房子中介 qq平台
有两个类A和B,类中各有一个数字,并且要保证类B中的数字永远是类A中数字的100倍。类A类B互相影响,就称为同事类。
类A
int number;
public void setNumber(int number, B b) {
this.number = number;
b.setNumber(number*100);
}
public void setNumber(int number){
this.number = number;
}
类B
int number;
public void setNumber(int number, A a) {
this.number = number;
a.setNumber(number/100);
}
public void setNumber(int number){
this.number = number;
}
Client类
A collA = new A();
B collB = new B();
collA.setNumber(100, collB);
中介者引入
中介者 AbstractMediator
protected A a;
protected B b;
public AbstractMediator(A a, B b) {
this.a = a;
this.b = b;
}
public abstract void AaffectB();
public abstract void BaffectA();
中介者 Mediator
public AbstractMediator(A a, B b) {
super(a,b);
}
public abstract void AaffectB(){
int number = a.getNumber();
b.setNumber(number*100);
}
public abstract void BaffectA(){
int number = b.getNumber();
a.setNumber(number/100);
}
类A
int number;
public void setNumber(int number, AbstractMediator m) {
this.number = number;
m.AaffectB();
}
public void setNumber(int number){
this.number = number;
}
类B
int number;
public void setNumber(int number, AbstractMediator m) {
this.number = number;
m.BaffectA();
}
public void setNumber(int number){
this.number = number;
}
client类
A collA = new A();
B collB = new B();
AbstractMediator am = new Mediator(collA, collB);
collA.setNumber(1000, am);
6 策略模式
Context 环境类
private IStrategy strategy;
public Context(IStrategy strategy){
this.strategy = strategy;
}
public void execute(){
strategy.doSomething();
}
client类
System.out.println("-----执行策略1-----");
Context context = new Context(new ConcreteStrategy1());
context.execute();
context = new Context(new ConcreteStrategy2());
context.execute();
必须对客户端(调用者)暴露所有的策略类,因为使用哪种策略是由客户端来决定的,因此,客户端应该知
道有什么策略,并且了解各种策略之间的区别,否则,后果很严重。例如,有一个排序算法的策略模式,提
供了快速排序、冒泡排序、选择排序这三种算法,客户端在使用这些算法之前,是不是先要明白这三种算法
的适用情况?再比如,客户端要使用一个容器,有链表实现的,也有数组实现的,客户端是不是也要明白链
表和数组有什么区别?
7迭代器模式
3 模板模式 子类和父类的方法复写,继承,调用架构
模板实现共性业务流程方法(调用子类对象的具体方法),子类对象实现各自被调用具体方法。
父类
methodA(..); //钩子方法
methodB(..);
templateMethod(..){
业务逻辑
this. methodA(..)
业务逻辑
methodB(..);
业务逻辑
}
子类1
methodA(..){
具体逻辑1
};
methodB(..){
具体逻辑1
};
子类2
methodA(..){
具体逻辑2
};
methodB(..){
具体逻辑2
};
Clinent 共性的 不用改
// 父类 fu=工厂.get方法(..);
父类 fu=new 子类 ( );
fu.templateMethod(..);
- C#设计模式(23种设计模式)
- C#设计模式(23种设计模式)
- C#设计模式(23种设计模式)
- C#设计模式(23种设计模式)
- C#设计模式(23种设计模式)
- C#设计模式(23种设计模式)
- C#设计模式(23种设计模式)
- 23种设计模式-设计模式总结
- C#设计模式(23种设计模式)
- 【设计模式】:23种设计模式
- 【设计模式】23种设计模式
- 设计模式:23种设计模式综述
- C#设计模式(23种设计模式)
- 【设计模式】转载23种设计模式
- 设计模式分类,23种设计模式
- JAVA设计模式---23种设计模式
- 23种设计模式
- 23种模式设计
- AngularJS 脏检查机制
- 【第16周项目1-基数排序】
- 如何让Embed Tomcat和Netty支持HTTPS
- 第十六周项目2-大数据集上排序算法性能的体验
- sqlalchemy(一)基本操作
- 23设计模式
- android java代码调用linux命令续
- angular2 Property 'map' does not exist on type 'Observable<Response>'.
- 第16周项目3-归并排序算法的改进
- PCB走线技巧
- linux 常用命令三:date
- gbk2utf utf2gbk
- Jquery Datatable editor 1.5.4破解版本
- Java源码阅读(类图自动生成工具)