重构-改善既有的代码设计-处理概括关系(11-2)

版权声明:不自见故明;不自是故彰;不自伐故有功;不自矜故长; https://blog.csdn.net/LightUpHeaven/article/details/84893941

11.6.提炼子类(Extract Subclass)

type Employee struct {
	_rate int
}

func (e *Employee) getRate() int {
	return e._rate
}


type JobItem struct {
	_quantity int
}

func (j *JobItem) getTotalPrice() int {
	return j.getUnitPrice() * j._quantity
}

func (j *JobItem) getUnitPrice() int {
	return 0
}

func (j *JobItem)isLabor() bool {
	return false
}

func (j *JobItem) getQuantity() int {
	return j._quantity
}



type LaborItem struct {
	JobItem
	_employee Employee
}

func (l *LaborItem) getEmployee() Employee {
	return l._employee
}

func (l *LaborItem) isLabor() bool {
	return true
}

func (l *LaborItem) getUnitPrice() int {
	return l._employee.getRate()
}

type PartsItem struct {
	JobItem
	_unitPrice int
}

func (p *PartsItem) getUnitPrice() int {
	return p._unitPrice
}

11.7.提炼超类(Extract Superclass)

11.8.提炼接口(Extract Interface)

11.9.折叠继承体系(Collapse Hierarchy)

11.10.塑造模板函数(Form Template Method)

11.11.以委托取代继承(Replace Inheritance with Delegation)

11.12.以继承取代委托(Replace Delegation with Inheritance)

猜你喜欢

转载自blog.csdn.net/LightUpHeaven/article/details/84893941
今日推荐