9-1.인터페이스가 다른 대용 클래스 Alternative Classes with Different Interfaces
[ 2017-08.30 작성 ]
1.
이 냄새는 다른 클래스의 두 메소드가 같은 일을하지만 다른 메소드 서명을 가질 때 발생합니다. 일관된 방식으로 동일한 작업을 수행하는 메소드의 이름 을 변경 해야합니다.
그러나 종종 두 클래스가 비슷한 것을하고있는 경우 더 깊은 문제가 있습니다. 당신은 시도 할 수 방법을 이동 주위 또는 공통의 슈퍼 클래스에 공통적 인 행동을 추출합니다.
출처 : http://wiki3.cosc.canterbury.ac.nz/index.php/Alternative_classes_with_different_interfaces_smell
sample code : old
PaperPrice
public class PaperPrice
{
int width = 0 ; // 넓이
int height = 0 ; // 높이
int sellUnit = 0 ; // 판매수량
int selLineCount = 0 ; // 롤 1줄 개수
public void SetInfo ( int width , int height , int sellUnit , int selLineCount )
{
this . width = width ;
this . height = height ;
this . sellUnit = sellUnit ;
this . selLineCount = selLineCount ;
}
public int GetPrice ()
{
if ( this . width == 0 || this . height == 0 ) { return 0 ; }
double cost = width * height * sellUnit ; // 원가
double marginPrice = cost * 0.25 ; // 마진
double consumptionTax = ( cost + marginPrice ) * 0.08 ; // 소비세율
int price = (( int ) Math . Ceiling ( consumptionTax / Convert . ToDouble ( 1000 ))); // 판매가
return price ;
}
}
StickerPrice
public class StickerPrice
{
int width = 0 ; // 넓이
int height = 0 ; // 높이
int sellUnit = 0 ; // 판매수량
int selLineCount = 0 ; // 롤 1줄 개수
public void SetInfo ( int width , int height , int sellUnit , int selLineCount )
{
this . width = width ;
this . height = height ;
this . sellUnit = sellUnit ;
this . selLineCount = selLineCount ;
}
public int GetPrice ()
{
if ( this . width == 0 || this . height == 0 ) { return 0 ; }
double cost = width * height * sellUnit ; // 원가
double marginPrice = cost * 0.25 ; // 마진
double consumptionTax = ( cost + marginPrice ) * 0.08 ; // 소비세율
int price = (( int ) Math . Ceiling ( consumptionTax / Convert . ToDouble ( 1000 ))); // 판매가
return price ;
}
}
PaperClass와 StickerClass가 동일을 행동을 하고 있습니다.
아래와 같이 슈퍼 클래스에 공통적인 행동을 추출합니다.
sample code : new
ProductGetPrice
public class ProductGetPrice
{
int width = 0 ; // 넓이
int height = 0 ; // 높이
int sellUnit = 0 ; // 판매수량
int selLineCount = 0 ; // 롤 1줄 개수
public void SetInfo ( int width , int height , int sellUnit , int selLineCount )
{
this . width = width ;
this . height = height ;
this . sellUnit = sellUnit ;
this . selLineCount = selLineCount ;
}
public int GetPrice ()
{
if ( width == 0 || height == 0 ) { return 0 ; }
double cost = width * height * sellUnit ; // 원가
double marginPrice = cost * 0.25 ; // 마진
double consumptionTax = ( cost + marginPrice ) * 0.08 ; // 소비세율
int price = (( int ) Math . Ceiling ( consumptionTax / Convert . ToDouble ( 1000 ))); // 판매가
return price ;
}
}
public class PaperPrice : ProductGetPrice { }
public class StickerPrice : ProductGetPrice { }
static void Main ( string [] args )
{
PaperPrice pp = new PaperPrice ();
pp . SetInfo ( 120 , 150 , 500 , 5 );
int ppPrice = pp . GetPrice ();
StickerPrice sp = new StickerPrice ();
sp . SetInfo ( 100 , 200 , 300 , 6 );
int spPrice = sp . GetPrice ();
}