Factory pattern ให้วิธีสร้างอ็อบเจ็กต์โดยไม่ต้องระบุคลาสที่แน่นอน เป็นเมธอดหรือคลาสที่จัดการการสร้างอ็อบเจ็กต์ โดยตัดสินใจว่าจะสร้าง type ใด มันรวมศูนย์และทำให้การสร้างอ็อบเจ็กต์เป็นนามธรรม โดยแยกโค้ดออกจากคลาสที่เป็นรูปธรรม
Factory pattern ทำอะไร
FACTORY → encapsulate object CREATION (instead of calling constructors directly):
→ a factory method/class decides WHICH class to instantiate and creates it
→ the caller asks the factory for an object (by type/parameter) → gets the right object
WITHOUT knowing/specifying the concrete class
→ decouples code from CONCRETE classes; centralizes creation logic
