Replace Primitive with Object (แทนที่ primitive ด้วย object)

เดิมชื่อ: Replace Data Value with Object

เดิมชื่อ: Replace Type Code with Class

A figure shows a simple representation and a code for replacing a primitive with an object.

แผนภาพแสดงการแทนที่ primitive ด้วย object โดยมี class ที่แยกออกมาอยู่ด้านบน และ code จริงคือ orders.filter(o => "high" === o.priority || "rush" === o.priority); code หลัง refactoring คือ orders.filter(o => o.priority.higherThan(new Priority("normal")))

Motivation (แรงจูงใจ)

บ่อยครั้งในช่วงแรกของการพัฒนา คุณตัดสินใจแทนข้อเท็จจริงง่าย ๆ ด้วยข้อมูลแบบง่าย ๆ เช่น ตัวเลขหรือ string เมื่อการพัฒนาดำเนินต่อไป สิ่งที่เคยเรียบง่ายก็ไม่เรียบง่ายอีกต่อไป เบอร์โทรศัพท์อาจ ถูกแทนด้วย string อยู่พักหนึ่ง แต่ต่อมามันจะต้องมีพฤติกรรมพิเศษสำหรับการจัดรูปแบบ การแยกเลขพื้นที่ และอะไรทำนองนั้น logic แบบนี้จะถูกก็อปปี้กระจายไปทั่ว code base อย่างรวดเร็ว และเพิ่ม ความพยายามทุกครั้งที่ต้องใช้งานมัน

ทันทีที่ผมรู้ตัวว่าต้องการทำอะไรที่มากกว่าการพิมพ์ค่าทั่วไป ผมจะสร้าง class ใหม่สำหรับข้อมูลชิ้นนั้น ตอนแรก class แบบนี้ทำอะไรได้ไม่มากไปกว่าครอบ primitive—แต่พอมี class แล้ว ผมก็มีที่สำหรับใส่พฤติกรรมเฉพาะตามความต้องการของมัน ค่าเล็ก ๆ พวกนี้เริ่มต้นอย่างต่ำต้อย แต่เมื่อได้รับการดูแล มันก็เติบโตเป็นเครื่องมือที่มีประโยชน์ มันอาจดูไม่สวยงาม แต่ผลกระทบต่อ code base นั้นน่าประหลาดใจมาก จริง ๆ แล้ว นักพัฒนาที่มีประสบการณ์หลายคนถือว่านี่คือหนึ่งใน refactorings ที่มีค่าที่สุดในชุดเครื่องมือ—ถึงแม้ว่ามันมักจะดูขัดสามัญสำนึกสำหรับโปรแกรมเมอร์มือใหม่

Mechanics (กลไก)

  • ใช้ Encapsulate Variable ( 132 ) ถ้ายังไม่ได้ทำ

  • สร้าง value class ง่าย ๆ สำหรับค่านั้น โดยให้ constructor รับค่าเดิมที่มีอยู่และมี getter สำหรับค่านั้น

  • รัน static checks.

  • เปลี่ยน setter ให้สร้างอินสแตนซ์ใหม่ของ value class แล้วเก็บลงใน field พร้อมทั้งเปลี่ยนชนิดของ field ถ้ามีการระบุชนิดไว้

  • เปลี่ยน getter ให้คืนผลลัพธ์จากการเรียก getter ของ class ใหม่

  • test.

  • ลองพิจารณาใช้ Rename Function ( 124 ) กับ accessor เดิมเพื่อให้ชื่อสะท้อนสิ่งที่มันทำได้ดีขึ้น

  • ลองพิจารณาทำให้บทบาทของออบเจกต์ใหม่ชัดเจนขึ้นว่าเป็น value หรือ reference object โดยใช้ Change Reference to Value ( 252 ) หรือ Change Value to Reference ( 256 ) .

Example (ตัวอย่าง)

ผมเริ่มจาก class order ง่าย ๆ ที่อ่านข้อมูลจากโครงสร้าง record แบบง่าย คุณสมบัติหนึ่งของมันคือ priority ซึ่งอ่านออกมาเป็น string ธรรมดา

class Order…

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

