这个问题的要点是扩展一个类,最大限度地减少堵塞 - 将所有内容打包到一个类中,并最大化代码重用.阅读完这个问题后,请随时编辑标题或说明,使其更加简洁.虽然帖子看起来很长,但我只是想通过使用大量的例子来彻底解决.
假设我有一个班级:
class UsedByManyPeople
{
// ...has many fields
};
Run Code Online (Sandbox Code Playgroud)
顾名思义,这个类被许多开发人员使用.我必须为这个类添加2个功能:
它们都是我部门需要的.
起初我想过简单地向UsedByManyPeople添加两个新方法.因此,该类现在看起来像:
class UsedByManyPeople
{
// ...has many fields
public:
SomeOtherType const convert() const;
std::string const getFileName() const;
};
Run Code Online (Sandbox Code Playgroud)
但是,这2个功能实际上是特定于我部门的用例,而其他部门甚至没有SomeOtherType的类定义,也不关心getFileName().
显然,上述方法不是一个好方法(?).
你会如何扩展这门课程?
我想到的替代方案:
例如,
class ExtUsedByManyPeople : public UsedByManyPeople
{
public:
SomeOtherType const convert() const;
std::string const getFileName() const;
};
Run Code Online (Sandbox Code Playgroud)
例如,
class UsedByManyPeopleToSomeOtherTypeConverter
{
public:
static SomeOtherType const convert(UsedByManyPeople const&);
};
class UsedByManyPeopleFileName
{
public:
static std::string const getFileName(UsedByManyPeople …Run Code Online (Sandbox Code Playgroud)