不同类型的复合图案重用 [英] Composite Pattern Reuse For Different Types

查看:287
本文介绍了不同类型的复合图案重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

扩展海军模拟项目,我想添加复合模式,所以我可以有对象组。这是当前层次结构的样子:

  class Sim_object {
};

class Ship:public Sim_object {
};

class Island:public Sim_object {
};

到目前为止,我想出了:

  class Sim_object {
//保存通用sim函数
get_name()
};

// COMPONENT
class Ship_object:public Sim_object {
//保存船舶函数
add()
remove()
攻击)
move()
};

// COMPOSITE
class Group:public Ship_object {
//保存组船舶函数列表
add()
remove()
attack()
move()
};

// LEAF
class Ship:public Ship_object {
//保存船舶函数
attack()
move()
};

class Island:public Sim_object {
//保存岛函数
};

我有它的工作,但它只适用于船舶和船组。我想重新使用Group代码(添加,删除,显示等),所以一个具体的Group类(如Island_group)只能继承这个并使用它。我尝试改变设计,Ship_group类继承自一个共同的基类,称为Group,但我面临一个钻石程序。



有什么办法我可以去重复使用我的组控制代码基于类型?

解决方案

复合,为了使用多种类型 Sim_object ,应继承并撰写 Sim_objects

 code> class Sim_object {}; 
class Ship:public Sim_object {};
class Island:public Sim_object {};
class Group:public Sim_object {
add()
remove()
};

要将其定制为特定类型,您可以对其进行模板化, :

 模板< class T& 
class Group:public Sim_object {
add()
remove()
};

类Ship_Group:public Group< Ship> {
attack()
move()
};

class Island_Group:public Group< Island> {} //可能不必要

现在,让 Ship_Group 代替运输,您可以为运输创建一个接口(纯虚拟类)继承 Ship_Group 。您也可以使用新界面,而不是客户端代码中的具体 Ship

  class Ship_Like {// interface 
attack()= 0
move()= 0
};
class Ship:public Sim_object,public Ship_Like {}; // concrete
class Ship_Group:public Group< Ship> public Ship_Like {
attack()
move()
}
/ pre>

不用担心钻石问题...


Extending a naval simulation project, I'm looking to add the Composite Pattern so I can have groups of objects. This is what the current hierarchy looks like:

class Sim_object {
};

class Ship : public Sim_object {
};

class Island : public Sim_object {
};

And so far I've come up with:

class Sim_object {
  // holds general sim functions
  get_name()
};

// COMPONENT
class Ship_object : public Sim_object {
  // holds ship functions
  add()
  remove()
  attack()
  move()
};

// COMPOSITE
class Group : public Ship_object {
   // holds list of group ship functions
   add()
   remove()
   attack()
   move()
};

// LEAF
class Ship : public Ship_object {
   // holds ship functions
   attack()
   move()
};

class Island : public Sim_object {
  // holds island functions
};  

I have it working, but it only works for Ships and groups of Ship. I'd like to reuse the Group code (add, remove, display, etc.) so then a concrete Group class (like Island_group) can just inherit from this and use it. I tried changing the design where Ship_group class inherits from a common base class called Group, but I was faced with a diamond program.

Is there any way I can go about reusing my Group control code based on the type?

解决方案

The composite, in order to work with more than one type of Sim_object, should inherit and compose Sim_objects:

class Sim_object { };
class Ship : public Sim_object { };
class Island : public Sim_object { }; 
class Group : public Sim_object { 
  add()
  remove()
}; 

To tailor it to specific types, you could templatize it, and (possibly) derive other groups from it:

template <class T>
class Group : public Sim_object {
  add()
  remove()
};

class Ship_Group : public Group<Ship> {
  attack()
  move()
};

class Island_Group : public Group<Island> { } // possibly unnecessary

Now, to have your Ship_Group substitute Ships, you could create an "interface" (pure-virtual class) for Ship and inherit Ship_Group from it as well. You'd also use your new interface instead of the concrete Ship in client code:

class Ship_Like { // interface
  attack() = 0
  move() = 0
};
class Ship : public Sim_object, public Ship_Like { }; // concrete
class Ship_Group : public Group<Ship>, public Ship_Like { 
  attack()
  move()
}

No worries about diamond-problems there...

这篇关于不同类型的复合图案重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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