【设计模式】之六大设计原则--下篇

接口隔离原则(Interface Segregation Principle, ISP)

接口分为两种:
实例接口,JAVA中类也是一个接口
类接口,JAVA经常使用interface关键字定义的接口
define:Clients should not be forced to depend upon interfaces that they don’t use.
(客户端不应该依赖它不需要的接口。)
The dependency of one class to another one should depend on the smallest possible interface.
(类间的依赖关系应该建立在最小的接口上。)
通俗的讲,就是尽量把接口细化。注意与单一职责原则的区别,单一职责不一定接口是最细化的。
根据接口隔离原则拆分接口时,首先必须满足单一职责原则,保证接口不会过分的臃肿

接口要高内聚,即接口尽量少公布public方法,接口是对外的承诺,承诺越少对系统的开发越有利
定制服务,为各个客户端定制服务,提供访问者需要的方法

接口和类尽量使用原子接口或原子类来组装,衡量原子划分的几个规则:
1 一个接口只服务于一个子模块或业务逻辑
2 通过业务逻辑压缩接口中的public方法,接口时常去回顾,尽量让接口达到“满身筋骨”,而不是“肥嘟嘟”的一大堆方法
3 已经被污染了的接口,尽量去修改,若变更的风险较大,则采用适配器模式进行转化处理
4 了解环境,拒绝盲从

迪米特法则(Law of Demeter, LoD)

最少知识原则 Least Knowledge Principle LKP
一个对象应该对其他对象有最少的了解。
通俗来说,只需要知道提供的方法或接口是实现的什么目的,至于怎么去实现不关心。

四层含义:
1 Only talk to your immediate friends.(只与直接的朋友通信)
不需要不必要的依赖,降低系统间的耦合,提高系统的健壮性
eg.
体育老师清点班级女生人数
错误的做法
体育老师获取女生列表,然后通知体育委员清点,最后体育委员拿着列表点名
这样做会使体育老师和体育委员都依赖女生类
正确的做法
体育老师通知体育委员清点,体育委员直接获取女生列表点名。这样体育老师类将不会依赖女生类
2 朋友间也是有距离的
尽量不要对外公布太多的public方法和非静态的public变量。
3 是自己的就是自己的
如果一个方法放在本类中,既不增加类间关系,也对本类不产生负面影响,那就放置在本类中。
4 谨慎使用Serializable

核心观念就是类间解耦,弱耦合,提高类的复用率。

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

class Wizard {
private:
    int first() {
        cout << "first..." << endl;
        srand(time(0));
        return rand()%100;
    }
    int second() {
        cout << "second..." << endl;
        srand(time(0));
        return rand()%100;
    }
    int third() {
        cout << "third..." << endl;
        srand(time(0));
        return rand()%100;
    }
public:
    void installWizard() {
        int f = first();
        if(f > 50) {
            int s = second();
            if(s > 50) {
                int t = third();
                if(t > 50) {
                    cout << "successful!" << endl;
                }
            }
        }
    }
};

class InstallSoftware {
public:
    void installWizard(Wizard wizard) {
        wizard.installWizard();
    }
};

void test0() {
    InstallSoftware ins;
    Wizard wiz;
    ins.installWizard(wiz);
}

int main() {
    test0();
    return 0;
}

开闭原则(Open Close Principle)

Define:Software entities like classes,modules and functions should be open for extension but closed for modifications.(一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。)
变化可以分为逻辑变化,子模块变化,可见视图变化。
当发生变化时,开闭原则要求我们通过扩展实现实现变化。
重要性:
1 开闭原则对测试的影响,减少单元测试用例
2 开闭原则可以提高复用性
3 开闭原则可以提高可维护性
4 面向对象开发的要求
应用:
1 抽象约束
通过接口或抽象类约束扩展,对扩展进行边界限定,不允许出现在接口或抽象类中不存在的public方法
参数类型,引用对象尽量使用接口或抽象类而不是实现类
抽象层尽量保持稳定,一旦确定即不允许修改
2 元数据(metadata)控制模块行为
3 制定项目章程
4 封装变化
将相同的变化封装到一个接口或抽象类中
将不同的变化封装到不同的接口或抽象类中

#include <iostream>
#include <vector>

using namespace std;

class IBook {
public:
    virtual string getName() {}
    virtual int getPrice() {}
    virtual string getAuthor() {}
};

class NovelBook : public IBook {
public:
    NovelBook(string name, int price, string author) {
        mname = name;
        mprice = price;
        mauthor = author;
    }
    virtual string getName() {return mname;}
    virtual int getPrice() {return mprice;}
    virtual string getAuthor() {return mauthor;}
private:
    string mname;
    int mprice;
    string mauthor;
};

class offNovelBook : public NovelBook {
public:
    offNovelBook(string name, int price, string author) 
        :NovelBook(name, price, author) {
    }
    virtual int getPrice() {
        int selfPrice = NovelBook::getPrice();
        int offPrice = 0;
        if(selfPrice > 4000) {
            offPrice = selfPrice * 90 / 100;
        }else {
            offPrice = selfPrice * 80 / 100;
        }
        return offPrice;
    }
};

void test() {
    NovelBook *one = new offNovelBook("TLBB", 3200, "JY");
    NovelBook *two = new offNovelBook("BLSMY", 5600, "YG");
    NovelBook *three = new offNovelBook("BCSJ", 3500, "YG");
    NovelBook *four = new offNovelBook("JPM", 4300, "LLXXS");
    vector<IBook*> booklist = {one, two, three, four};
    cout << "sell books : " << endl;
    for(auto b : booklist) {
        cout << b->getName() << "\t" << b->getAuthor() << "\t" << b->getPrice() << endl;
    }

}

int main() {
    test();
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值