代理(Proxy)
模式:为某对象提供一种代理以控制对该对象的访问。即客户端通过代理间接地访问该对象,从而限制、增强或修改该对象的一些特性。
ES6
class Proxys {
constructor(){
this.realSubject = new RealSubject();
}
Request() {
this.preRequest();
this.realSubject.Request();
this.postRequest();
}
preRequest() {
console.log('访问真实之前');
}
postRequest() {
console.log('访问真实之后');
}
}
class RealSubject {
Request() {
console.log('真实的')
}
}
function proxyTest() {
const proxy = new Proxys()
proxy.Request();
}
proxyTest();
// 或者
let Flower = function() {}
let xiaoming = {
sendFlower: function(target) {
let flower = new Flower()
target.receiveFlower(flower)
}
}
let B = {
receiveFlower: function(flower) {
A.listenGoodMood(function() {
A.receiveFlower(flower)
})
}
}
let A = {
receiveFlower: function(flower) {
console.log('收到花'+ flower)
},
listenGoodMood: function(fn) {
setTimeout(function() {
fn()
}, 1000)
}
}
xiaoming.sendFlower(B)
Typescript
interface motheds {
Request(): void;
}
class Proxys implements motheds {
realSubject: RealSubject;
constructor(){
this.realSubject = new RealSubject();
}
Request() {
this.preRequest();
this.realSubject.Request();
this.postRequest();
}
preRequest() {
console.log('访问真实之前');
}
postRequest() {
console.log('访问真实之后');
}
}
class RealSubject implements motheds {
Request() {
console.log('真实的')
}
}
function proxyTest() {
const proxy = new Proxys()
proxy.Request();
}
proxyTest();
代理模式的主要优点有:
- 代理模式在客户端与目标对象之间起到一个中介作用和保护目标对象的作用;
- 代理对象可以扩展目标对象的功能;
- 代理模式能将客户端与目标对象分离,在一定程度上降低了系统的耦合度;
其主要缺点是:
- 在客户端和目标对象之间增加一个代理对象,会造成请求处理速度变慢;
- 增加了系统的复杂度;
适配器(Adapter)
模式:将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作。
ES6
class Adaptee {
specificRequest() {
console.log('适配者中的被调用!');
}
}
class ClassAdapter extends Adaptee {
request() {
this.specificRequest();
}
}
const ClassAdapterTest = new ClassAdapter();
ClassAdapterTest.request();
Typescript
interface Target {
request(): void;
}
class Adaptee {
specificRequest() {
console.log('适配者中的被调用!');
}
}
class ClassAdapter extends Adaptee implements Target{
request() {
this.specificRequest();
}
}
const ClassAdapterTest = new ClassAdapter();
ClassAdapterTest.request();
该模式的主要优点如下。
- 客户端通过适配器可以透明地调用目标接口。
- 复用了现存的类,程序员不需要修改原有代码而重用现有的适配者类。
- 将目标类和适配者类解耦,解决了目标类和适配者类接口不一致的问题。
桥接(Bridge)
模式:将抽象与实现分离,使它们可以独立变化。它是用组合关系代替继承关系来实现,从而降低了抽象和实现这两个可变维度的耦合度。
ES6
// 具体实现化角色
class ConcreteImplementorA {
operationImpl() {
console.log('具体实现化角色');
this.addOperationImpl();
}
addOperationImpl() {
console.log('具体实现化角色 - 增加功能');
}
}
// 扩展抽象化角色
class RefinedAbstraction {
constructor(implementation) {
this.implementation = implementation;
}
operation() {
console.log('扩展了实现化角色能力');
this.implementation.operationImpl();
}
}
const concreteImplementorATest = new ConcreteImplementorA();
const refinedAbstractionTest = new RefinedAbstraction(concreteImplementorATest);
refinedAbstractionTest.operation();
Typescript
interface Implementor {
operationImpl(): void;
addOperationImpl():void
}
// 具体实现化角色
class ConcreteImplementorA implements Implementor {
operationImpl() {
console.log('具体实现化角色');
this.addOperationImpl();
}
addOperationImpl() {
console.log('具体实现化角色 - 增加功能');
}
}
abstract class Abstraction {
implementation!: Implementor;
constructor(implementation:Implementor) {
}
abstract operation():void
}
// 扩展抽象化角色
class RefinedAbstraction extends Abstraction {
constructor(implementation:Implementor) {
super(implementation);
this.implementation = implementation;
}
operation() {
console.log('扩展了实现化角色能力');
this.implementation.operationImpl();
}
}
const concreteImplementorATest = new ConcreteImplementorA();
const refinedAbstractionTest = new RefinedAbstraction(concreteImplementorATest);
refinedAbstractionTest.operation();
桥接(Bridge)模式的优点是:
- 由于抽象与实现分离,所以扩展能力强;
- 其实现细节对客户透明。
缺点是:由于聚合关系建立在抽象层,要求开发者针对抽象化进行设计与编程,这增加了系统的理解与设计难度。
装饰(Decorator)
模式:指在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式,它属于对象结构型模式。
ES6
class ConcreteComponent {
operation() {
console.log('ConcreteComponent - 组件功能');
}
}
class ConcreteDecorator {
constructor(components) {
this.components = components;
}
operation() {
console.log('装饰者增加力功能了');
this.components.operation();
this.addFn();
}
addFn() {
console.log('增加力功能了');
}
}
function clientTest() {
const p = new ConcreteComponent();
p.operation();
const concreteDecorator = new ConcreteDecorator(p);
concreteDecorator.operation();
}
clientTest();
Typescript
interface motheds {
operation(): void;
}
interface concreteDecorator{
addFn():void
}
class ConcreteComponent implements motheds {
operation() {
console.log('ConcreteComponent - 组件功能');
}
}
class ConcreteDecorator implements motheds, concreteDecorator {
components: motheds;
constructor(components:motheds) {
this.components = components;
}
operation() {
console.log('装饰者增加力功能了');
this.components.operation();
this.addFn();
}
addFn() {
console.log('增加力功能了');
}
}
function clientTest() {
const p = new ConcreteComponent();
p.operation();
const concreteDecorator = new ConcreteDecorator(p);
concreteDecorator.operation();
}
clientTest();
装饰(Decorator)模式的主要优点有:
- 采用装饰模式扩展对象的功能比采用继承方式更加灵活。
- 可以设计出多个不同的具体装饰类,创造出多个不同行为的组合。
其主要缺点是:装饰模式增加了许多子类,如果过度使用会使程序变得很复杂。
外观(Facade)
模式:为多个复杂的子系统提供一个一致的接口,使这些子系统更加容易被访问。该模式对外有一个统一接口,外部应用程序不用关心内部子系统的具体的细节,这样会大大降低应用程序的复杂度,提高了程序的可维护性。
ES6
class Facade {
system1 = new Subsystem1();
system2 = new Subsystem2();
system3 = new Subsystem3();
method(){
this.system1.methods();
this.system2.methods();
this.system3.methods()
}
}
class Subsystem1 {
methods() {
console.log('系统1');
}
}
class Subsystem2 {
methods() {
console.log('系统2');
}
}
class Subsystem3 {
methods() {
console.log('系统3');
}
}
const facadeTest = new Facade();
facadeTest.method();
Typescript
interface system {
methods(): void;
}
class Facade {
system1 = new Subsystem1();
system2 = new Subsystem2();
system3 = new Subsystem3();
method(){
this.system1.methods();
this.system2.methods();
this.system3.methods()
}
}
class Subsystem1 implements system{
methods() {
console.log('系统1');
}
}
class Subsystem2 implements system{
methods() {
console.log('系统2');
}
}
class Subsystem3 implements system{
methods() {
console.log('系统3');
}
}
const facadeTest = new Facade();
facadeTest.method();
外观(Facade)模式是“迪米特法则”的典型应用,它有以下主要优点。
- 降低了子系统与客户端之间的耦合度,使得子系统的变化不会影响调用它的客户类。
- 对客户屏蔽了子系统组件,减少了客户处理的对象数目,并使得子系统使用起来更加容易。
- 降低了大型软件系统中的编译依赖性,简化了系统在不同平台之间的移植过程,因为编译一个子系统不会影响其他的子系统,也不会影响外观对象。
外观(Facade)模式的主要缺点如下。
- 不能很好地限制客户使用子系统类。
- 增加新的子系统可能需要修改外观类或客户端的源代码,违背了“开闭原则”。
享元(Flyweight)
模式:运用共享技术来有效地支持大量细粒度对象的复用。 使用场景:资源池、线程池、缓存池、链接池
ES6
class ConcreteFlyweight {
constructor() {
console.log('哈哈被创建力');
}
operation(key){
console.log('哈哈享元模式设置力了' + key);
}
}
class FlyweightFactory {
constructor() {
this.flyweightInstance = null
}
getFlyweight(key) {
if(this.flyweightInstance) {
this.flyweightInstance.operation(key);
} else {
this.flyweightInstance = new ConcreteFlyweight();
this.flyweightInstance.operation(key);
}
}
}
class unFlyweightFactory {
constructor() {
this.flyweightInstance = null
}
getFlyweight(key) {
this.flyweightInstance = new ConcreteFlyweight();
this.flyweightInstance.operation(key);
}
}
const flyweightFactoryText = new FlyweightFactory();
flyweightFactoryText.getFlyweight('a');
flyweightFactoryText.getFlyweight('b');
flyweightFactoryText.getFlyweight('c');
flyweightFactoryText.getFlyweight('e');
const unFlyweightFactoryText = new unFlyweightFactory();
unFlyweightFactoryText.getFlyweight('a');
unFlyweightFactoryText.getFlyweight('b');
unFlyweightFactoryText.getFlyweight('c');
unFlyweightFactoryText.getFlyweight('e');
Typescript
interface concreteFlyweightMotheds{
operation(key:string): void;
}
abstract class WeightFactory {
flyweightInstance: concreteFlyweightMotheds|undefined;
constructor() {
}
abstract getFlyweight(key:string):void
}
class ConcreteFlyweight implements concreteFlyweightMotheds{
constructor() {
console.log('哈哈被创建力');
}
operation(key:string){
console.log('哈哈享元模式设置力了' + key);
}
}
class FlyweightFactory extends WeightFactory {
constructor() {
super();
}
getFlyweight(key:string) {
if(this.flyweightInstance) {
this.flyweightInstance.operation(key);
} else {
this.flyweightInstance = new ConcreteFlyweight();
this.flyweightInstance.operation(key);
}
}
}
class unFlyweightFactory extends WeightFactory{
constructor() {
super();
}
getFlyweight(key:string) {
this.flyweightInstance = new ConcreteFlyweight();
this.flyweightInstance.operation(key);
}
}
const flyweightFactoryText = new FlyweightFactory();
flyweightFactoryText.getFlyweight('a');
flyweightFactoryText.getFlyweight('b');
flyweightFactoryText.getFlyweight('c');
flyweightFactoryText.getFlyweight('e');
const unFlyweightFactoryText = new unFlyweightFactory();
unFlyweightFactoryText.getFlyweight('a');
unFlyweightFactoryText.getFlyweight('b');
unFlyweightFactoryText.getFlyweight('c');
unFlyweightFactoryText.getFlyweight('e');
享元模式的主要优点是:相同对象只要保存一份,这降低了系统中对象的数量,从而降低了系统中细粒度对象给内存带来的压力。
其主要缺点是:
- 为了使对象可以共享,需要将一些不能共享的状态外部化,这将增加程序的复杂性。
- 读取享元模式的外部状态会使得运行时间稍微变长。
组合(Composite)
模式:将对象组合成树状层次结构,使用户对单个对象和组合对象具有一致的访问性。 一致性,除了访问方法相同,组合调用的方法也是一样的。
ES6
class Leaf {
constructor(name) {
this.name = name;
}
operation() {
console.log('树叶被访问了' + this.name);
}
}
class Composite {
constructor() {
this.childrenList = []
}
add(part) {
this.childrenList.push(part);
}
operation() {
this.childrenList.forEach((child) => {
child.operation();
})
}
}
const composite1 = new Composite();
const composite2 = new Composite();
const leaf1 = new Leaf('a');
const leaf2 = new Leaf('b');
const leaf3 = new Leaf('c');
composite1.add(leaf1);
composite1.add(composite2);
composite2.add(leaf2);
composite2.add(leaf3);
composite1.operation();
Typescript
interface leaCon {
new(name:string):composite
}
interface composite {
operation():void
}
class Leaf implements composite {
name:string
constructor(name:string) {
this.name = name;
}
operation() {
console.log('树叶被访问了' + this.name);
}
}
class Composite implements composite{
childrenList: leaCon[];
constructor() {
this.childrenList = []
}
add(name:leaCon) {
this.childrenList.push(name);
}
operation() {
this.childrenList.forEach((child:any) => {
child.operation();
})
}
}
const composite1 = new Composite();
const composite2 = new Composite();
const leaf1 = new Leaf('a');
const leaf2 = new Leaf('b');
const leaf3 = new Leaf('c');
composite1.add(leaf1);
composite1.add(composite2);
composite2.add(leaf2);
composite2.add(leaf3);
composite1.operation();
组合模式的主要优点有:
- 组合模式使得客户端代码可以一致地处理单个对象和组合对象,无须关心自己处理的是单个对象,还是组合对象,这简化了客户端代码;
- 更容易在组合体内加入新的对象,客户端不会因为加入了新的对象而更改源代码,满足“开闭原则”;
其主要缺点是:
- 设计较复杂,客户端需要花更多时间理清类之间的层次关系;
- 不容易限制容器中的构件;
- 不容易用继承的方法来增加构件的新功能;