ลองมาดูการออกแบบนี้กัน Factory ที่เรากำลังพิจารณาอยู่นี้ตั้งอยู่บน Calendar และใช้สำหรับสร้าง CalendarEntry instance ทีม CollabOvation จะพาเราไปดูการ implement กัน
ต่อไปนี้คือ test ที่พัฒนาขึ้นเพื่อสาธิตวิธีการใช้งาน Factory Method ของ Calendar:
public class CalendarTest extends DomainTest {
private CalendarEntry calendarEntry;
private CalendarEntryId calendarEntryId;
...
public void testCreateCalendarEntry() throws Exception {
Calendar calendar = this.calendarFixture();
DomainRegistry.calendarRepository().add(calendar);
DomainEventPublisher
.instance()
.subscribe(
new DomainEventSubscriber<CalendarEntryScheduled>() {
public void handleEvent(
CalendarEntryScheduled aDomainEvent) {
calendarEntryId = aDomainEvent.calendarEntryId();
}
public Class<CalendarEntryScheduled>
subscribedToEventType() {
return CalendarEntryScheduled.class;
}
});
calendarEntry =
calendar.scheduleCalendarEntry(
DomainRegistry
.calendarEntryRepository()
.nextIdentity()
new Owner(
"jdoe",
"John Doe",
"[email protected]"),
"Sprint Planning",
"Plan sprint for first half of April 2012.",
this.tomorrowOneHourTimeSpanFixture(),
this.oneHourBeforeAlarmFixture(),
this.weeklyRepetitionFixture(),
"Team Room",
new TreeSet<Invitee>(0));
DomainRegistry.calendarEntryRepository().add(calendarEntry);
assertNotNull(calendarEntryId);
assertNotNull(calendarEntry);
...
}
}
มีพารามิเตอร์เก้าตัวถูกส่งเข้าไปใน scheduleCalendarEntry() แต่ดังที่จะเห็นในภายหลัง constructor ของ CalendarEntry ต้องการพารามิเตอร์รวมทั้งหมด 11 ตัว เดี๋ยวเราจะมาพิจารณากันถึงประโยชน์ของเรื่องนี้ หลังจากสร้าง CalendarEntry ตัวใหม่สำเร็จแล้ว client จะต้องนำมันไปเพิ่มลงใน Repository ของตน หากไม่ทำเช่นนั้น instance ใหม่นี้ก็จะถูกปล่อยให้ garbage collector กวาดทิ้งไป
assertion แรกแสดงให้เห็นว่า CalendarEntryId ที่ถูก publish พร้อมกับ Event จะต้องไม่เป็น null ซึ่งยืนยันได้ว่า Event ถูก publish สำเร็จ ไม่ใช่ว่า client ที่เรียกใช้ Calendar โดยตรงจะต้องมา subscribe Event นั้นจริง ๆ แต่ test นี้แสดงให้เห็นว่า Event CalendarEntryScheduled ถูก publish ขึ้นจริง
instance ของ CalendarEntry ตัวใหม่ก็ต้องไม่เป็น null เช่นกัน เราอาจเพิ่ม assertion อื่น ๆ ได้อีก แต่สองอย่างที่เพิ่งแสดงไปนั้นสำคัญที่สุดสำหรับการบันทึกการออกแบบ Factory Method และวิธีที่ client ใช้งานมัน
ทีนี้ลองมาดู implementation ของ Factory Method กัน:
package com.saasovation.collaboration.domain.model.calendar;
public class Calendar extends Entity {
...
public CalendarEntry scheduleCalendarEntry(
CalendarEntryId aCalendarEntryId,
Owner anOwner,
String aSubject,
String aDescription,
TimeSpan aTimeSpan,
Alarm anAlarm,
Repetition aRepetition,
String aLocation,
Set<Invitee> anInvitees) {
CalendarEntry calendarEntry =
new CalendarEntry(
this.tenant(),
this.calendarId(),
aCalendarEntryId,
anOwner,
aSubject,
aDescription,
aTimeSpan,
anAlarm,
aRepetition,
aLocation,
anInvitees);
DomainEventPublisher
.instance()
.publish(new CalendarEntryScheduled(...));
return calendarEntry;
}
...
}
Calendar ทำการ instantiate Aggregate ตัวใหม่ขึ้นมา นั่นคือ CalendarEntry จากนั้น instance ใหม่นี้จะถูกส่งกลับคืนให้กับ client ภายหลังการ publish Event CalendarEntryScheduled (รายละเอียดของ Event ที่ถูก publish ไม่ได้สำคัญต่อการอภิปรายในตอนนี้) คุณอาจสังเกตได้ว่าตอนต้นของ method นี้ไม่มี guard เลย ซึ่งอันที่จริงไม่จำเป็นต้องใส่ guard ให้ Factory Method เองก็ได้ เพราะ constructor ของ Value parameter แต่ละตัวและ constructor ของ CalendarEntry รวมถึง setter method ที่ constructor เรียกใช้แบบ self-delegation ต่างก็ให้ guard ที่จำเป็นครบถ้วนอยู่แล้ว (ดู Entities (5) สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับ self-delegation และ guard) หากต้องการความระมัดระวังเป็นสองเท่า จะเพิ่ม guard ไว้ตรงนี้ก็ได้
ทีมออกแบบชื่อของ method ให้ยึดตาม Ubiquitous Language โดย Domain experts ร่วมกับสมาชิกที่เหลือในทีมได้พูดคุยถึงสถานการณ์ต่อไปนี้:
Calendar ทำหน้าที่จัดตารางเวลาให้กับ calendar entries
หากการออกแบบของเรารองรับเฉพาะ public constructor บน CalendarEntry ความสามารถในการสื่อความหมายของ model ก็จะลดลง และเราจะไม่สามารถจำลองส่วนนั้นของ language ของ domain มาได้ การออกแบบแบบนี้จำเป็นต้องซ่อน full Aggregate constructor ไว้จาก client เราจึงประกาศ constructor ด้วยขอบเขต protected ซึ่งบังคับให้ client ต้องใช้งาน Factory Method scheduleCalendarEntry() บน Calendar:
public class CalendarEntry extends Entity {
...
protected CalendarEntry(
Tenant aTenant, CalendarId aCalendarId,
CalendarEntryId aCalendarEntryId, Owner anOwner,
String aSubject, String aDescription, TimeSpan aTimeSpan,
Alarm anAlarm, Repetition aRepetition, String aLocation,
Set<Invitee> anInvitees) {
...
}
...
}
แม้จะได้ข้อดีในแง่ของการสร้างวัตถุอย่างรอบคอบ ภาระการใช้งานของ client ที่ลดลง และ model ที่สื่อความหมายได้ดี แต่การใช้ Factory Method ของ Calendar ก็มีข้อเสียตรงที่เพิ่ม overhead ด้าน performance ขึ้นเล็กน้อย เช่นเดียวกับ Aggregate Factory Method อื่น ๆ ในลักษณะนี้ Calendar จะต้องถูกดึงออกมาจาก persistence store ก่อนจึงจะนำมาใช้สร้าง CalendarEntry ได้ ค่าใช้จ่ายที่เพิ่มขึ้นเล็กน้อยนี้อาจคุ้มค่ามาก แต่เมื่อ traffic ใน Bounded Context นี้เพิ่มขึ้น ทีมก็จะต้องชั่งน้ำหนักผลที่จะตามมาอย่างรอบคอบ
สำหรับประโยชน์ของการใช้ Factory นั้น สิ่งที่เกี่ยวข้องโดยตรงคือพารามิเตอร์สองตัวของ constructor CalendarEntry ไม่ได้ถูกส่งมาจาก client ด้วยความที่ constructor ต้องการพารามิเตอร์ทั้งหมด 11 ตัว การออกแบบนี้จึงช่วยแบ่งเบาภาระของ client ให้ส่งเพียงเก้าตัว และพารามิเตอร์ที่ต้องส่งทั้งเก้าตัวนั้นส่วนใหญ่ client ก็สร้างขึ้นมาได้ไม่ยากนัก (ยอมรับว่าการสร้าง Set ของ instance Invitee จะยุ่งยากกว่าเล็กน้อย แต่ก็ไม่ใช่ความผิดของ Factory Method ทีมควรคิดถึงการออกแบบ facility ที่ช่วยให้การเตรียม Set นี้สะดวกขึ้น ซึ่งอาจชี้ไปที่การสร้าง Factory เฉพาะขึ้นมาอีกตัว)
กระนั้น Tenant และ CalendarId ที่สัมพันธ์กันนั้นจะถูกส่งให้จาก Factory Method เพียงแห่งเดียวเท่านั้น และนี่คือจุดที่เรามั่นใจได้ว่า instance ของ CalendarEntry จะถูกสร้างขึ้นสำหรับ Tenant ที่ถูกต้องและเชื่อมโยงกับ Calendar ที่ถูกต้องเท่านั้น
ทีนี้ลองมาพิจารณาอีกหนึ่งตัวอย่างจาก Collaboration Context กัน