Introduction to PHP Design Patterns
What are Design Patterns?
- Solution to common development problems
- Reusable successful design and architectures
- Common language for developers
What Design Patterns are not?
- The panacea for solving all development problems
- Ready to use code scripts
- Cut & Paste solutions
Why Design Patterns?
- Help you to reuse successful design and architectures
- Help you to choose web design alternatives if click this over here now
- Help you to solve problems you haven’t seen before
- Give a common vocabulary
- Let you communicate quickly and unambiguously
- Make a system more reusable
One of those magical performance cum code reduction issues is “Design Pattern” which was introduced by Eric Gamma and his three other friends in the book Design Patterns in 1972. Because of four authors, the book was introduced as written by Gang of Four or simply Goff. In that legendary book, Gang of Four introduced several patterns to minimize the amount of code as well as to introduce effective coding practice. In this chapter we will learn some of those patterns to implement in PHP.
The Gang of Four
- Erich Gamma
- Richard Helm
- Ralph Johnson
- John Vlissides
Essential Elements
Pattern name: A descriptive name for the pattern, in a word or two
Problem: Describes the problem the pattern applies to
Solution:
¨ Describes the elements and the resources along with their relationship and responsibilities for solving the problem.
¨ This is just an abstract description; the implementation varies according to the programming language
Consequences: Results and trade-off
Golden Rules
- Program to an interface, not an implementation
- Some languages takes this rule to the next level with Duck Typing
- Favor Object Composition over Inheritance
- Black Box approach
- Delegate
How to Select a Design Pattern
- Consider how to design patterns solve design problems
- Scan Intent section
- Study how patterns interrelate
- Study patterns of like purpose
- Examine a cause of redesign
- Consider what should be variable in your design
How to Use a Design Pattern
- Read the pattern once through for an overview
- Go back and study the Structure, Participants and Collaboration sections
- Look at the Sample Code section to see a concrete example of the pattern in code
- Choose names for pattern participants that are meaningful in the application context
- Define the classes
- Define application-specific names for operations in the pattern
- Implement the operations to carry out the responsibilities and collaborations in the patterns
How to NOT Use a Design Pattern
- Don’t try to rewrite your application to fit a Design Pattern, select the best Pattern according to your needs
- Don’t use a Design Pattern because it’s cool… use a Pattern because you need it
- Don’t copy/paste language-specific Pattern implementations, understand how the Pattern works and how you can implement it in your application
Frameworks vs Design Patterns
- Design patterns are more abstract than frameworks
- Design patterns are smaller architectural elements than frameworks
- Design patterns are less specialized than frameworks
- A typical framework contains several design patterns, but the reverse is never true
Design Pattern Classification
Design Patterns by Purpose
- Creational Patterns
Concern the process of object creation
- Structural Patterns
Deal with the composition of object and classes
- Behavioral Patterns
Characterize the way in which classes or objects interact
- Concurrency Patterns
- Architectural Patterns
…
Design Patterns by Scope
Class Patterns
- Deal with relationships between classes and their subclasses.
Ex. Factory Method, Adapter, Template method…
Object Patterns
- Deal with object relationships.
- The most part of patterns are in the object scope.
Ex. Singleton, Adapter, Decorator, Proxy, Iterator, Observer…
Design Patterns
Architectural
- MVC
Creational
- Singleton
- Factory Method
- Lazy initialization
Structural
- Adapter
- Proxy
Behavioral
- Iterator
- Observer
- Strategy
- Template Method
More
- Registry
- Mock Object
Design Patterns in Action
Pattern Template
- Problem & Solution
- Considerations
- In pictures
- Implementation & Examples
- In the Wild
- Using and Abusing

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first Fifteen years a number of leadership positions at AmarBebsha Ltd as a CTO, Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now I’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.
Hi,
Would you like to add some code to demonstrate where/how and which to use design patterns? Like processing orders (shop), validating forms/input data, fascade for complex system with multiple models/tables….?
GUY I LIKE THIS SITE . I WILL SEND U A MAIL LATER.
guy i like ur site. i wl contact u later.
very useful