简体   繁体   English

用C ++重构一个类

[英]Refactoring a class in C++

I have a class A which implements many functions. 我有一个实现许多功能的A类。 Class A is very stable. A级非常稳定。

Now I have a new feature requirement, some of whose functionality matches that implemented by A. I cannot directly inherit my new class from class A, as that would bring lot of redundancy into my new class. 现在我有一个新的功能要求,其中一些功能与A实现的功能匹配。我不能直接从A类继承我的新类,因为这会给我的新类带来很多冗余。

So, should i duplicate the common code in both the classes? 那么,我应该在两个类中复制公共代码吗?

Or, should i create a new base class and move the common code to base class, and derive class A and the new class from it? 或者,我应该创建一个新的基类并将公共代码移动到基类,并从中派生出A类和新类? But this will lead to changes in my existing class. 但这会导致我现有课程的变化。

So, which would be a better approach? 那么,哪种方法更好?

Unless there is a very good reason not to modify class A, refactor and make a common base (or even better, a common class that both can use, but not necessarily derive from). 除非有一个很好的理由不修改A类,重构并建立一个共同的基础(甚至更好,一个既可以使用但又不一定来自的公共类)。

You can always use private inheritance to gain access to the shared functionality without modifying class As external interface - this change would require a rebuild, but nothing more. 您始终可以使用私有继承来访问共享功能,而无需修改类作为外部接口 - 此更改将需要重建,但仅此而已。 Leave all the functions on class A, and just have them forward to the shared implementation class. 将所有函数保留在A类中,并将它们转发到共享实现类。

One reason you might not want to refactor, but rather copy the code is if it's likely that the new class' functionality will change, but without the same change being needed in the old class. 您可能不想重构,而是复制代码的一个原因是,新类的功能可能会发生变化,但旧类中不需要进行相同的更改。 One of the reasons for not duplicating code is so that a fix or a change needs only to be made in one place. 不复制代码的原因之一是只需要在一个地方进行修复或更改。 If changes are going to happen that will break the original class, then maybe you want to copy the code instead. 如果要发生会破坏原始类的更改,那么您可能希望复制代码。 Although in most cases, this will only happen if the two classes aren't as similar as you thought, and you'd be fighting to try and abstract a common set of functionality. 虽然在大多数情况下,只有当这两个类没有您想象的相似时才会发生这种情况,并且您将努力尝试抽象一组通用功能。

(1) Do not duplicate the common code in both classes unless you or your employer are prepared to maintain both copies indefinitely. (1)除非您或您的雇主准备无限期地保留两份副本,否则不要在两个类别中复制共同代码。

(2) Refactoring by definition changes classes you already have. (2)按定义重构会更改您已有的类。 The refactor you propose is called "Extract SuperClass" followed by "Pull Up Method" for each method that is common to the derived classes. 您建议的重构称为“Extract SuperClass”,后跟“Pull Up Method”,用于派生类通用的每个方法。 This is a fine approach. 这是一个很好的方法。

Edit: I remembered the real gem behind Refactoring: Within reason it is perfectly fluid and reversible. 编辑:我记得Refactoring背后的真正宝石:在合理的范围内,它是完美的流畅和可逆的。 There is never a "Right" there is only a "Right for now". 从来没有一个“权利”只有一个“现在的权利”。 If you decide later that using inheritance for these classes isn't a good object model then you can refactor again to use composition (as superbly suggested by Josh). 如果您稍后决定对这些类使用继承不是一个好的对象模型,那么您可以再次重构以使用组合(正如Josh所建议的那样)。

Classes should share a common base class if you intend the classes to be substitutable (the "IS-A" relationship). 如果您希望类是可替换的(“IS-A”关系),则类应该共享一个公共基类。

You might consider composition. 你可以考虑组成。 Your new class B could be a wrapper class around a private member variable of type class A. Class B could augment class A with new methods or logic without changing class A. This is convenient if you can't change class A (eg it is closed source software, owned by another team, or you are afraid to break compatibility with existing code using class A). 您的新类B可以是类A类的私有成员变量的包装类。类B可以使用新方法或逻辑来增加类A而不更改类A.如果您不能更改类A(例如它是闭源软件,由另一个团队拥有,或者您害怕使用A类破坏与现有代码的兼容性。

For a nice diagram, see "Replace Inheritance with Delegation" refactoring pattern. 有关一个不错的图表,请参阅“使用委派替换继承”重构模式。

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

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