constructor(data) {
  this.priority = data.priority;
  // more initialization

client code บางส่วนใช้งานมันแบบนี้:

client…

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

highPriorityCount = orders.filter(o => "high" === o.priority
                                    || "rush" === o.priority)
                          .length;

เมื่อใดก็ตามที่ผมกำลังยุ่งกับ data value สิ่งแรกที่ผมทำคือใช้ Encapsulate Variable ( 132 ) กับมัน

class Order…

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

get priority()        {return this._priority;}
set priority(aString) {this._priority = aString;}

บรรทัดใน constructor ที่กำหนดค่าเริ่มต้นให้ priority จะเปลี่ยนไปใช้ setter ที่ผมนียามไว้ตรงนี้

การทำแบบนี้เท่ากับ self-encapsulate field เพื่อให้ผมรักษาการใช้งานปัจจุบันของมันไว้ได้ระหว่างที่จัดการกับตัวข้อมูล

ผมสร้าง value class ง่าย ๆ สำหรับ priority โดยมี constructor รับค่า และ function แปลงค่าเพื่อคืนค่าเป็น string

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

class Priority {
  constructor(value) {this._value = value;}
  toString() {return this._value;}
}

ตรงนี้ผมชอบใช้ function แปลงค่า (toString) มากกว่า getter (value) เพราะสำหรับ client ของ class การขอค่าในรูปแบบ string ควรให้ความรู้สึกเหมือนการแปลงค่ามากกว่า การดึง property

จากนั้นผมก็แก้ accessor ให้ใช้ class ใหม่นี้

class Order…

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

get priority()        {return this._priority.toString();}
set priority(aString) {this._priority = new Priority(aString);}

พอมี class priority แล้ว ผมพบว่า getter ปัจจุบันบน order ทำให้เข้าใจผิด เพราะมันไม่ได้คืน priority—แต่คืน string ที่อธิบาย priority สิ่งที่ผมทำทันทีคือใช้ Rename Function ( 124 ) .

class Order…

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

get priorityString()  {return this._priority.toString();}
set priority(aString) {this._priority = new Priority(aString);}

client…

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

highPriorityCount = orders.filter(o => "high" === o.priorityString
                                    || "rush" === o.priorityString)
                          .length;

ในกรณีนี้ผมพอใจที่จะใช้ชื่อ setter เดิม เพราะชื่อของ argument บ่งบอกอยู่แล้วว่ามันคาดหวังอะไร

ตอนนี้การ refactoring ตามรูปแบบเสร็จสิ้นแล้ว แต่เมื่อผมดูผู้ใช้ priority ผมก็คิดว่าพวกเขาควรใช้ class priority เองหรือไม่ ด้วยเหตุนี้ผมจึงเพิ่ม getter บน order ที่คืน object priority ใหม่ ออกไปโดยตรง

class Order…

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

get priority()        {return this._priority;}
get priorityString()  {return this._priority.toString();}
set priority(aString) {this._priority = new Priority(aString);}

client…

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

highPriorityCount = orders.filter(o => "high" === o.priority.toString()
                                    || "rush" === o.priority.toString())
                          .length;

เมื่อ class priority เริ่มมีประโยชน์ในที่อื่น ผมก็จะยอมให้ client ของ order ใช้ setter กับอินสแตนซ์ priority ได้ ซึ่งทำได้โดยปรับ constructor ของ priority

class Priority…

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

constructor(value) {
  if (value instanceof Priority) return value;
  this._value = value;
}

ประเด็นของทั้งหมดนี้คือ ตอนนี้ class priority ใหม่ของผมกลายเป็นที่รองรับพฤติกรรมใหม่—ไม่ว่าจะเป็นของใหม่ ใน code หรือถูกย้ายมาจากที่อื่น นี่คือ code ง่าย ๆ สำหรับเพิ่มการตรวจสอบค่า priority และ logic การเปรียบเทียบ:

class Priority…

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

constructor(value) {
  if (value instanceof Priority) return value;
  if (Priority.legalValues().includes(value))
    this._value = value;
  else
    throw new Error(`<${value}> is invalid for Priority`);
}
toString() {return this._value;}
get _index() {return Priority.legalValues().findIndex(s => s === this._value);}
static legalValues() {return ['low', 'normal', 'high', 'rush'];}

equals(other) {return this._index === other._index;}
higherThan(other) {return this._index > other._index;}
lowerThan(other) {return this._index < other._index;}

ระหว่างที่ทำ ผมตัดสินใจว่า priority ควรเป็น value object ผมจึงเพิ่ม method equals และทำให้แน่ใจว่ามัน แก้ไขไม่ได้ (immutable)

เมื่อเพิ่มพฤติกรรมนั้นเข้าไปแล้ว ผมก็ทำให้ client code สื่อความหมายได้มากขึ้น:

client…

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

highPriorityCount = orders.filter(o => o.priority.higherThan(new Priority("normal")))
                          .length;