Class refactoring in C ++

I have a class A that implements many functions. Class A is very stable.

Now I have a new requirement for functions, some of which correspond to functions A. I cannot directly inherit my new class from class A, as this will bring a lot of redundancy to my new class.

So, should I duplicate common code in both classes?

Or, should I create a new base class and transfer the common code to the base class and deduce from it class A and the new class? But this will lead to changes in my existing class.

So what would be a better approach?

+5
source share
3 answers

A, (, , , , ).

, . - , . A .

, , , , , , , . , . , , , . , , , , .

+16

(1) , .

(2) , . , , "Extract SuperClass" , "Pull Up Method" , . .

: : . "", " ". , , ( ).

+8

, , ( "IS-A" ).

. B - - A. B A A. , A (, , , , A).

, . "Replace Inheritance with Delegation" , .

+1

All Articles