当前位置: 移动技术网 > IT编程>软件设计>设计模式 > 结构型设计模式(下)

结构型设计模式(下)

2019年10月05日  | 移动技术网IT编程  | 我要评论

装饰模式:

  1、定义:动态地给一个对象增加一些额外的职责,就增加对象功能来说,装饰模式比生成子类实现更为灵活

  2、模型结构:

    (1)抽象构件(component):定义一个抽象接口以规范准备接收附加责任的对象

    (2)具体构件(concretecomponent):实现抽象构件,通过装饰角色为其添加一些职责

    (3)抽象装饰类(decorator):继承抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能

    (4)具体装饰类(concretedecorator):实现抽象装饰的相关方法,并给具体构件对象添加附加的责任

  3、优点:

    (1)装饰模式与继承关系的目的都是要扩展对象的功能,但是装饰模式可以提供比继承更多的灵活性

    (2)可以通过一种动态的方式来扩展一个对象的功能,通过配置文件可以在运行时选择不同的装饰器实现不同的行为

    (3)通过使用不同的具体装饰类以及这些装饰类的排列组合,可以创造出很多不同行为的组合

    (4)具体构件类与具体装饰类可以独立变化,用户可以根据需要增加新的具体构件类和具体装饰类,

      在使用时再对其进行组合,原有代码无须改变,符合“开闭原则”

  4、缺点:

    (1)使用装饰模式进行系统设计时将产生很多小对象,这些对象的区别在于它们之间相互连接的方式有所不同,

      而不是它们的类或者属性值有所不同,同时还将产生很多具体装饰类。

      这些装饰类和小对象的产生将增加系统的复杂度,加大学习与理解的难度

    (2)这种比继承更加灵活机动的特性,也同时意味着装饰模式比继承更加易于出错,排错也很困难

      对于多次装饰的对象,调试时寻找错误可能需要逐级排查,较为烦琐

  5、适用环境:

    (1)在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责

    (2)需要动态地给一个对象增加功能,这些功能也可以动态地被撤销

    (3)当不能采用继承的方式对系统进行扩充或者采用继承不利于系统扩展和维护时

注:不能采用继承的情况主要有两类

  (1)系统中存在大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长

  (2)因为类定义不能继承(如final类)

// 抽象构件 component
interface iperson {
    getname(): string;
}
// 具体构件 concretecomponent,继承抽象构件
class tim implements iperson {
    private name: string;
    getname(): string {
        this.name = "tim";
        return this.name;
    }
}
// 抽象装饰类 decorator,继承抽象构件,并聚合抽象构件
class cloths implements iperson {
    protected person: iperson;
    constructor(person: iperson) {
        this.person = person;
    }
    getname(): string {
        return this.person.getname();
    }
}
class trousers implements iperson {
    protected person: iperson;
    constructor(person: iperson) {
        this.person = person;
    }
    getname(): string {
        return this.person.getname();
    }
}
// 具体装饰类 concretedecorator,继承抽象装饰类
class suit extends cloths {
    private info: string;
    constructor(person: iperson) {
        super(person);
    }
    getcloths(): string {
        this.info = this.person.getname() + "上衣: " + "suit...";
        return this.info;
    }
}
class jack extends cloths {
    private info: string;
    constructor(person: iperson) {
        super(person);
    }
    getcloths(): string {
        this.info = this.person.getname() + "上衣: " + "jacket...";
        return this.info;
    }
}
class pants extends trousers {
    private info: string;
    constructor(person: iperson) {
        super(person);
    }
    gettrousers(): string {
        this.info = this.person.getname() + "裤子: " + "pants...";
        return this.info;
    }
}
class jean extends trousers {
    private info: string;
    constructor(person: iperson) {
        super(person);
    }
    gettrousers(): string {
        this.info = this.person.getname() + "裤子: " + "jean...";
        return this.info;
    }
}

let tim: iperson = new tim();
console.log("get suit");
let suit: suit = new suit(tim);
console.log(suit.getcloths());  // tim上衣: suit...
console.log("get jean");
let jean: jean = new jean(tim);
console.log(jean.gettrousers());    // tim裤子: jean...

 

