Inline Class (อินไลน์คลาส)

ตรงข้ามกับ: Extract Class ( 182 )

A figure shows a simple representation and a code pertaining to an inline class.

แผนภาพแสดง Inline Class โดยมีฟีเจอร์ที่ถูกย้ายจาก class หนึ่งไปยังอีก class หนึ่งอยู่ด้านบน code จริงคือ class Person { get officeAreaCode() {return this._telephoneNumber.areaCode;} get officeNumber() {return this._telephoneNumber.number;} } class TelephoneNumber { get areaCode() {return this._areaCode;} get number() {return this._number;} } code หลัง refactoring คือ class Person { get officeAreaCode() {return this._officeAreaCode;} get officeNumber() {return this._officeNumber;}

Motivation (แรงจูงใจ)

Inline Class คือสิ่งที่ตรงข้ามกับ Extract Class ( 182 ) ผมใช้ Inline Class เมื่อคลาสไม่ได้ช่วยแบ่งเบาภาระอีกต่อไปและไม่ควรมีอยู่ต่อไป บ่อยครั้งนี่คือ ผลจากการ refactoring ที่ย้ายความรับผิดชอบอื่นออกจากคลาสจนแทบไม่เหลืออะไร ตอนนั้นผม ก็ยุบคลาสนั้นเข้ากับอีกคลาส—คลาสที่ใช้ประโยชน์จากคลาสแคระนี้มากที่สุด

อีกเหตุผลในการใช้ Inline Class คือเมื่อผมมีสองคลาสที่ต้องการ refactor ให้กลายเป็นคลาสคู่ใหม่ที่มี การจัดสรรฟีเจอร์ต่างออกไป ผมอาจพบว่าการใช้ Inline Class เพื่อรวมมันเป็นคลาสเดียวก่อนนั้นง่ายกว่า คลาส แล้วค่อย Extract Class ( 182 ) เพื่อแยกมันใหม่ แนวทางนี้ใช้ได้ทั่วไปเวลาจัดระเบียบสิ่งต่าง ๆ: บางครั้งการย้าย สมาชิกทีละตัวจากบริบทหนึ่งไปอีกบริบทหนึ่งจะง่ายกว่า แต่บางครั้งการใช้ inline refactoring เพื่อ ยุบบริบทเข้าด้วยกันก่อน แล้วค่อยใช้ extract refactoring เพื่อแยกพวกมันเป็นสมาชิกต่าง ๆ จะดีกว่า

Mechanics (กลไก)

  • ใน class เป้าหมาย ให้สร้าง function สำหรับ function public ทั้งหมดของ class ต้นทาง โดย function เหล่านี้ควร ทำหน้าที่แค่ delegate ไปยัง class ต้นทางเท่านั้น

  • เปลี่ยน reference ทั้งหมดที่ชี้ไปยัง method ของ class ต้นทางให้ใช้ delegator ของ class เป้าหมายแทน test หลังการ เปลี่ยนแปลงแต่ละครั้ง

  • ย้าย function และข้อมูลทั้งหมดจาก class ต้นทางไปยัง class เป้าหมาย โดย test หลังการย้ายแต่ละครั้ง จนกว่า class ต้นทาง จะว่างเปล่า

  • ลบ class ต้นทางทิ้ง แล้วจัดพิธีศพสั้น ๆ เรียบง่ายให้มัน

Example (ตัวอย่าง)

นี่คือ class ที่เก็บข้อมูลการติดตามสองสามชิ้นสำหรับการจัดส่ง (shipment)

คลิกที่นี่เพื่อดู code

class TrackingInformation {
  get shippingCompany()    {return this._shippingCompany;}
  set shippingCompany(arg) {this._shippingCompany = arg;}
  get trackingNumber()    {return this._trackingNumber;}
  set trackingNumber(arg) {this._trackingNumber = arg;}
  get display()            {
    return `${this.shippingCompany}: ${this.trackingNumber}`;
  }
}

มันถูกใช้เป็นส่วนหนึ่งของ class shipment

class Shipment…

คลิกที่นี่เพื่อดู code

get trackingInfo() {
  return this._trackingInformation.display;
}
get trackingInformation()    {return this._trackingInformation;}
set trackingInformation(aTrackingInformation) {
  this._trackingInformation = aTrackingInformation;
}

ถึงแม้ class นี้อาจเคยมีคุณค่าในอดีต แต่ตอนนี้ผมรู้สึกว่ามันไม่ได้ช่วยแบ่งเบาภาระอีกต่อไป ผมจึงอยากอินไลน์ มันเข้าไปใน Shipment .

ผมเริ่มจากการหาจุดที่เรียก method ของ TrackingInformation .

caller…

คลิกที่นี่เพื่อดู code

aShipment.trackingInformation.shippingCompany = request.vendor;

ผมจะย้ายฟังก์ชันพวกนั้นทั้งหมดไปที่ Shipment , แต่ผมจะทำมันต่างออกไปเล็กน้อยจากวิธีที่ผมใช้ตามปกติ Move Function ( 198 ) ในกรณีนี้ ผมเริ่มจากใส่ delegating method ลงใน shipment แล้วปรับ client ให้เรียกเมธอดนั้น

class Shipment…

คลิกที่นี่เพื่อดู code

set shippingCompany(arg) {this._trackingInformation.shippingCompany = arg;}

caller…

คลิกที่นี่เพื่อดู code

aShipment.trackingInformation.shippingCompany = request.vendor;

ผมทำแบบนี้กับทุกส่วนของ tracking information ที่ client ใช้ พอทำเสร็จ ผมก็ย้าย ทุกส่วนของ tracking information เข้าไปใน class shipment ได้

ผมเริ่มด้วยการนำ Inline Function ( 115 ) ไปใช้กับ method display .

class Shipment…

คลิกที่นี่เพื่อดู code

get trackingInfo() {
  return `${this.shippingCompany}: ${this.trackingNumber}`;
}

ผมย้าย field บริษัทขนส่ง (shipping company)

คลิกที่นี่เพื่อดู code

get shippingCompany()    {return this._trackingInformation._shippingCompany;}
set shippingCompany(arg) {this._trackingInformation._shippingCompany = arg;}

ผมไม่ได้ใช้กลไกเต็มรูปแบบของ Move Field ( 207 ) เพราะในกรณีนี้ผมอ้างถึง shippingCompany จาก Shipment ซึ่งเป็นเป้าหมายของการย้าย ดังนั้นผมจึงไม่ต้องใช้ขั้นตอนที่สร้าง reference จากต้นทางไปยังเป้าหมาย

ผมทำต่อไปจนกว่าทุกอย่างจะถูกย้ายครบ พอเสร็จแล้วก็ลบ class tracking information ได้

class Shipment…

คลิกที่นี่เพื่อดู code

get trackingInfo() {
  return `${this.shippingCompany}: ${this.trackingNumber}`;
}
get shippingCompany()    {return this._shippingCompany;}
set shippingCompany(arg) {this._shippingCompany = arg;}
get trackingNumber()    {return this._trackingNumber;}
set trackingNumber(arg) {this._trackingNumber = arg;}