C++ 的一些设计问题;

发布于 2024-11-02 00:30:11 字数 719 浏览 0 评论 0原文

我在这里遇到以下困境:我有几个类,比方说 A、B、C 和 D。A 有一个公共接口,并且与 B 存在关系(就像 A 有一个 B 类型的成员变量)并且A 的方法之一返回这个 B 对象,B 只是一个公开一些方法的类,C 是另一个公开其他方法的类,D 是一个单例对象。 D 的公共接口具有对类 C 的对象的引用(如果您更喜欢,则为指针)。

因此,显然,当我想在这一步绘制关系图时,我将在 A 和 B 之间建立关系,并且将 C 放在该图与其他两个没有明显的关系。所以,这是基于头文件(.h),其中包含类 A、B、C 的声明。我现在对 D 有点困惑。

另一方面:

  1. A 和 B 的实现(在 .cpp 文件中)都严重依赖于从类 C 创建的对象(不,C 不是标准的东西,例如列表、字符串、队列,而是另一个有意义的类)我的申请)。
  2. A 和 B 的实现都使用 D 单例和本地 C 对象。

我的问题是:

  1. 我应该在 A、B、C 和 D 之间的类图上放置什么关系,不包括我已经确定的关系(A 有一个 B)?我对单例 D 与类 C 的关系特别感兴趣。
  2. 这种情况下普遍接受的方法是什么(当接口在对象之间没有关系时,因为没有关系,但在实现中它们被大量使用) ?
  3. 如果我根据Java而不是C++有同样的问题会有区别吗(因为在java中与类相关的所有内容都在一个文件中,因此更容易看到类方法实际使用的内容,而在C++中你通常只看标题)。

非常感谢您的指导。

I have the following dilemma here: I have a few classes, let's say A, B, C and D. A has a public interface, and a has-a relationship with B (like A is having a member variable of type B) and one of the methods of A is returning this B object, B is just a class who exposes some methods, C is another class which exposes other methods, and D is a singleton object. The public interface of D has references (pointers if you like more) to objects of class C.

So, obviously when I want to draw a relationship diagram at this step, I would have a relationship between A and B and C would be put on the diagram, without visible relationship to the other two. So, this is based on the header (.h) files, which contain the declaration of class A, B, C. I'm a little confused about D right now.

On the other end:

  1. both the implementations (in the .cpp files) of A and B are heavily dependent on objects created from class C (no, C is not something standard, such as list, string, queue, but another meaningful class in my application).
  2. both the implementations of A and B use the D singleton with the local C objects.

And here are my questions:

  1. What relationships should I put on the class diagram between A, B, C and D, not counting the one I have identified (A has-a B)? I'm particularly interested in the singleton D's relationship to class C.
  2. What is the generally accepted methodology for this kind of situations (when the interface is not having relationships between objects, because there are none, but in the implementation they are heavily used)?
  3. Would there be a difference if I would have the same question in accordance to Java and not C++ (because in java everything related to a class is in one file, so it's easier to see what a class method is actually using, while in C++ you usually just see the header).

Thanks a lot for your guidance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末蓝 2024-11-09 00:30:12

您提供的大部分信息应该可以在以下课程中识别
图表(plantuml 输入)。我希望这能回答第一个问题。

@startuml
class A
A o--> B
A : + method()
A : + B& getB()
A : - B m_B
A --> "getC" D 

class B
B : + method()
B --> "getC" D 

class C
C : + method()

class D <<Singleton>>
D --> "0..n" C
D : + C* getC( int index )
D : - list<C> m_containerOfC

@enduml

关于第二个问题:我认为绘制UML图(我想是为了设计)的目的主要是抽象,因此忽略了细节。编写完程序后,尝试用 UML 表达完整的 C++ 程序是没有意义的。您可以购买(尝试)为您执行此操作的程序,但我认为这些图表没有用。

第三个问题的答案是,在设计阶段,java 和 c++ 实现的 UML 应该是相同的,或者至少在很大程度上是相同的。设计就是选择和连接设计模式等,而这些都是独立于语言的。当您开始详细说明图表以表示更多实现细节(例如使用的容器类型等)时,所选的实现语言就会发挥作用。然而,在那个阶段,您应该问自己,您的图表是否让您对您的设计有足够的信心,然后开始编码。

Most of the information you provide should be recognizable in the following class
diagram(plantuml input). I hope that answers the first question.

@startuml
class A
A o--> B
A : + method()
A : + B& getB()
A : - B m_B
A --> "getC" D 

class B
B : + method()
B --> "getC" D 

class C
C : + method()

class D <<Singleton>>
D --> "0..n" C
D : + C* getC( int index )
D : - list<C> m_containerOfC

@enduml

Regarding the second question: I think the point of drawing UML diagrams (I suppose for designing) is mostly about abstracting, hence ignoring details. There is no point in trying to express a complete C++ program in UML after you have written the program. You can buy programs that (attempt to) do that for you but I don't think these diagrams are useful.

The answer to your third question is that at a design stage the UML for a java and a c++ implementation should be equal, or at least for a large proportion. Designing is about choosing and connecting design patterns etc., and these are language independent. When you start detailing your diagrams to represent more implementation details (e.g. the type of containers used etc.) then the chosen language for implementation comes into play. However, at that stage you should ask yourself whether your diagram gives you enough confidence in your design and then start coding it.

酒绊 2024-11-09 00:30:12

您绝对应该阅读大规模 C++ 软件设计这本书。

它通过引入两个新的关系uses-in-the-interfaceuses-in-the-implementation来特别处理接口和实现之间依赖关系的建模,而不是只是传统的“有一个”。

然后,继续讨论应用于此类建模的设计原则(例如隔离绝缘封装等)。不过,这确实是一本技术性很强的书。所以要做好准备!

You should definitively read the book Large-Scale C++ Software Design.

It particularly deals with the modeling of dependencies between interfaces and implementation, by introducing two new relationships uses-in-the-interface and uses-in-the-implementation, instead of just the traditional "has-a".

Then, it goes on with design principles applied to such modeling (such as isolation, insulation, encapsulation, etc.). It is really a highly technical book, though. So be prepared!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文