外观模式:

  1、定义:外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面

        外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用

  2、模型结构:

    (1)外观角色(facade):为多个子系统对外提供一个共同的接口

    (2)子系统角色(subsystem):实现系统的部分功能,客户可以通过外观角色访问它

  3、优点:

    (1)对客户屏蔽子系统组件,减少了客户处理的对象数目并使得子系统使用起来更加容易

    (2)降低了子系统与客户端之间的耦合度,使得子系统的变化不会影响调用它的客户类

    (3)降低了大型软件系统中的编译依赖性,简化了系统在不同平台之间的移植过程,

      因为编译一个子系统不会影响其他的子系统,也不会影响外观对象

    (4)只是提供了一个访问子系统的统一入口,并不影响用户直接使用子系统类

  4、缺点:

    (1)不能很好地限制客户使用子系统类,如果对客户访问子系统类做太多的限制则减少了可变性和灵活性

    (2)在不引入抽象外观类的情况下,增加新的子系统可能需要修改外观类或客户端的源代码,违背了“开闭原则”

  5、适用环境:

    (1)对分层结构系统构建时,使用外观模式定义子系统中每层的入口点可以简化子系统之间的依赖关系

    (2)当一个复杂系统的子系统很多时,外观模式可以为系统设计一个简单的接口供外界访问

    (3)当客户端与子系统之间存在很大的联系时,引入外观模式可将它们分离,从而提高子系统的独立性和可移植性

// 子系统类
class light {
    private name: string;
    constructor(name) {
        this.name = name;
    }
    open(): void {
        console.log(this.name + " opened!");
    }
}
class heater {
    open(): void {
        console.log("heater opened!");
    }
}

// 外观类,统一操作多个子系统
class facade{
    private light1: light;
    private light2: light;
    private light3: light;
    private heater: heater;
    constructor() {
        this.light1 = new light("light1");
        this.light2 = new light("light2");
        this.light3 = new light("light3");
        this.heater = new heater();
    }
    open(): void {
        this.light1.open();
        this.light2.open();
        this.light3.open();
        this.heater.open();
    }
}

let facade: facade = new facade();
facade.open();  // light1 opened!
                // light2 opened!
                // light3 opened!
                // heater opened!

 

享元模式:

 

  1、定义:运用共享技术来有効地支持大量细粒度对象的复用。它通过共享已经存在的对象来

        大幅度减少需要创建的对象数量、避免大量相似类的开销,从而提高系统资源的利用率

  2、模型结构:

    (1)抽象享元类(flyweight):是所有的具体享元类的基类,为具体享元规范需要实现的公共接口,

                     非享元的外部状态以参数的形式通过方法传入

    (2)具体享元类(concreteflyweight):实现抽象享元角色中所规定的接口

    (3)非共享具体享元类(unsharedconcreteflyweight):不可共享的外部状态,以参数形式注入具体享元的相关方法中

    (4)享元工厂类(flyweightfactory):负责创建和管理享元角色。当客户对象请求一个享元对象时,

                          享元工厂检査系统中是否存在符合要求的享元对象,如果存在则提供给客户;

                          如果不存在的话,则创建一个新的享元对象

  3、优点:

    (1)享元模式的优点在于它可以极大减少内存中对象的数量,使得相同对象或相似对象在内存中只保存一份

    (2)享元模式的外部状态相对独立,而且不会影响其内部状态,从而使得享元对象可以在不同的环境中被共享

  4、缺点:

    (1)享元模式使得系统更加复杂,需要分离出内部状态和外部状态,这使得程序的逻辑复杂化

    (2)读取享元模式的外部状态会使得运行时间稍微变长

  5、适用环境:

    (1)一个系统有大量相同或者相似的对象,由于这类对象的大量使用,造成内存的大量耗费

    (2)大部分的对象可以按照内部状态进行分组,且可将不同部分外部化,这样每一个组只需保存一个内部状态

    (3)由于享元模式需要额外维护一个保存享元的数据结构,所以应当在有足够多的享元实例时才值得使用享元模式

// 抽象享元类 flyweight,书本
interface book {
    cell(): void;
}
// 具体享元类 concreteflyweight,书名
class bookorder implements book {
    private name: string;
    constructor(name: string) {
        this.name = name;
    }
    cell(): void {
        console.log("cell a book, named " + this.name);
    }
}
// 享元工厂类 flyweightfactory
class bookfactory {
    // 享元池
    private bookpools: map<string, book> = new map<string, book>();
    // 和单例模式结合
    private constructor() {}
    private static factory: bookfactory = new bookfactory();
    static getinstance(): bookfactory {
        return this.factory;
    }
    getorder(bookname: string): book {
        let order: book = null;
        // 如果存在该书,则从享元池中取出,否则新建一个书对象
        if (this.bookpools.has(bookname)) {
            order = this.bookpools.get(bookname);
        } else {
            order = new bookorder(bookname);
            this.bookpools.set(bookname, order);
        }
        return order;
    }
    // 返回创建对象的数目
    gettotalobjs(): number {
        return this.bookpools.size;
    }
}



