加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

设计模式之依赖倒置原则 C++

发布时间:2020-12-13 23:12:38 所属栏目:百科 来源:网络整理
导读:定义:High level modules should not depend upon low level modules .Both modules .Both should depend on abstractions .Abstractions should not depend on details.Details should depend on abstractions. 采用依赖倒置原则可以减少类之间的耦合性,

定义:High level modules should not depend upon low level modules .Both modules .Both should depend on abstractions .Abstractions should not depend on details.Details should depend on abstractions.

采用依赖倒置原则可以减少类之间的耦合性,提高系统的稳定性,并降低并行开发引起的风险,提高代码的可读性和可维护性。

依赖的三种写法

1 构造函数传递依赖对象

public IDirver

{

virtual void dirve();

}

public Driver: public

{

public :

public Dirver(ICar _car);

virtual void drive();

private:

ICar *car;

}

2 Setter 方法传递依赖对象

3 接口声明依赖对象

public IDriver

{

public:

virtual void drive(ICar _car);

}

怎么在项目中使用

1,每个类尽量都有接口和抽象类,或者抽象类和接口两者都具备。、

2 尽量不要覆写基类的方法。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读