2015年1月26日星期一

Coder on Code: Design Patterns in PHP: Adapters


The Coder on Code site has posted a new tutorial covering the Adapter design pattern in detail. They talk about what the pattern is, what it can be useful for and include some code to illustrate.



The adapter pattern also referred as the wrapper pattern, I find that wrapper is a more fitting name since it describes clearly what this pattern does; it encapsulates the functionality of a class or object into a class with a common public interfaces. [...] Adapters are one of the easiest patterns to comprehend and at the same time one of the most useful ones.


He starts with some of the basic definitions of terms involved in the pattern: client, adapter and adapteee. His example centers around a notification manager class that lets you switch types between Twitter, Email and SMS messaging. His initial code has all of the message types handled in one class method. He shows how to refactor this out to an interface and a set of child classes, each with the corresponding handling in a "sendNotification" method. These are then used by an adapter in the main class to send the given message. This simplifies the main messenger class and contributes to the overall improvement of architecture and testability of the application.


Link: http://coderoncode.com/2015/01/25/design-patterns-in-php-adapters.html

没有评论:

发表评论