let orders: book[] = new array<book>();
// 确保只有一个工厂对象
let factory: bookfactory = bookfactory.getinstance();

function takeorders(bookname: string): void {
    orders.push(factory.getorder(bookname));
}

takeorders("三国演义");
takeorders("水浒传");
takeorders("封神榜");
takeorders("三体");
takeorders("红楼梦");
takeorders("三国演义");
takeorders("封神榜");
takeorders("水浒传");

for (let i in orders) {
    orders[i].cell();
}

console.log("\n客户一共买了 " + orders.length + " 本书! "); // 客户一共买了 8 本书! 
console.log("共生成了 " + factory.gettotalobjs() + " 个 book 对象! ");  // 共生成了 5 个 book 对象! 

 

代理模式:

  1、定义:给某一个对象提供一个代理,并由代理对象控制对原对象的引用

  2、模型结构:

    (1)抽象主题角色(subject):通过接口或抽象类声明真实主题和代理对象实现的业务方法

    (2)代理主题角色(proxy):提供了与真实主题相同的接口,其内部含有对真实主题的引用

                    它可以访问、控制或扩展真实主题的功能

    (3)真实主题角色(realsubject):实现抽象主题中的具体业务,是代理对象所代表的真实对象,是最终要引用的对象

  3、优点:

    (1)代理模式能够协调调用者和被调用者,在一定程度上降低了系统的耦合度

    (2)代理模式在客户端与目标对象之间起到一个中介作用和保护目标对象的作用

    (3)代理对象可以扩展目标对象的功能

  4、缺点:

    (1)在客户端和真实主题之间增加了代理对象,因此有些类型的代理模式可能会造成请求的处理速度变慢

    (2)实现代理模式需要额外的工作,有些代理模式的实现非常复杂,增加系统复杂度

  5、适用环境:

    (1)远程(remote)代理:为一个位于不同的地址空间的对象提供一个本地的代理对象,

      这个不同的地址空间可以是在同一台主机中,也可是在另一台主机中,远程代理又叫做大使(ambassador)

    (2)虚拟(virtual)代理:如果需要创建一个资源消耗较大的对象,

      先创建一个消耗相对较小的对象来表示,真实对象只在需要时才会被真正创建

    (3)保护(protect or access)代理:控制对一个对象的访问,可以给不同的用户提供不同级别的使用权限

    (4)智能引用(smart reference)代理:当一个对象被引用时,提供一些额外操作,如记录该对象被调用的次数等

    (5)缓冲(cache)代理:为某一个目标操作的结果提供临时的存储空间,以便多个客户端可以共享这些结果

    (6)防火墙(firewall)代理:保护目标不让恶意用户接近

    (7)同步化(synchronization)代理:使几个用户能够同时使用一个对象而没有冲突

    (8)copy-on-write代理:它是虚拟代理的一种,把复制(克隆)操作延迟到只有在客户端真正需要时才执行

// 保护代理
//  抽象主题角色 subject
interface doc {
    read(): string;
}
// 真实主题角色 realsubject
class secretdoc implements doc {
    private secret: string = "secret doc!!!";
    private password: string = "123";
    read(): string {
        return this.secret;
    }
    getpsw(): string {
        return this.password;
    }
}
// 代理主题角色 proxy
class protectionsecretdocproxy implements doc{
    private doc: secretdoc;
    private inputpsw: string;
    constructor(password: string) {
        this.inputpsw = password;
        this.doc = new secretdoc();
    }
    // 判断密码是否正确
    checkpassword(): boolean {
        return this.doc.getpsw() === this.inputpsw;
    }
    read(): string {
        if (this.checkpassword()) {
            return this.doc.read();
        }
        return "password is wrong!";
    }
}

let userproxy1: protectionsecretdocproxy = new protectionsecretdocproxy("123");
console.log("user1 get: " + userproxy1.read()); // user1 get: secret doc!!!
let userproxy2: protectionsecretdocproxy = new protectionsecretdocproxy("234");
console.log("user2 get: " + userproxy2.read()); // user2 get: password is wrong!

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网