Pull Up Constructor Body (การดึง constructor body ขึ้น)
แผนภาพแสดง constructor ต่าง ๆ ที่ถูกดึงขึ้นไปไว้ใน superclass จาก subclass code จริงคือ class Party {...} class Employee extends Party { constructor(name, id, monthlyCost) { super(); this._id = id; this._name = name; this._monthlyCost = monthlyCost; } } code หลัง refactoring คือ class Party { constructor(name){ this._name = name; } } class Employee extends Party { constructor(name, id, monthlyCost) { super(name); this._id = id; this._monthlyCost = monthlyCost; } }
Motivation (แรงจูงใจ)
Constructor เป็นสิ่งทีี่ซับซ้อน พวกมันไม่ใช่ method ปกติเสียทีเดียว—ดังนั้นผมจึงถูกจำกัดมากขึ้นในสิ่งที่ทำได้กับมัน
ถ้าผมเห็นเมธอดใน subclass ที่มีพฤติกรรมร่วมกัน ความคิดแรกของผมคือการใช้ Extract Function ( 106 ) ตามด้วย Pull Up Method ( 350 ) ซึ่งจะย้ายมันขึ้นไปไว้ใน superclass ได้อย่างเรียบร้อย แต่ constructor ทำให้เรื่องนี้พัวพัน—เพราะมันมีกฎพิเศษเกี่ยวกับสิ่ง ที่สามารถทำได้ในลำดับใด ดังนั้นผมจึงต้องใช้แนวทางที่แตกต่างออกไปเล็กน้อย
ถ้า refactoring นี้เริ่มจะยุ่งเหยิง ผมจะใช้ Replace Constructor with Factory Function ( 334 ) .
Mechanics (ขั้นตอนการทำ)
-
นิยาม constructor ของ superclass ขึ้น ถ้ายังไม่มีอยู่ แล้วทำให้แน่ใจว่า constructor ของ subclass เรียกมัน
-
ใช้ Slide Statements ( 223 ) เพื่อย้าย statement ที่เหมือนกันทั้งหมดไปไว้ถัดจาก super call
-
นำ code ที่เหมือนกันออกจาก subclass แต่ละตัวแล้วใส่ไว้ใน superclass เพิ่ม parameter ของ constructor ที่ถูกอ้างถึงใน code ที่เหมือนกันนั้นเข้าไปใน super call ด้วย
-
test
-
ถ้ามี code ที่เหมือนกันบางส่วนที่ไม่สามารถย้ายไปไว้ที่จุดเริ่มต้นของ constructor ได้ ให้ใช้ Extract Function ( 106 ) ตามด้วย Pull Up Method ( 350 ) .
Example (ตัวอย่าง)
ผมเริ่มจาก code ต่อไปนี้:
class Party {}
class Employee extends Party {
constructor(name, id, monthlyCost) {
super();
this._id = id;
this._name = name;
this._monthlyCost = monthlyCost;
}
// rest of class...
class Department extends Party {
constructor(name, staff){
super();
this._name = name;
this._staff = staff;
}
// rest of class...
code ที่เหมือนกันตรงนี้คือ assignment ของ name ผมใช้ Slide Statements ( 223 ) เพื่อย้าย assignment ใน Employee ไปไว้ถัดจากการเรียก super() :
class Employee extends Party {
constructor(name, id, monthlyCost) {
super();
this._name = name;
this._id = id;
this._monthlyCost = monthlyCost;
}
// rest of class...
เมื่อ test แล้ว ผมก็ย้าย code ที่เหมือนกันไปไว้ใน superclass เนื่องจาก code นั้นมีการอ้างอิง argument ของ constructor ผมจึงส่งมันเข้าไปเป็น parameter
class Party…
constructor(name){
this._name = name;
}
class Employee…
constructor(name, id, monthlyCost) {
super(name);
this._id = id;
this._monthlyCost = monthlyCost;
}
class Department…
constructor(name, staff){
super(name);
this._staff = staff;
}
รัน test แล้วก็เสร็จ
โดยส่วนใหญ่ พฤติกรรมของ constructor จะเป็นแบบนี้: ทำองค์ประกอบที่เหมือนกันก่อน (ด้วยการเรียก super ) จากนั้นค่อยทำงานพิเศษที่ subclass ต้องการ อย่างไรก็ตาม ในบางครั้งก็มีพฤติกรรมที่เหมือนกันอยู่ช่วงท้าย ๆ
ลองพิจารณาตัวอย่างนี้:
class Employee…
constructor (name) {...}
get isPrivileged() {...}
assignCar() {...}
class Manager extends Employee…
constructor(name, grade) {
super(name);
this._grade = grade;
if (this.isPrivileged) this.assignCar(); // every subclass does this
}
get isPrivileged() {
return this._grade > 4;
}
ปมปัญหาตรงนี้มาจากข้อเท็จจริงที่ว่าการเรียก isPrivileged จะเกิดขึ้นได้ก็ต่อเมื่อ field grade ถูกกำหนดค่าแล้วเท่านั้น และการกำหนดค่านั้นทำได้ใน subclass เท่านั้น
ในกรณีนี้ ผมใช้ Extract Function ( 106 ) กับ code ที่เหมือนกัน:
class Manager…
constructor(name, grade) {
super(name);
this._grade = grade;
this.finishConstruction();
}
finishConstruction() {
if (this.isPrivileged) this.assignCar();
}
จากนั้น ผมใช้ Pull Up Method ( 350 ) เพื่อย้ายมันขึ้นไปไว้ใน superclass
class Employee…
finishConstruction() {
if (this.isPrivileged) this.assignCar();
}