Encapsulate Record (ห่อหุ้มเรคคอร์ด)
เดิมชื่อ: Replace Record with Data Class
แผนภาพแสดงกระบวนการห่อหุ้ม record โดยมี object และ record ซึ่ง object ประกอบด้วยพื้นที่เก็บข้อมูลที่ซ่อนอยู่และ method อื่น ๆ code จริงคือ organization = {name: "Acme Gooseberries", country: "GB"}; code หลัง refactoring คือ class Organization { constructor(data) { this._name = data.name; this._country = data.country; } get name() {return this._name;} set name(arg) {this._name = arg;} get country() {return this._country;} set country(arg) {this._country = arg;} }
Motivation (แรงจูงใจ)
นี่คือเหตุผลที่ผมมักเลือกใช้ object มากกว่า record สำหรับข้อมูลที่แก้ไขได้ (mutable data) ด้วย object ผมสามารถซ่อนสิ่งที่ถูกจัดเก็บไว้ และจัดเตรียม method สำหรับค่าทั้งสามอย่าง ผู้ใช้ object ไม่จำเป็นต้องรู้หรือสนใจว่าค่าไหนถูกเก็บไว้และค่าไหนถูก คำนวณขึ้นมา encapsulation นี้ยังช่วยในเรื่องการเปลี่ยนชื่อด้วย: ผมสามารถเปลี่ยนชื่อ field พร้อมกับเตรียม method สำหรับทั้งชื่อ ใหม่และชื่อเก่าไว้ แล้วค่อย ๆ อัปเดตผู้เรียกใช้ (caller) จนครบทั้งหมด
ที่ผมเพิ่งพูดไปว่าผมชอบ object สำหรับข้อมูลแบบ Mutable Data (แก้ไขได้) ถ้าผมมีค่าที่แก้ไขไม่ได้ (immutable value) ผมก็แค่เก็บค่าทั้งสามไว้ใน record และใช้ขั้นตอนเพิ่มเติม (enrichment) ถ้า จำเป็น ในทำนองเดียวกัน การคัดลอก field เมื่อเปลี่ยนชื่อก็ทำได้ง่าย
record มีอยู่สองแบบ: แบบที่ผมประกาศชื่อ field ที่ถูกต้องได้ และแบบที่เปิดให้ผมใช้ อะไรก็ได้ตามใจ แบบหลังมักถูก implement ผ่าน library class ที่มีชื่อเรียกต่างกันไป เช่น hash, map, hashmap, dictionary หรือ associative array หลายภาษาเตรียม syntax ที่สะดวกสำหรับสร้าง hashmap ซึ่ง ทำให้มันมีประโยชน์ในหลายสถานการณ์ของการเขียนโปรแกรม ข้อเสียของการใช้มันคือมันไม่ชัดเจนเกี่ยวกับ field ของตัวเอง วิธีเดียว ที่ผมจะบอกได้ว่ามันใช้ start/end หรือ start/length คือการดูว่ามันถูกสร้างและใช้ที่ไหนบ้าง นี่ไม่ใช่ ปัญหาถ้ามันถูกใช้ในโค้ดส่วนเล็ก ๆ ของโปรแกรม แต่ยิ่ง scope การใช้งานกว้างเท่าไร โครงสร้างโดยนัยก็ยิ่ง สร้างปัญหาให้ผมมากขึ้นเท่านั้น ผมอาจ refactor record แบบโดยนัยเหล่านี้ให้เป็นแบบชัดเจน—แต่ถ้าผมต้องทำ แบบนั้น ผมขอเปลี่ยนมันเป็นคลาสแทนจะดีกว่า
การส่งต่อโครงสร้างซ้อนของ list และ hashmap เป็นเรื่องปกติ ซึ่งมักถูกทำให้เป็นรูปแบบ (serialize) อย่าง JSON หรือ XML โครงสร้างแบบนี้ก็สามารถห่อหุ้มได้เช่นกัน ซึ่งช่วยได้ถ้ารูปแบบของมันเปลี่ยนไปทีหลัง หรือถ้าผมกังวลเรื่อง การอัปเดตข้อมูลที่ติดตามได้ยาก
Mechanics (กลไก)
-
ใช้ Encapsulate Variable ( 132 ) กับตัวแปรที่เก็บ record ไว้
ตั้งชื่อ function ที่ห่อหุ้ม record ให้ค้นหาได้ง่าย
-
แทนที่เนื้อหาของ variable ด้วย class ง่าย ๆ ที่ครอบ record ไว้ กำหนด accessor ภายใน class นี้เพื่อคืน record ดิบ แล้วแก้ function ที่ห่อหุ้ม variable ให้ใช้ accessor นี้
-
test.
-
เตรียม function ใหม่ที่คืน object แทน record ดิบ
-
สำหรับผู้ใช้ record แต่ละราย ให้เปลี่ยนจากการใช้ function ที่คืน record ไปเป็น function ที่คืน object แล้วใช้ accessor บน object เพื่อเข้าถึงข้อมูล field โดยสร้าง accessor ขึ้นมาใหม่ถ้าจำเป็น test หลัง การเปลี่ยนแปลงแต่ละครั้ง
ถ้าเป็น record ที่ซับซ้อน เช่นมีโครงสร้างซ้อนกัน ให้โฟกัสที่ client ที่อัปเดตข้อมูลก่อน ลองพิจารณาคืนสำเนาหรือ read-only proxy ของข้อมูลให้ client ที่อ่านข้อมูลอย่างเดียว
-
ลบ raw data accessor ของ class และ function ที่ค้นหาง่ายซึ่งเคยคืน record ดิบออก
-
test.
-
ถ้า field ของเรคคอร์ดเป็นโครงสร้างด้วยกัน ให้พิจารณาใช้ Encapsulate Record และ Encapsulate Collection ( 170 ) ซ้ำ ๆ ลงไปเรื่อย ๆ (recursively)
Example (ตัวอย่าง)
ผมจะเริ่มจาก constant ที่ถูกใช้อย่างแพร่หลายทั่วทั้งโปรแกรม
คลิกที่นี่เพื่อดู code
const organization = {name: "Acme Gooseberries", country: "GB"};
นี่คือ JavaScript object ที่ถูกใช้เป็นโครงสร้าง record โดยหลายส่วนของโปรแกรม โดยมีการเข้าถึง แบบนี้:
คลิกที่นี่เพื่อดู code
result += `<h1>${organization.name}</h1>`;
และ
organization.name = newName;
ขั้นตอนแรกคือการทำแบบง่าย ๆ ด้วย Encapsulate Variable ( 132 ) .
คลิกที่นี่เพื่อดู code
function getRawDataOfOrganization() {return organization;}
example reader…
คลิกที่นี่เพื่อดู code
result += `<h1>${getRawDataOfOrganization().name}</h1>`;
example writer…
คลิกที่นี่เพื่อดู code
getRawDataOfOrganization().name = newName;
มันไม่ใช่การทำแบบมาตรฐานเสียทีเดียว Encapsulate Variable ( 132 ) , เพราะผมตั้งชื่อ getter ให้ทั้งน่าเกลียดและค้นหาได้ง่ายโดยเจตนา นั่นเป็นเพราะผม ตั้งใจให้มันมีอายุสั้น
การห่อหุ้ม record หมายถึงการลงลึกไปไกลกว่าแค่ variable ผมต้องการควบคุมว่ามันถูกจัดการอย่างไร ซึ่งผม ทำได้โดยการแทนที่ record ด้วย class
class Organization…
class Organization {
constructor(data) {
this._data = data;
}
}
top level
คลิกที่นี่เพื่อดู code
const organization = new Organization({name: "Acme Gooseberries", country: "GB"});
function getRawDataOfOrganization() {return organization._data;}
function getOrganization() {return organization;}
เมื่อมี object เข้าที่แล้ว ผมก็เริ่มสำรวจผู้ใช้ record ผู้ใช้รายใดที่อัปเดต record จะถูก แทนที่ด้วย setter
class Organization…
คลิกที่นี่เพื่อดู code
set name(aString) {this._data.name = aString;}
client…
getOrganization().name = newName;
เช่นเดียวกัน ผมแทนที่ reader ทั้งหลายด้วย getter ที่เหมาะสม
class Organization…
คลิกที่นี่เพื่อดู code
get name() {return this._data.name;}
client…
คลิกที่นี่เพื่อดู code
result += `<h1>${getOrganization().name}</h1>`;
เมื่อทำเสร็จแล้ว ผมก็ทำตามที่ขู่ไว้—ให้ function ที่ฟังดูน่าเกลียดนั้นมีอายุสั้น
คลิกที่นี่เพื่อดู code
function getRawDataOfOrganization() {return organization._data;}
function getOrganization() {return organization;}
ผมยังรู้สึกอยากย้าย field _data เข้าไปไว้ใน object โดยตรง
คลิกที่นี่เพื่อดู code
class Organization {
constructor(data) {
this._name = data.name;
this._country = data.country;
}
get name() {return this._name;}
set name(aString) {this._name = aString;}
get country() {return this._country;}
set country(aCountryCode) {this._country = aCountryCode;}
}
วิธีนี้ได้ข้อดีคือตัดความเชื่อมโยงกับ record ข้อมูลต้นทาง ซึ่งมีประโยชน์ถ้ามี reference ไปยัง record นั้นหลุด รอดไปใช้ที่อื่น ซึ่งจะทำลาย encapsulation ถ้าผมไม่ย้ายข้อมูลมาเป็น field ย่อย ๆ ผมควรคัดลอก _data ตอนที่กำหนดค่าให้มัน
Example: Encapsulating a Nested Record (ตัวอย่าง: การห่อหุ้มเรคคอร์ดที่ซ้อนกัน)
ตัวอย่างข้างต้นดู record แบบตื้น ๆ แต่ถ้าข้อมูลซ้อนกันลึก ๆ เช่นข้อมูลที่มาจาก เอกสาร JSON ล่ะ? ขั้นตอนหลักของ refactoring ยังใช้ได้เหมือนเดิม และผมต้องระวังเรื่องการอัปเดตพอ ๆ กัน แต่การ อ่านค่ามีตัวเลือกเพิ่มขึ้นมาบ้าง
ตัวอย่างเช่น ข้อมูลที่ซ้อนกันมากขึ้นเล็กน้อย: collection ของลูกค้าที่เก็บใน hashmap โดยใช้ customer ID เป็น index
คลิกที่นี่เพื่อดู code
"1920": {
name: "martin",
id: "1920",
usages: {
"2016": {
"1": 50,
"2": 55,
// remaining months of the year
},
"2015": {
"1": 70,
"2": 63,
// remaining months of the year
}
}
},
"38673": {
name: "neal",
id: "38673",
// more customers in a similar form
เมื่อข้อมูลซ้อนกันมากขึ้น การอ่านและเขียนก็อาจต้องเจาะลึกลงไปใน data structure
sample update…
คลิกที่นี่เพื่อดู code
customerData[customerID].usages[year][month] = amount;
sample read…
คลิกที่นี่เพื่อดู code
function compareUsage (customerID, laterYear, month) {
const later = customerData[customerID].usages[laterYear][month];
const earlier = customerData[customerID].usages[laterYear - 1][month];
return {laterAmount: later, change: later - earlier};
}
เพื่อห่อหุ้มข้อมูลนี้ ผมก็เริ่มด้วย Encapsulate Variable ( 132 ) .
คลิกที่นี่เพื่อดู code
function getRawDataOfCustomers() {return customerData;}
function setRawDataOfCustomers(arg) {customerData = arg;}
sample update…
คลิกที่นี่เพื่อดู code
getRawDataOfCustomers()[customerID].usages[year][month] = amount;
sample read…
คลิกที่นี่เพื่อดู code
function compareUsage (customerID, laterYear, month) {
const later = getRawDataOfCustomers()[customerID].usages[laterYear][month];
const earlier = getRawDataOfCustomers()[customerID].usages[laterYear - 1][month];
return {laterAmount: later, change: later - earlier};
}
จากนั้นผมก็สร้าง class สำหรับ data structure โดยรวม
class CustomerData {
constructor(data) {
this._data = data;
}
}
top level…
คลิกที่นี่เพื่อดู code
function getCustomerData() {return customerData;}
function getRawDataOfCustomers() {return customerData._data;}
function setRawDataOfCustomers(arg) {customerData = new CustomerData(arg);}
ส่วนที่สำคัญที่สุดที่ต้องจัดการคือการอัปเดต ดังนั้นระหว่างที่ผมไล่ดู caller ของ getRawDataOfCustomers , ผมจะโฟกัสที่จุดที่มีการแก้ข้อมูล เพื่อเตือนความจำ นี่คือโค้ดอัปเดตอีกครั้ง:
sample update…
คลิกที่นี่เพื่อดู code
getRawDataOfCustomers()[customerID].usages[year][month] = amount;
กลไกทั่วไปในตอนนี้คือให้คืนข้อมูลลูกค้าทั้งชุดแล้วใช้ accessor โดยสร้างขึ้นใหม่ถ้าจำเป็น แต่ผมไม่มี setter สำหรับการอัปเดตแบบนี้บนตัวลูกค้า และการอัปเดตนี้ต้องเจาะลึกเข้าไปในโครงสร้าง ดังนั้นเพื่อสร้าง setter ผมจึงเริ่มจากใช้ Extract Function ( 106 ) กับ code ที่เจาะลึกเข้าไปใน data structure
sample update…
คลิกที่นี่เพื่อดู code
setUsage(customerID, year, month, amount);
top level…
คลิกที่นี่เพื่อดู code
function setUsage(customerID, year, month, amount) {
getRawDataOfCustomers()[customerID].usages[year][month] = amount;
}
จากนั้นผมใช้ Move Function ( 198 ) เพื่อย้ายมันเข้าไปใน class ข้อมูลลูกค้าใหม่ (Data Class)
sample update…
คลิกที่นี่เพื่อดู code
getCustomerData().setUsage(customerID, year, month, amount);
class CustomerData…
คลิกที่นี่เพื่อดู code
setUsage(customerID, year, month, amount) {
this._data[customerID].usages[year][month] = amount;
}
เวลาทำงานกับ data structure ขนาดใหญ่ ผมชอบโฟกัสที่การอัปเดต การทำให้มันมองเห็นได้และรวมอยู่ ในที่เดียวคือส่วนที่สำคัญที่สุดของ encapsulation
ถึงจุดหนึ่งผมจะคิดว่ารวบรวมได้ครบแล้ว—แต่จะมั่นใจได้อย่างไร? มีวิธีตรวจสอบอยู่สองสามวิธี วิธีหนึ่งคือการ แก้ getRawDataOfCustomers ให้คืนสำเนาแบบ deep copy ของข้อมูล ถ้า test coverage ดีพอ test สักตัวควรพังถ้าผมพลาดการแก้ไข บางจุด
top level…
คลิกที่นี่เพื่อดู code
function getCustomerData() {return customerData;}
function getRawDataOfCustomers() {return customerData.rawData;}
function setRawDataOfCustomers(arg) {customerData = new CustomerData(arg);}
class CustomerData…
คลิกที่นี่เพื่อดู code
get rawData() {
return _.cloneDeep(this._data);
}
ผมใช้ library lodash เพื่อทำสำเนาแบบ deep copy
อีกวิธีคือคืน read-only proxy ให้ data structure proxy แบบนี้จะโยน exception ถ้า client code พยายามแก้ไข object ต้นทาง บางภาษาทำเรื่องนี้ได้ง่าย แต่ใน JavaScript มันปวดหัวมาก ผมจึง ขอฝากไว้เป็นการบ้านให้ผู้อ่านลองทำ ผมอาจคัดลอกข้อมูลแล้ว freeze แบบวนซ้ำ (recursively) เพื่อจับการ แก้ไขใด ๆ ก็ได้
การจัดการกับการอัปเดตนั้นมีค่า แต่ส่วนของผู้อ่านล่ะ? ตรงนี้มีตัวเลือกอยู่สองสามทาง
ตัวเลือกแรกคือทำแบบเดียวกับที่ทำกับ setter คือแยกการอ่านทั้งหมดออกเป็น function ของมันเอง แล้วย้ายมันเข้าไปใน class ข้อมูลลูกค้า (Data Class)
class CustomerData…
คลิกที่นี่เพื่อดู code
usage(customerID, year, month) {
return this._data[customerID].usages[year][month];
}
top level…
คลิกที่นี่เพื่อดู code
function compareUsage (customerID, laterYear, month) {
const later = getCustomerData().usage(customerID, laterYear, month);
const earlier = getCustomerData().usage(customerID, laterYear - 1, month);
return {laterAmount: later, change: later - earlier};
}
ข้อดีใหญ่ของวิธีนี้คือมันทำให้ customerData มี API ที่ชัดเจนซึ่งครอบคลุมการใช้งานทั้งหมดของมัน ผมสามารถดู class แล้วเห็นการใช้งานข้อมูลทั้งหมดได้ แต่นี่อาจกลายเป็น code จำนวนมากสำหรับกรณีพิเศษหลายกรณี ภาษาสมัยใหม่มีเครื่องมือที่ดีสำหรับการเจาะลึก data structure แบบ list-and-hash [ mf-lh ] ดังนั้นการให้ client ทำงานกับ data structure แบบนี้โดยตรงจึงมีประโยชน์
ถ้า client ต้องการ data structure ผมก็แค่ส่งข้อมูลจริงให้ไป แต่ปัญหาคือมัน ไม่มีทางป้องกันไม่ให้ client แก้ไขข้อมูลโดยตรง ซึ่งทำลายจุดประสงค์ทั้งหมดของการห่อหุ้ม การอัปเดตไว้ใน function ดังนั้นสิ่งที่ง่ายที่สุดคือการส่งสำเนาของข้อมูลต้นทาง โดยใช้ method rawData ที่ผมเขียนไว้ก่อนหน้านี้
class CustomerData…
คลิกที่นี่เพื่อดู code
get rawData() {
return _.cloneDeep(this._data);
}
top level…
คลิกที่นี่เพื่อดู code
function compareUsage (customerID, laterYear, month) {
const later = getCustomerData().rawData[customerID].usages[laterYear][month];
const earlier = getCustomerData().rawData[customerID].usages[laterYear - 1][month];
return {laterAmount: later, change: later - earlier};
}
ถึงแม้จะง่าย แต่ก็มีข้อเสีย ปัญหาที่ชัดเจนที่สุดคือต้นทุนการคัดลอก data structure ขนาดใหญ่ ซึ่งอาจกลายเป็นปัญหาเรื่อง performance อย่างไรก็ตาม เช่นเดียวกับเรื่องอื่น ๆ แบบนี้ ต้นทุนด้าน performance อาจยอมรับได้—ผมควรวัดผลกระทบก่อนที่จะเริ่มกังวล และอาจเกิดความสับสน ถ้า client คาดหวังว่าการแก้ไขข้อมูลที่คัดลอกไปจะแก้ไขข้อมูลต้นฉบับด้วย ในกรณีนั้น read-only proxy หรือการ freeze ข้อมูลที่คัดลอกไว้อาจช่วยเตือนด้วย error ที่เป็นประโยชน์ถ้าพวกเขาทำแบบนั้น
อีกตัวเลือกหนึ่งใช้แรงงานมากกว่าแต่ให้การควบคุมสูงสุด: ใช้ Encapsulate Record แบบวนซ้ำ ด้วยวิธีนี้ ผมจะเปลี่ยน เรคคอร์ดลูกค้าให้เป็น class ของมันเอง แล้วใช้ Encapsulate Collection ( 170 ) กับ usages และสร้าง class สำหรับ usage จากนั้นผมก็บังคับควบคุมการอัปเดตด้วย accessor โดยอาจใช้ Change Reference to Value ( 252 ) กับ object usage แต่การทำแบบนี้กับ data structure ขนาดใหญ่อาจใช้ความพยายามมาก—และไม่จำเป็นจริง ๆ ถ้าผมไม่ได้ เข้าถึง data structure มากขนาดนั้น บางครั้งการผสม getter กับ class ใหม่อย่างชาญฉลาดก็ใช้ได้ เช่นใช้ getter เจาะลึกเข้าไปในโครงสร้าง แต่คืน object ที่ครอบโครงสร้างนั้นไว้แทนที่จะคืน ข้อมูลที่ไม่ได้ห่อหุ้ม (unencapsulated data) ผมเคยเขียนเกี่ยวกับเรื่องแบบนี้ในบทความ “Refactoring Code to Load a Document” [ mf-ref-doc ] .