Remove Subclass (การลบ Subclass)
ชื่อเดิม: Replace Subclass with Fields
ตรงข้ามกับ: Replace Type Code with Subclasses ( 362 )
แผนภาพแสดงการลบ subclass ออก code จริงคือ class Person { get genderCode() {return "X";} } class Male extends Person { get genderCode() {return "M";} } class Female extends Person { get genderCode() {return "F";} } code หลัง refactoring คือ class Person { get genderCode() {return this._genderCode;} }
Motivation (แรงจูงใจ)
Subclass นั้นมีประโยชน์ พวกมันรองรับความหลากหลายของ data structure และพฤติกรรมแบบ polymorphism และเป็นวิธีที่ดีในการเขียนโปรแกรมโดยใช้ความแตกต่าง (program by difference) แต่เมื่อระบบ software วิวัฒนาการไป subclass อาจสูญเสียคุณค่าเมื่อความหลากหลายที่พวกมันรองรับถูกย้ายไปที่อื่นหรือถูกลบทิ้งไปหมดสิ้น บางครั้ง subclass ถูกเพิ่มขึ้นเพราะคาดการณ์ feature ที่สุดท้ายไม่เคยถูกสร้าง หรือถูกสร้างในแบบที่ไม่ต้องใช้ subclass เหล่านั้น
Subclass ที่ทำงานน้อยเกินไปนั้นมีต้นทุนด้านความเข้าใจที่ไม่คุ้มค่าอีกต่อไป เมื่อถึงเวลานั้น วิธีที่ดีที่สุดคือลบ subclass ทิ้ง แล้วแทนที่มันด้วย field บน superclass
Mechanics (ขั้นตอนการทำ)
-
ใช้ Replace Constructor with Factory Function ( 334 ) กับ constructor ของ subclass
ถ้า client ของ constructor ใช้ data field ในการตัดสินใจว่าจะสร้าง subclass ไหน ให้ใส่ decision logic นั้นลงใน factory method ของ superclass
-
ถ้ามี code ใดทดสอบกับ type ของ subclass ให้ใช้ Extract Function ( 106 ) กับ type test แล้วใช้ Move Function ( 198 ) ย้ายมันไปที่ superclass ทดสอบหลังการเปลี่ยนแปลงแต่ละครั้ง
-
สร้าง field เพื่อแทนประเภทของ subclass
-
เปลี่ยน method ที่อ้างถึง subclass ให้ใช้ field ประเภทใหม่
-
ลบ subclass ทิ้ง
-
Test
บ่อยครั้งที่ refactoring นี้ถูกใช้กับกลุ่มของ subclass พร้อมกัน—ในกรณีนั้นให้ทำขั้นตอนที่ encapsulate พวกมันก่อน (เพิ่ม factory function, ย้าย type tests) จากนั้นค่อย ๆ ยุบมันเข้าไปใน superclass ทีละตัว
Example (ตัวอย่าง)
ผมจะเริ่มด้วย subclass เศษเดนชุดนี้:
class Person…
constructor(name) {
this._name = name;
}
get name() {return this._name;}
get genderCode() {return "X";}
// snip
class Male extends Person {
get genderCode() {return "M";}
}
class Female extends Person {
get genderCode() {return "F";}
}
ถ้า subclass ทำได้แค่นี้ มันก็ไม่คุ้มที่จะมีจริง ๆ แต่ก่อนที่ผมจะลบ subclass พวกนี้ โดยปกติแล้วมันคุ้มที่จะตรวจสอบก่อนว่ามีพฤติกรรมที่ขึ้นกับ subclass ใน client ใดที่ควรย้ายเข้ามาในนี้หรือไม่ ในกรณีนี้ ผมไม่พบอะไรที่คุ้มค่าจะเก็บ subclass ไว้เลย
client…
const numberOfMales = people.filter(p => p instanceof Male).length;
เมื่อใดก็ตามที่ผมต้องการเปลี่ยนวิธีแทนสิ่งใดสิ่งหนึ่ง ผมจะพยายาม encapsulate การแทนแบบปัจจุบันก่อนเพื่อลดผลกระทบต่อ client code เมื่อพูดถึงการสร้าง subclass วิธีที่จะ encapsulate คือการใช้ Replace Constructor with Factory Function ( 334 ) . ในกรณีนี้ มีสองสามวิธีที่ผมจะสร้าง factory ได้
วิธีที่ตรงไปตรงมาที่สุดคือการสร้าง factory method หนึ่งตัวต่อ constructor หนึ่งตัว
function createPerson(name) {
return new Person(name);
}
function createMale(name) {
return new Male(name);
}
function createFemale(name) {
return new Female(name);
}
แต่ถึงแม้นั่นจะเป็นทางเลือกที่ตรงไปตรงมา object แบบนี้มักถูกโหลดมาจากแหล่งข้อมูลที่ใช้ gender code ตรง ๆ
function loadFromInput(data) {
const result = [];
data.forEach(aRecord => {
let p;
switch (aRecord.gender) {
case 'M': p = new Male(aRecord.name); break;
case 'F': p = new Female(aRecord.name); break;
default: p = new Person(aRecord.name);
}
result.push(p);
});
return result;
}
ในกรณีนั้น ผมพบว่าการใช้ Extract Function ( 106 ) กับ selection logic ว่าจะสร้าง class ไหน แล้วทำให้มันเป็น factory function นั้นดีกว่า
function createPerson(aRecord) {
let p;
switch (aRecord.gender) {
case 'M': p = new Male(aRecord.name); break;
case 'F': p = new Female(aRecord.name); break;
default: p = new Person(aRecord.name);
}
return p;
}
function loadFromInput(data) {
const result = [];
data.forEach(aRecord => {
result.push(createPerson(aRecord));
});
return result;
}
ระหว่างนั้น ผมจะเก็บกวาด function สองตัวนั้นด้วย ผมจะใช้ Inline Variable ( 123 ) กับ createPerson :
function createPerson(aRecord) {
switch (aRecord.gender) {
case 'M': return new Male (aRecord.name);
case 'F': return new Female(aRecord.name);
default: return new Person(aRecord.name);
}
}
และใช้ Replace Loop with Pipeline ( 231 ) กับ loadFromInput :
function loadFromInput(data) {
return data.map(aRecord => createPerson(aRecord));
}
Factory encapsulate การสร้าง subclass แต่ก็ยังมีการใช้ instanceof อยู่—ซึ่งไม่เคยให้ความรู้สึกดีเลย ผมใช้ Extract Function ( 106 ) กับ type check นั้น
client…
const numberOfMales = people.filter(p => isMale(p)).length;
function isMale(aPerson) {return aPerson instanceof Male;}
จากนั้นผมใช้ Move Function ( 198 ) ย้ายมันเข้าไปใน Person .
class Person…
get isMale() {return this instanceof Male;}
client…
const numberOfMales = people.filter(p => p.isMale).length;
เมื่อทำ refactoring นั้นเสร็จ ความรู้ทั้งหมดเกี่ยวกับ subclass ก็ถูกห่อหุ้มไว้อย่างปลอดภัยภายใน superclass และ factory function แล้ว (โดยปกติผมจะระวังเรื่อง superclass ที่อ้างถึง subclass แต่ code ชุดนี้คงอยู่ไม่ทันจนกว่าผมจะได้จิบชารอบหน้า ดังนั้นผมจะไม่กังวลกับมัน)
ตอนนี้ผมเพิ่ม field เพื่อแทนความแตกต่างระหว่าง subclass ต่าง ๆ เนื่องจากผมใช้ code ที่โหลดมาจากที่อื่น ผมก็อาจจะใช้มันตรง ๆ เลย
class Person…
constructor(name, genderCode) {
this._name = name;
this._genderCode = genderCode || "X";
}
get genderCode() {return this._genderCode;}
ตอน initializing ผมตั้งค่าให้เป็นกรณี default (หมายเหตุข้าง ๆ ว่า แม้คนส่วนใหญ่จะจำแนกได้เป็นชายหรือหญิง แต่ก็มีคนที่ไม่เข้าพวก การลืมประเด็นนี้เป็นความผิดพลาดในการออกแบบโมเดลที่พบบ่อย)
จากนั้นผมก็เอาคดีของ male มาพับ logic ของมันเข้าไปใน superclass สิ่งนี้เกี่ยวข้องกับการแก้ factory ให้คืนค่า Person และแก้ test instanceof ใด ๆ ให้ใช้ gender code field แทน
function createPerson(aRecord) {
switch (aRecord.gender) {
case 'M': return new Person(aRecord.name, "M");
case 'F': return new Female(aRecord.name);
default: return new Person(aRecord.name);
}
}
class Person…
get isMale() {return "M" === this._genderCode;}
ผม test ลบ subclass male ทิ้ง test อีกครั้ง แล้วทำซ้ำกับ subclass female
function createPerson(aRecord) {
switch (aRecord.gender) {
case 'M': return new Person(aRecord.name, "M");
case 'F': return new Person(aRecord.name, "F");
default: return new Person(aRecord.name);
}
}
ผมรู้สึกรำคาญกับความไม่สมมาตรของ gender code ผู้อ่านโค้ดในอนาคตจะต้องสงสัยถึงความไม่สมมาตรนี้อยู่เสมอ ดังนั้นผมจึงอยากเปลี่ยนโค้ดให้มันสมมาตร—ถ้าผมทำได้โดยไม่เพิ่มความซับซ้อนอื่นใด ซึ่งก็เป็นกรณีนี้
function createPerson(aRecord) {
switch (aRecord.gender) {
case 'M': return new Person(aRecord.name, "M");
case 'F': return new Person(aRecord.name, "F");
default: return new Person(aRecord.name, "X");
}
}
class Person…
constructor(name, genderCode) {
this._name = name;
this._genderCode = genderCode || "X";
}