我应严格遵守“有"的规定和“是"楷模? [英] How strictly should I observe "has a" and "is a" models?

查看:67
本文介绍了我应严格遵守“有"的规定和“是"楷模?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的事务类,它们都具有相同的数据,并且我认为我会为序列化的通用代码做些事情.

似乎最简单的方法是为两者创建一个通用的基类(这是中介类,因为它们都已经具有基类).这样,我要做的就是更改构造函数,而不必更改已使用公共变量的任何内容.一切都会神奇地工作.但是我不知道将来修改构造函数是否会带来管理问题.

如果我嵌入一个具有通用性的类,我相信我将不得不更改所有访问变量的位置.似乎有点麻烦.也许我错了.

假设数据是拥有形状的m_Circle和m_Parent.对于CAddCircleTransaction和CRemoveCircleTransaction,说有一个圆圈"要比是一个圆圈"听起来好一点,但这还不是很清楚.

我觉得很愚蠢,就像我已经花了太长时间编程以致无法问这样的基本问题了,但我无法决定.上一次,我将继承用于类似的事情,并且工作如此顺利.这次我似乎倾向于嵌入,但是继承的难易使我犹豫.我只能选择一种方法还是更好的方法?

I have two different transaction classes that both have the same data and I thought I''d do something about the common code for serialization.

It seems like the easiest way to do it would be to create a common base class for both (which would be intermediary since they both already have base classes). That way all I''d have to do is change the constructors and I wouldn''t have to change anything that already uses the common variables. Everything would magically work. But I wonder if modifying the constructors could have management issues in the future.

If I embedded a class that had the commonality, I believe I''d have to change all of the places that access the variables. It seems a little more cumbersome. Maybe I''m wrong.

Let''s say the data was a m_Circle and a m_Parent that owned the shape. For a CAddCircleTransaction and a CRemoveCircleTransaction, it sounds a little better to say "has a circle" than "is a circle" but it''s not entirely clear.

I feel silly, like I''ve been programming way too long to asking such basic question but I can''t decide. Last time I used inheritance for a similar thing and it worked so smoothly. This time I seem to leaning towards embedding but the ease of inheritance is making me hesitate. Can I just choose or is one way better?

推荐答案

我不确定我是否完全理解您的问题,但是我认为,如果您使用的是其他类型的工具,关于事务,我几乎可以肯定地说,它们全部都继承自通用的抽象基类.如果您的事务在某种程度上类似于您的示例,那么我会在层次结构中添加一个附加类(因此,您将继承CTransaction,然后继承CCircleTransaction,然后继承CCircleTransaction和CAddCircleTransaction和CRemoveCircleTransaction).
关于有一个圆圈"和是一个圆圈"的句子,您的说法有点错误,因为您不是继承自circle类而是继承自CCircleTransaction. CAddCircleTransaction 确实是也是CCircleTransaction.

也许我不太清楚您的问题,所以在这种情况下,请尝试重新措辞.
I''m not sure I fully understand your problem but in my opinion, if you are working with different kind of transactions, I would almost certainly make them all inherit from a common abstract base class. In case you have transactions which are, like in your example, similar in some way, I would add an additional class in the hierarchy (so, you would have CTransaction then CCircleTransaction inheriting from it and then CAddCircleTransaction and CRemoveCircleTransaction both inheriting from CCircleTransaction).
You were a bit wrong in your sentence about "has a circle" and "is a circle" since you do not inherit from the circle class but from the CCircleTransaction. And a CAddCircleTransaction is indeed also a CCircleTransaction.

Maybe I didn''t clearly understand your question, so if that''s the case please try to rephrase it.


可能是一个开始:):
It could be a possible begin :) :
class CShape : public CObject
{
..
};

class CCircle : public CShape
{
..
};

class CTransaction : public CObject
{
..
};

template <typename T>
class CShapeTransaction : public CTransaction
{
  T* m_ptShape;
..
public:
..
  T* GetShape() { return m_ptShape; }
..
};

class CAddCircleTrasaction : public CShapeTransaction<CCircle>
{
..
};

class CRemoveCircleTrasaction : public CShapeTransaction<CCircle>
{
..
};


您在一开始就说过,两个对象具有相同的数据.它们是数据的所有者,因此,他们拥有此数据,但不是该数据.

如果transactionData是两者之间常用的数据,那么我将编写一个名为transactionData的类,然后编写另一个名为transactionOperator的抽象类.

实现最基本的通用功能(即,它具有transactionData,访问器,最简单的操作),然后例如编写一个纯虚函数:: addTransaction().

然后,在该类的继承版本中,您可以实现他们想要处理事务的不同方式.

根据给定的信息,只有我的两分钱.

不,没有一条规则可以说一种方法或另一种方法是更好的答案.

查找组成与继承.他们都有自己的优缺点.
You said it in the beginning, both objects have the same data. They are owner''s of the data, and as a result they HAVE this data, but they are not that data.

If transactionData is the data used commonly between the two, I''d write a class called transactionData, then another abstract class called transactionOperator.

Implement the most base common functionality (i.e, it has transactionData, the accessors, the most simple of operations) and then, for example, write a pure virtual function ::addTransaction().

Then in inherited versions of the class, you can implement the different ways they want to handle the transaction.

Just my two cents based on the info given.

And no, there isn''t a rule that says one way or the other is a better answer.

Look up composition vs inheritence. They both have their pluses and minuses.


这篇关于我应严格遵守“有"的规定和“是"楷模?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