重构 -- Replace Subclass with Fields(以值域取代子类)
来源:互联网 发布:node.js 数据库 编辑:程序博客网 时间:2024/05/29 08:07
动机(Motivation)
修改这些函数,使它们返回superclass中的某个(新增值域,然后销毁subclasses)
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public
abstract
class
Person
{
public
abstract
bool
IsMale();
public
abstract
string
GetCode();
public
Person CreateMale()
{
return
new
Male();
}
public
Person CreateFemale()
{
return
new
Female();
}
}
public
class
Male : Person
{
public
override
bool
IsMale()
{
return
true
;
}
public
override
string
GetCode()
{
return
"M"
;
}
}
public
class
Female : Person
{
public
override
bool
IsMale()
{
return
false
;
}
public
override
string
GetCode()
{
return
"F"
;
}
}
改为
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public
class
Person
{
private
bool
_IsMale;
private
string
_Code;
public
bool
IsMale
{
get
{
return
_IsMale; }
set
{ _IsMale = value; }
}
public
string
Code
{
get
{
return
_Code; }
set
{ _Code = value; }
}
public
Person(
bool
isMale,
string
code)
{
this
._IsMale = isMale;
this
._Code = code;
}
}
public
class
Male : Person
{
public
Male()
:
base
(
true
,
"M"
)
{ }
}
public
class
Female : Person
{
public
Female()
:
base
(
false
,
"F"
)
{ }
}
0 0
- 重构 -- Replace Subclass with Fields(以值域取代子类)
- Replace Subclass with Fields(以值域取代子类)
- 重新组织数据之十六 :Replace Subclass with Fields(以值域取代子类)
- 重构手法31:Replace Type Code with Subclass (以子类取代类型码)
- 重构之3.Replace Type Code with Subclasses(以子类取代类型码)
- 重构---重构手法-----以查询取代临时变量(Replace Temp with Query)
- 重构手法30:Replace Type Code with Class (以类取代类型码)
- Replace Type Code with Subclasses(以子类取代型别码)
- 重新组织数据之十四 :Replace Type Code with Subclasses(以子类取代型别码)
- 重构之2.Replace Type Code with Class(以类取代类型码)
- 重构之4.Replace Type Code with State/Strategy(以State/Strategy取代类型码)
- java重构学习7:以函数对象取代函数(Replace Method with Method Object)
- 重构--Replace Temp with Query(以查询取代临时变量)(六)
- 8.14 replace type code with subclasses (以子类取代类型码)
- Replace Temp With Query(以查询取代临时变量)
- Replace Conditional with Polymorphism(以多态取代条件式)
- Replace Inheritance with Delegation(以继承取代委托)
- Replace Delegation with Inheritance(以委托取代继承)
- 十大酷刑之断wifi,你中了?
- 设计模式 - 观察者模式(Observer Pattern) 详解
- cocostudio介绍
- memset 赋值
- c++ 二维数组简单介绍
- 重构 -- Replace Subclass with Fields(以值域取代子类)
- iOS---CGAffineTransform
- Ubuntu下安装 Source insight
- 作践了自己,辜负了岁月
- mysql workbench建表时PK,NN,UQ,BIN,UN,ZF,AI代表的含义
- 一个比nginx速度更快的HTTP服务器
- u-boot移植总结(一)start.S分析
- 图像增强相关基础知识
- eclipse 修改主题