1. 程式人生 > >重構-改善既有的程式碼設計-處理概括關係(11-2)

重構-改善既有的程式碼設計-處理概括關係(11-2)

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)