gpt4 book ai didi

c++ - 固体 : Does DIP mean that composition/aggreation to an object is forbidden?

转载 作者:搜寻专家 更新时间:2023-10-31 01:33:53 25 4
gpt4 key购买 nike

我正在尝试理解和应用 SOLID 原则。关于依赖倒置原则,这是否意味着禁止对对象进行组合/聚合?因此必须始终使用接口(interface)来访问另一个类方法?

我的意思是:

class ServiceClass {
void serviceClasshelper();
}

class MainClass {
void MainClass(ServiceClass service); // To use serviceClasshelper
}

必须改为:

class ServiceInterface {
virtual void interfaceHelper() =0;
}

class ServiceClass : public ServiceInterface {
void serviceClasshelper();
void interfaceHelper() { serviceClasshelper(); };
}

class MainClass {
void MainClass(ServiceInterface service); // Uses interfaceHelper
}

我认为(或者至少我希望)我理解这个原理。但是想知道是否可以这样改写。事实上,我读到的有关 DIP 的文章建议使用接口(interface)。

谢谢!

最佳答案

基本上,DIP 的主要思想是:

  • High-level modules should not depend on low-level modules. Both should depend on abstractions.
  • Abstractions should not depend on details. Details should depend on abstractions.

如您所见,它说的是应该而不是必须。它不禁止你做任何事情。

如果您的类组合/聚合到其他特定的(不是接口(interface)/抽象类),那很好!您的代码仍会编译和运行,不会显示任何警告告诉您:“嘿,您违反了 DIP”。所以我认为您的问题的答案是:不,不是

假设您的系统由一千个类组成,您可以只将 DIP 应用于 2 个类,并让其中一个依赖于第三个特定类(不是接口(interface)/抽象类)。只要你的问题解决了,什么都不重要。因此,请尽量使您的解决方案简短明了 -> 易于理解。相信我,当您在 1 个月后回顾您的解决方案时,您会发现它很有值(value)。

DIP 是一个指南,它告诉您在遇到一组特定问题时该怎么做才能解决这些问题。这不是一个神奇的指南,它是有代价的:复杂性。您应用 DIP 的次数越多,您的系统就会越复杂。所以明智地使用它。为了进一步支持这一点,我建议您查看此引用资料(摘自 Head First: Design Patterns 一书)。访问此 link (它是一个 PDF 文件)并在顶部栏导航到页面 635/681。或者,如果您足够懒惰,只需阅读以下引述:

Your Mind on Patterns

The Beginner uses patterns everywhere. This is good: the beginner gets lots of experience with and practice using patterns. The beginner also thinks, “The more patterns I use, the better the design.” The beginner will learn this is not so, that all designs should be as simple as possible. Complexity and patterns should only be used where they are needed for practical extensibility.

As learning progresses, the Intermediate mind starts to see where patterns are needed and where they aren’t. The intermediate mind still tries to fit too many square patterns into round holes, but also begins to see that patterns can be adapted to fit situations where the canonical pattern doesn’t fit.

The Zen mind is able to see patterns where they fit naturally. The Zen mind is not obsessed with using patterns; rather it looks for simple solutions that best solve the problem. The Zen mind thinks in terms of the object principles and their trade-offs. When a need for a pattern naturally arises, the Zen mind applies it knowing well that it may require adaptation. The Zen mind also sees relationships to similar patterns and understands the subtleties of differences in the intent of related patterns. The Zen mind is also a Beginner mind — it doesn’t let all that pattern knowledge overly influence design decisions.

最后,我将向您介绍一个使用 DIP 的四人组设计模式:Strategy

示例问题:一个角色可以使用3种武器:。他(角色)可以随时更换他当前的武器。

分析:这是一个很典型的问题。棘手的部分是如何在运行时处理武器交换。

候选解决方案与策略:(只是草图):

enter image description here

weapon = new Hand();
weapon.Attack(); // Implementation of Hand class

weapon = new Sword();
weapon.Attack(); // Implementation of Sword class

weapon = new Gun();
weapon.Attack(); // Implementation of Gun class

其他使用 DIP 的设计模式和框架:

关于c++ - 固体 : Does DIP mean that composition/aggreation to an object is forbidden?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40573392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com