类设计建议:扩展类和代码重用 [英] Class design suggestions: extending a class and code reuse

查看:112
本文介绍了类设计建议:扩展类和代码重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题的要点是扩展类,最小化把一切都包装到一个类中,并最大化代码重用。阅读此问题后,请随意编辑标题或说明,使其更简洁。虽然帖子看起来很长,但我只是想通过使用大量的例子来彻底。



假设我有一个类:

  class UsedByManyPeople 
{
// ...有很多字段
};

顾名思义,这个类被很多开发人员使用。我必须为此类添加2个功能:


  1. 将UsedByManyPeople转换为SomeOtherType的convert()

  2. 一个返回字符串的getFileName()

这两个都是针对我部门的需求。






第一次尝试解决方案



刚开始,我想到只需向UsedByManyPeople添加两个新方法。因此,类将如下所示:

  class UsedByManyPeople 
{
// ... has许多字段

public:
SomeOtherType const convert()const;
std :: string const getFileName()const;
};

但是,这2个功能实际上是针对我部门的用例,其他部门甚至没有SomeOtherType的类定义,也不关心getFileName()。



显然,上述方法不是一个好方法(?)。



您将如何扩展这个类?



我觉得的替代方法: b
$ b




子类UsedByManyPeople并创建自己的类。




  • 一起绑定数据和方法



例如,

  class ExtUsedByManyPeople:public UsedByManyPeople 
{
public:
SomeOtherType const convert()const;
std :: string const getFileName()const;
};






创建帮助程序类,每个方法一个




  • 从方法中分离数据,单类一个责任



例如,

  class UsedByManyPeopleToSomeOtherTypeConverter 
{
public:
static SomeOtherType const convert(UsedByManyPeople const&);
};
class UsedByManyPeopleFileName
{
public:
static std :: string const getFileName(UsedByManyPeople const&);
};






创建一个单独的Helper类, >


  • 从方法中分离数据,单一类别的许多职责



例如

  class UsedByManyPeopleHelper 
{
public:
static SomeOtherType const convert(UsedByManyPeople const&);
static std :: string const getFileName(UsedByManyPeople const&);
};


解决方案

特别是如果方法特定于您的部门使用



有几个原因可以解决这个问题:




  • 单一辅助类可以是位于另一个逻辑项目中的

    结构

  • 如果你的新方法不需要访问类的内部状态,这是清楚地表达。它提供更好的封装。 (你补充了const ref,这是一个好的风格)

  • UsedByManyPeople 不应该
    负责用于将自身
    转换为另一种类型。这违反了
    SOLID


The gist of this question is about extending a class, minimizing jam-packing everything into a single class, and maximizing code re-use. After reading this question, please feel free to edit the title or description to make it more succinct. Though the post looks long, I am just trying to be thorough by using a lot of examples.

Suppose I have a class:

class UsedByManyPeople
{
  // ...has many fields
};

As the name implies, this class is used by a lot of developers. I have to add 2 features to this class:

  1. a convert() that converts UsedByManyPeople to SomeOtherType
  2. a getFileName() that returns a string

Both of them are specific to my department's needs.


First attempt solution

At first I thought about simply adding 2 new methods to UsedByManyPeople.Thus, the class will now look like:

class UsedByManyPeople
{
  // ...has many fields

  public:
    SomeOtherType const convert() const;
    std::string   const getFileName() const;
};

However, the 2 features are actually specific to my department's use case, and other departments do not even have the class definition of SomeOtherType nor do they care about the getFileName().

Clearly, the above approach is not a good approach (?).

How would you extend this class?

Alternatives that came to my mind:


Subclass UsedByManyPeople and create my own class.

  • Tie data and method together

For example,

class ExtUsedByManyPeople : public UsedByManyPeople
{
  public:
    SomeOtherType const convert() const;
    std::string   const getFileName() const;
};


Create Helper classes, one for each method (yikes!), and implement it as static methods.

  • Separate data from methods, single class one responsibility

For example,

class UsedByManyPeopleToSomeOtherTypeConverter
{    
  public:
    static SomeOtherType const convert(UsedByManyPeople const&);
};
class UsedByManyPeopleFileName
{
  public:
    static std::string const getFileName(UsedByManyPeople const&);
};


Create a single Helper class, with all the methods inside.

  • Separate data from methods, single class many responsibilities

For example,

class UsedByManyPeopleHelper
{
  public:
    static SomeOtherType const convert(UsedByManyPeople const&);
    static std::string   const getFileName(UsedByManyPeople const&);
};

解决方案

Especially if the methods are specific to your departments use of the class you should implement them as in: Create a single Helper class, with all the methods inside.

There are several reasons for this:

  • The single helper class can be located in another logical project structure
  • If your new methods don't require access to the internal state of the class this is expressed clearly. It provides better encapsulation. (which you supplemented by const ref, which is think is a good style)
  • UsedByManyPeople shouldn't be responsible for converting itself into another type. This violates SOLID

这篇关于类设计建议:扩展类和代码重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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