简体   繁体   English

在这种C ++设计方案-OOPS中,如何避免油脂过多/污染的接口?

[英]How to avoid the fat/polluted interface in this scenario of C++ design - OOPS?

We are writing a generic middleware for the gadget- DTV. 我们正在为小工具DTV编写通用中间件。 We have a module called ContentMgr, which is a base class. 我们有一个称为ContentMgr的模块,这是一个基类。 Now, for the different customer needs, we have the VideoconContentMgr ( for instance)- which is derived from ContentMgr. 现在,针对不同的客户需求,我们有VideoconContentMgr(例如),它来自ContentMgr。

class ContentMgr
{
  public:

virtual void setContent()
{
  cout<<"Unused function";
}

};

class VideoconContentMgr: public ContentMgr
{
  virtual void setContent()
  {
     cout<<"Do some useful computation;
  }

};

client code - based on the product type - the 客户代码-根据产品类型-

/** if the product is generic **/ ContentMgr *product = new ContentMgr(); / **如果产品是通用产品** / ContentMgr * product = new ContentMgr();

/** If the product is videocon **/ ContentMgr *product = new VideoconContentMgr(); / **如果产品是videocon ** / ContentMgr * product = new VideoconContentMgr();

My question is according to the Interface Segregation Principle - One should avoid fat/polluted interfaces. 我的问题是根据接口隔离原理-应该避免油脂过多/污染的接口。 How can I avoid the polluted method - setContent() here. 我如何避免受污染的方法-setContent()。 For the generic product,setContent() is not useful. 对于通用产品,setContent()无效。 However, for the videocon product it is useful. 但是,对于videocon产品,它很有用。 How can I avoid the fat/polluted method setContent()? 如何避免脂肪/污染方法setContent()?

For the generic product, setContent() is not useful. 对于通用产品, setContent()无效。 However, for the videocon product it is useful. 但是,对于videocon产品,它很有用。

One solution is to keep the member function where it is useful - put it into VideoconContentMgr only, and use it in the context specific to the VideoconContentMgr subtype: 一种解决方案是将成员函数保持在有用的位置-仅将其放入VideoconContentMgr ,并在特定于VideoconContentMgr子类型的上下文中使用它:

/** if the product is generic **/ ContentMgr *product = new ContentMgr();

/** If the product is videocon **/ {
    VideoconContentMgr *vcm = new VideoconContentMgr();
    vcm->setContent();
    ContentMgr *product = vcm;
}

If the usefulness of the member function specific to a subclass extends past the initialization time, use a visitor -like approach. 如果特定于子类的成员函数的有用性超出了初始化时间,请使用类似访问者的方法。 Subclass-specific code remains bound to subclasses, but now you add a visitor that does nothing for a generic class, and calls setContent() on VideoconContentMgr class: 特定于子类的代码仍然绑定到子类,但是现在您添加了对通用类不执行任何操作的访问者,并在VideoconContentMgr类上调用setContent()

struct ContentMgrVisitor {
  virtual void visitGeneric(ContentMgr& m) {};
  virtual void visitVideo(VideoconContentMgr& vm) {};
};

struct ContentMgr
{
  virtual void accept(ContentMgrVisitor& v)
  {
    v.visitGeneric(this);
  }
};

struct VideoconContentMgr: public ContentMgr
{
  virtual void setContent()
  {
     cout<<"Do some useful computation;
  }
  virtual void accept(ContentMgrVisitor& v)
  {
     v.visitVideo(this);
  }
};

Now a client who wants to call setContent can do it from a visitor: 现在,想要致电setContent的客户可以从访问者处进行操作:

class SetContentVisitor : public ContentMgrVisitor {
    void visitVideo(VideoconContentMgr& vm) {
        vm.setContent();
    }
};
...
ContentMgr *mgr = ... // Comes from somewhere
SetContentVisitor scv;
mgr->accept(scv);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM