软件设计模式总结 02
单例模式
classDiagram
class Singleton {
- instance: Singleton
- Singleton()
+ getInstance(): Singleton
}
note for Singleton "if (instance == null) {
instance = new Singleton();
return instance; }"
Singleton o--> Singleton : instance结构
Singleton
单例模式只有一个类,没有接口。
单例模式是一种创建型设计模式,让你能够保证一个类只有一个实例,并提供一个访问该实例的全局节点。
适配器模式
---
title: class adapter
---
classDiagram
class Target {
<<interface>>
+ request()
}
class Adaptee {
+ request()
}
class Adapter {
- specificRequest()
}
note for Adapter "super.request();"
class Client
Client ..> Target
Target <|.. Adapter
Adapter --> Adaptee---
title: object adapter
---
classDiagram
class Target {
+ request()
}
class Adaptee {
+ request()
}
class Adapter {
+ specificRequest()
}
note for Adapter "adaptee.specificRequest();"
class Client
Client ..> Target
Target <|-- Adapter
Adapter --> Adaptee结构
Target
Adaptee
Adapter
Target 是客户端所期待的接口,Adaptee 是需要适配的类,Adapter 是适配器。
适配器模式是一种结构型设计模式,它能使接口不兼容的对象能够相互合作。
桥接模式
classDiagram
class Implementor {
<<interface>>
+ operationImpl()
}
class ConcreteImplementorA {
+ operationImpl()
}
class ConcreteImplementorB {
+ operationImpl()
}
class Abstraction {
+ operation()
}
class Client
note for Abstraction "impl.operationImpl();"
class RefinedAbstraction {
+ operation()
}
Abstraction <|-- RefinedAbstraction
Abstraction o--> Implementor : impl
ConcreteImplementorA ..|> Implementor
ConcreteImplementorB ..|> Implementor
class Client
Client ..> Abstraction
Client ..> Implementor结构
Implementor - 实现者接口
ConcreteImplementor - 具体实现者
Abstraction - 抽象类
RefinedAbstraction - 扩充抽象类
桥接模式是一种结构型设计模式,可将一个大类或一系列紧密相关的类拆分为抽象和实现两个独立的层次结构,从而能在开发时分别使用。
组合模式
classDiagram
class Component {
<<interface>>
+ operation()
}
class Leaf {
+ operation()
}
class Composite {
+ operation()
+ add(Component c)
+ remove(Component c)
+ getChild(int index): Component
}
note for Composite "for (Component c : children) {
c.operation(); }"
class Client
Component <|-- Leaf
Component <|-- Composite
Composite o--> Component : children
Client ..> Component结构
Component - 抽象构件
Leaf - 叶节点
Composite - 容器构件
组合模式是一种结构型设计模式,你可以使用它将对象组合成树状结构,并且能像使用独立对象一样使用它们。
装饰器模式
classDiagram
class Component {
+ operation()
}
class ConcreteComponent {
+ operation()
}
class Decorator {
+ operation()
}
note for Decorator "component.operation();"
class ConcreteDecoratorA {
- addedState
+ operation()
}
class ConcreteDecoratorB {
- addedBehavior
+ operation()
}
note for ConcreteDecoratorA "super.operation();
addedBehavior();"
Component <|-- ConcreteComponent
Component <|-- Decorator
Component <--o Decorator : component
Decorator <|-- ConcreteDecoratorA
Decorator <|-- ConcreteDecoratorB结构
Component - 抽象构件
ConcreteComponent - 具体构件
Decorator - 抽象装饰器
ConcreteDecorator - 具体装饰器
装饰器模式是一种结构型设计模式,允许你通过将对象放入包含行为的特殊封装对象中来为原对象绑定新的行为。
外观模式
classDiagram
class Subsystem1 {
+ operation1()
}
class Subsystem2 {
+ operation2()
}
class Subsystem3 {
+ operation3()
}
class Facade {
+ operation()
}
note for Facade "subsystem1.operation1();
subsystem2.operation2();
subsystem3.operation3();"
Client ..> Facade
Facade --> Subsystem1 : subsystem1
Facade --> Subsystem2 : subsystem2
Facade --> Subsystem3 : subsystem3结构
Subsystem - 子系统
Facade - 外观
外观模式是一种结构型设计模式,它为复杂子系统提供一个简单接口,使其更易于使用。
代理模式
classDiagram
class Subject {
<<interface>>
+ request()
}
class RealSubject {
+ request()
}
class Proxy {
- realSubject: RealSubject
+ preRequest()
+ request()
+ postRequest()
}
note for Proxy "preRequest();
realSubject.request();
postRequest();"
Subject <|-- RealSubject
Subject <|-- Proxy
Proxy --> RealSubject : realSubject
Client ..> Subject结构
Subject - 抽象主题
RealSubject - 真实主题
Proxy - 代理
代理模式是一种结构型设计模式,让你能提供对象的替代品或其占位符。代理控制对于原始对象的访问,并允许在将请求提交给对象前后进行一些处理。
迭代器模式
classDiagram
class Iterator {
<<interface>>
+ first()
+ next()
+ isDone()
+ currentItem()
}
class ConcreteIterator {
+ first()
+ next()
+ isDone()
+ currentItem()
}
class Aggregate {
<<interface>>
+ createIterator()
}
class ConcreteAggregate {
+ createIterator()
}
Iterator <|.. ConcreteIterator
Aggregate <|.. ConcreteAggregate
ConcreteAggregate ..> ConcreteIterator
ConcreteAggregate <-- ConcreteIterator : aggregate结构
Iterator - 迭代器
ConcreteIterator - 具体迭代器
Aggregate - 抽象聚合
ConcreteAggregate - 具体聚合
迭代器模式是一种行为设计模式, 让你能在不暴露集合底层表现形式(列表、栈和树等)的情况下遍历集合中所有的元素。