Showing posts with label Design Pattern. Show all posts
Showing posts with label Design Pattern. Show all posts

23 GOF Design Pattern

Creation Patterns

Structural Patterns
Behavioral Patterns
Reference:

Visitor Pattern

เป็นส่วนที่ไปทำงานบนระบบอื่น เพื่อให้ระบบอื่นเรียกใช้




Mediator Pattern




Interpreter Pattern

เป็นการแปลงข้อมูลจาก format หนึ่งไปยังอีก format หนึ่ง หรือการ convert ข้อมูล


ตัวอย่างของ Interpreter pattern เช่น parser, XML parser

Builder Pattern




Bridge Pattern

Bridge pattern will decouple an abstraction from its implementation so that the two can vary independently
แต่จะทำให้ระบบมีความซับซ้อนขึ้น และมี performance ต่ำลง แต่ว่าจะช่วยให้ระบบมีความยืดหยุ่นมากขึ้น



State Pattern

The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.




Proxy Pattern

โดยเมื่อก่อนใช้คำว่า Stub โดยมี Skeleton เป็น implementation หรือ object เป็นตัวที่ตรงกันข้ามกับ Facade คือเป็นตัวกั้นทางออก กั้น request จากภายในระบบกับระบบภายนอก




Observer Pattern

The Observer Pattern defines a one-to-many dependency between objects so theat when one object changes state, all of its dependents are notified and updated automatically.

Observer เช่น เรา subscribe blog เอาไว้ เมื่อมีการ publish subject ใหม่ ก็จะส่ง subject นั้นๆ มาให้ เป็นลักษณะเดียวกับ Event-base system, Event-driven system หรือที่ใช้กันใน Listener
ข้อเสียคือถ้ามี hacker เข้ามา hacker ก็จะเห็นข้อมูลเหมือนกับ observer 

Observer มีอยู่ 2 แบบ
  1. publish
  2. subscribe




Template Pattern

เป็นการทำ template ไว้ให้เลย



Singleton Pattern

The Singleton Pattern ensures a clas has only one instance, and provides a global point of access to it.





Factory Pattern

The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.




The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Abstract Factory จะเป็น Interface สำหรับ Factory Class เพื่อให้ Factory Class ในระบบมี pattern เดียวกัน เช่น มีชื่อ Factory Method เดียวกัน




Facade Pattern

เป็นตัวกั้น request จากภายนอกที่จะเข้ามาเรียกการทำงานภายในระบบ



ตัวอย่างของ Facade pattern เช่น Front Controller ซึ่งเป็นตัวกั้น request จาก UI กับระบบภายใน

Decorator Pattern

The Decorator Pattern attaches additional reponsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.




Iterator Pattern




Composite Pattern

Compose objects into tree structures to represent part




Strategy Pattern

The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
มักใช้ร่วมกันกับ Bridge pattern




Strategy Pattern เป็น pattern ที่เปลี่ยนแนวความคิดจากการใช้ Inheritance มาเป็น Composition แทนซึ่งมีความยืดหยุ่นกว่า และเป็น pattern ที่ใช้ในอีกหลายๆ pattern ด้วย

State Pattern

The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.




ดูแค่ structure จะเห็นว่า state pattern คล้ายกับ strategy pattern มากเลย เรียกว่าเหมือนกะเป๊งเลยดีกว่า แต่ว่า จุดประสงค์ของ 2 pattern ไม่เหมือนกัน state pattern จะเน้นที่ context object หรือ ตัว object หลักของแต่ละ state มีการเปลี่ยนพฤติกรรม ได้ โดยที่ context ไม่รู้ คือตัว state จะเปลี่ยนไปตามแต่ method ที่ถูกเรียก ในขณะีที่ strategy จะถูกโปรแกรมกำหนดให้