Extract Superclass (การแยก Superclass)

A figure illustrates how the refactoring technique is used to extract a superclass.

แผนภาพแสดง superclass ที่สืบทอดคุณสมบัติร่วมของ subclass ต่าง ๆ code จริงคือ class Department { get totalAnnualCost() {...} get name() {...} get headCount() {...} } class Employee { get annualCost() {...} get name() {...} get id() {...} } code หลัง refactoring คือ class Party { get name() {...} get annualCost() {...} } class Department extends Party { get annualCost() {...} get headCount() {...} } class Employee extends Party { get annualCost() {...} get id() {...} }

Motivation (แรงจูงใจ)

ถ้าผมเห็นสอง class ที่ทำอะไรคล้ายกัน ผมก็สามารถใช้ประโยชน์จากกลไกพื้นฐานของ inheritance เพื่อรวบรวมความคล้ายคลึงของพวกมันไว้ใน superclass ได้ ผมใช้ Pull Up Field ( 353 ) เพื่อย้ายข้อมูลร่วมขึ้นไปยัง superclass และใช้ Pull Up Method ( 350 ) เพื่อย้ายพฤติกรรมร่วม

นักเขียนหลายคนในสาย object orientation มองว่า inheritance เป็นสิ่งที่ต้องวางแผนล่วงหน้าอย่างรอบคอบ โดยอิงจากโครงสร้างการจำแนกบางอย่างใน "โลกแห่งความเป็นจริง" โครงสร้างการจำแนกแบบนั้นอาจเป็นคำใบ้ที่บอกให้ใช้ inheritance—แต่บ่อยครั้งพอ ๆ กัน inheritance คือสิ่งที่ผมเพิ่งจะนึกได้ระหว่างที่โปรแกรมวิวัฒนาการ เมื่อผมพบองค์ประกอบร่วมที่อยากดึงมารวมกัน

ทางเลือกหนึ่งของ Extract Superclass คือ Extract Class ( 182 ) . ตรงนี้คุณต้องเลือกระหว่างการใช้ inheritance หรือ delegation เป็นวิธีรวมพฤติกรรมที่ซ้ำกัน โดยทั่วไป Extract Superclass เป็นวิธีที่ง่ายกว่า ผมจึงทำมันก่อน โดยรู้ว่าผมใช้ Replace Superclass with Delegate ( 399 ) ได้ทีหลังถ้าจำเป็น

Mechanics (ขั้นตอนการทำ)

  • สร้าง superclass ที่ว่างเปล่า แล้วทำให้ class ดั้งเดิมทั้งสองเป็น subclass ของมัน

    ถ้าจำเป็น ให้ใช้ Change Function Declaration ( 124 ) กับ constructor ต่าง ๆ

  • test

  • ทีละอย่าง ใช้ Pull Up Constructor Body ( 355 ) , Pull Up Method ( 350 ) และ Pull Up Field ( 353 ) เพื่อย้ายองค์ประกอบร่วมขึ้นไปที่ superclass

  • ตรวจสอบเมธอดที่เหลืออยู่บน subclass ต่าง ๆ ว่ามีส่วนที่เหมือนกันหรือไม่ ถ้ามี ให้ใช้ Extract Function ( 106 ) ตามด้วย Pull Up Method ( 350 ) .

  • ตรวจสอบ client ของ class ดั้งเดิม พิจารณาปรับให้พวกมันใช้ interface ของ superclass

Example (ตัวอย่าง)

ผมกำลังครุ่นคิดกับสอง class นี้ พวกมันมีฟังก์ชันร่วมกันบางอย่าง—นั่นคือ name และแนวคิดเรื่องต้นทุนรายปีและรายเดือน:

class Employee {
  constructor(name, id, monthlyCost) {
    this._id = id;
    this._name = name;
    this._monthlyCost = monthlyCost;
  }
  get monthlyCost() {return this._monthlyCost;}
  get name() {return this._name;}
  get id() {return this._id;}

  get annualCost() {
    return this.monthlyCost * 12;
  }
}

class Department {
  constructor(name, staff){
    this._name = name;
    this._staff = staff;
  }
  get staff() {return this._staff.slice();}
  get name() {return this._name;}

  get totalMonthlyCost() {
    return this.staff
      .map(e => e.monthlyCost)
      .reduce((sum, cost) => sum + cost);
  }
  get headCount() {
    return this.staff.length;
  }
  get totalAnnualCost() {
    return this.totalMonthlyCost * 12;
  }
}

ผมสามารถทำให้พฤติกรรมร่วมชัดเจนขึ้นได้โดยการแยก superclass ร่วมออกมาจากพวกมัน

ผมเริ่มต้นด้วยการสร้าง superclass ที่ว่างเปล่า แล้วให้ทั้งสองคลาส extend จากมัน

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...

เวลาทำ Extract Superclass ผมชอบเริ่มจากข้อมูล ซึ่งใน JavaScript เกี่ยวข้องกับการจัดการกับ constructor ดังนั้นผมจึงเริ่มด้วย Pull Up Field ( 353 ) เพื่อดึง name ขึ้นไป

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;
}

เมื่อผมดึงข้อมูลขึ้นไปที่ superclass ได้แล้ว ผมก็ใช้ Pull Up Method ( 350 ) กับเมธอดที่เกี่ยวข้องได้ด้วย อย่างแรกคือ name:

class Party…

get name() {return this._name;}

class Employee…

get name() {return this._name;}

class Department…

get name() {return this._name;}

ผมมีสอง method ที่ body คล้ายกัน

class Employee…

get annualCost() {
  return this.monthlyCost * 12;
}

class Department…

get totalAnnualCost() {
  return this.totalMonthlyCost * 12;
}

method ที่พวกมันใช้คือ monthlyCost และ totalMonthlyCost มีชื่อต่างกันและ body ต่างกัน—แต่มันแทนเจตนาเดียวกันหรือเปล่า? ถ้าใช่ ผมควรใช้ Change Function Declaration ( 124 ) เพื่อรวมชื่อของพวกมันให้เป็นหนึ่งเดียว

class Department…

get totalAnnualCost() {
  return this.monthlyCost * 12;
}

get monthlyCost() { … }

จากนั้นผมก็ทำการเปลี่ยนชื่อที่คล้ายกันกับต้นทุนรายปี:

class Department…

get annualCost() {
  return this.monthlyCost * 12;
}

ตอนนี้ผมสามารถใช้ Pull Up Method ( 350 ) กับเมธอดต้นทุนรายปีได้แล้ว

class Party…

get annualCost() {
  return this.monthlyCost * 12;
}

class Employee…

get annualCost() {
  return this.monthlyCost * 12;
}

class Department…

get annualCost() {
  return this.monthlyCost * 12;
}