how to: extract interface of class with methods from a superclass
"Donaldson, John" <[email protected]> Fri, 20 Sep 2013 16:55:08 +0000
| Newsgroups | gmane.comp.programming.refactoring |
|---|---|
| Message-ID | <716D07F3CBA94244A0922E7A99837C3A109FB62E@G5W2715.americas.hpqcorp.net> |
--_000_716D07F3CBA94244A0922E7A99837C3A109FB62EG5W2715americas_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
I have a class (legacy code) - let's call it C, which uses methods on class=
B.
To make testing easier I could "extract interface" of B and create a test f=
ake of it.
Except, class B inherits class A and C is calling one of the inherited meth=
ods.
Something like this:
Class A{
MethodOfA(){ ... }
}
Class B inherits A{ ... }
Class C{
B myB;
MethodOfC(){
myB.MethodOfA();
}
}
Quite rightly "extract interface" on B only gives me the methods on B.
So, how to separate C from B abd A for testing purposes?
What's the good/correct/elegant refactoring?
John D.
--_000_716D07F3CBA94244A0922E7A99837C3A109FB62EG5W2715americas_
Content-Type: text/html; charset=US-ASCII
Content-Transfer-Encoding: 7bit
<head>
<style type="text/css">
<!--
/* start of attachment style */
.ygrp-photo-title{
clear: both;
font-size: smaller;
height: 15px;
overflow: hidden;
text-align: center;
width: 75px;
}
div.ygrp-photo{
background-position: center;
background-repeat: no-repeat;
background-color: white;
border: 1px solid black;
height: 62px;
width: 62px;
}
div.photo-title
a,
div.photo-title a:active,
div.photo-title a:hover,
div.photo-title a:visited {
text-decoration: none;
}
div.attach-table div.attach-row {
clear: both;
}
div.attach-table div.attach-row div {
float: left;
/* margin: 2px;*/
}
p {
clear: both;
padding: 15px 0 3px 0;
overflow: hidden;
}
div.ygrp-file {
width: 30px;
valign: middle;
}
div.attach-table div.attach-row div div a {
text-decoration: none;
}
div.attach-table div.attach-row div div span {
font-weight: normal;
}
div.ygrp-file-title {
font-weight: bold;
}
/* end of attachment style */
-->
</style>
</head>
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri","sans-serif";
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri","sans-serif";}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<!-- |**|begin egp html banner|**| -->
<br><br>
<!-- |**|end egp html banner|**| -->
<div class="WordSection1">
<p class="MsoNormal">I have a class (legacy code) – let’s call it C, which uses methods on class B.<o:p></o:p></p>
<p class="MsoNormal">To make testing easier I could “extract interface” of B and create a test fake of it.<o:p></o:p></p>
<p class="MsoNormal">Except, class B inherits class A and C is calling one of the inherited methods.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Something like this:<o:p></o:p></p>
<p class="MsoNormal"> Class A{<o:p></o:p></p>
<p class="MsoNormal"> MethodOfA(){ … }<o:p></o:p></p>
<p class="MsoNormal"> }<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> Class B inherits A{ … }<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> Class C{<o:p></o:p></p>
<p class="MsoNormal"> B myB;<o:p></o:p></p>
<p class="MsoNormal"> MethodOfC(){<o:p></o:p></p>
<p class="MsoNormal"> myB.MethodOfA();<o:p></o:p></p>
<p class="MsoNormal"> }<o:p></o:p></p>
<p class="MsoNormal"> }<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Quite rightly “extract interface” on B only gives me the methods on B.<o:p></o:p></p>
<p class="MsoNormal">So, how to separate C from B abd A for testing purposes?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">What’s the good/correct/elegant refactoring?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">John D.<o:p></o:p></p>
</div>
<!-- |**|begin egp html banner|**| -->
<br>
<br>
<!-- |**|end egp html banner|**| -->
<div width="1" style="color: white; clear: both;"/>__._,_.___</div>
<!-- Start Recommendations -->
<!-- End Recommendations -->
<!-- |**|begin egp html banner|**| -->
<img src="http://geo.yahoo.com/serv?s=97476590/grpId=3975602/grpspId=1707276718/msgId=10694/stime=1379696193" width="1" height="1"> <br>
<!-- |**|end egp html banner|**| -->
<!-- |**|begin egp html banner|**| -->
<br>
<div style="font-family: verdana; font-size: 77%; border-top: 1px solid #666; padding: 5px 0;" >
Your email settings: Individual Email|Traditional <br>
<a href="http://groups.yahoo.com/group/refactoring/join;_ylc=X3oDMTJmdTlyMzg4BF9TAzk3NDc2NTkwBGdycElkAzM5NzU2MDIEZ3Jwc3BJZAMxNzA3Mjc2NzE4BHNlYwNmdHIEc2xrA3N0bmdzBHN0aW1lAzEzNzk2OTYxOTM-">Change settings via the Web</a> (Yahoo! ID required) <br>
Change settings via email: <a href="mailto:[email protected]?subject=Email Delivery: Digest">Switch delivery to Daily Digest</a> | <a href = "mailto:[email protected]?subject=Change Delivery Format: Fully Featured">Switch to Fully Featured</a> <br>
<a href="http://groups.yahoo.com/group/refactoring;_ylc=X3oDMTJkZm11NDZkBF9TAzk3NDc2NTkwBGdycElkAzM5NzU2MDIEZ3Jwc3BJZAMxNzA3Mjc2NzE4BHNlYwNmdHIEc2xrA2hwZgRzdGltZQMxMzc5Njk2MTkz">
Visit Your Group
</a> |
<a href="http://info.yahoo.com/legal/us/yahoo/utos/terms/">
Yahoo! Groups Terms of Use
</a> |
<a href="mailto:[email protected]?subject=Unsubscribe">
Unsubscribe
</a>
<br>
</div>
<br>
<!-- |**|end egp html banner|**| -->
<div style="color: white; clear: both;"/>__,_._,___</div>
</body>
</html>
--_000_716D07F3CBA94244A0922E7A99837C3A109FB62EG5W2715americas_--