设计模式系列之结构型模式

介绍

为了解决怎样组装现有的类,设计他们的交互方式,从而实现一定的功能

适配器模式

将一个类的接口变换为客户端所期待的另一个接口
从而使原本因接口不匹配而无法在一起工作的两个类能够一起工作
也就是说原本的类或者实例通过适配器的方式添加了新的功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 接口,定义了多种方法
interface Target {
request(): void;
}
// 原类
class Adaptee {
constructor() { }
public specificRequest(): void { }
}
// 适配器给原类添加新的接口
class Adapter extends Adaptee implements Target {
constructor() {
super()
}
public request(): void {
super.specificRequest()
}
}

装饰者模式

动态的将新功能附加到对象上
装饰者模式也是一种非常常见的模式,其实也就是我们日常使用的装饰器,将公共的方法变成一个装饰方法或者装饰类。
这样就能简化代码量。

比如在后端中每个接口都需要判断用户是否登录,可以写一个装饰方法定义用户的登录状态
这样每个需要判断用户登录态的接口直接用装饰器装饰即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Component {
public operate(): void { }
}

class Decorator extends Component {
constructor() {
super()
}
private method(): void { }
public operate(): void {
this.method()
super.operate()
}
}

组合模式

主要用来描述部分和整体的关系,将对象组合成树形结构表示部分-整体的结构层次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
abstract class Component {
protected name: string;
constructor(name: string) {
this.name = name
}
public abstract doOperation(): void;
public add(component: Component): void { }
public remove(component: Component): void { }
public getChildren(): Array<Component> {
return []
}
}

class Composite extends Component {
private componentList: any;
constructor(name: string) {
super(name)
this.componentList = []
}
public doOperation(): void {
console.log(this.name)
}
public add(component: Component): void {
this.componentList.push(component)
}
public remove(component: Component): void {
const componentIndex = this.componentList.findIndex((value: Component, index: Number) => {
return value == component;
});
this.componentList.splice(componentIndex, 1);
}
public getChildren(): Array<Component> {
return this.componentList
}
}

class Leaf extends Component {
constructor(name: string) {
super(name)
}
public doOperation(): void {
console.log(this.name)
}
}

function main() {
const root: Component = new Composite('root');
const node1: Component = new Leaf('1');
const node2: Component = new Composite('2');
const node3: Component = new Leaf('3');

root.add(node1);
root.add(node2);
root.add(node3);

const node2_1: Component = new Leaf("2_1");
node2.add(node2_1);

const children1 = root.getChildren();
console.log(children1);

root.remove(node2);

const children2 = root.getChildren();
console.log(children2);
}

main();

代理模式

为其他对象提供一种代理以控制对这个对象的询问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 静态代理
interface Subject {
doOperation(): void;
}

class RealSubject implements Subject {
public doOperation(): void {
console.log('RealSubject running')
}
}

class MyProxy implements Subject {
private target: Subject;
constructor(realSubject: Subject) {
this.target = realSubject
}
public doOperation() {
console.log('proxy class')
this.target.doOperation()
}
}

function main() {
const realSubject: Subject = new RealSubject();
const myProxy: Subject = new MyProxy(realSubject);

myProxy.doOperation();
}

main();

文章作者: woyao
文章链接: https://chenwoyao.github.io/2021/04/23/设计模式/设计模式系列之结构型模式/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 woyao的博客