Design patterns are just for Java™ architects — at least that’s what you may have been led to believe. In fact, design patterns are useful for everyone. If these tools aren’t exclusive to architecture astronauts, what are they, and why are they useful in PHP applications? This article explains.
1. Is easily testable
2. Constains no duplication
3. Has intentional coupling, strong cohesion, and clarity.
A typicaly Singleton implementation in C# looks like below:
using System;
public sealed class Singleton
{
private static volatile Singleton instance;
private static object syncRoot = new Object();
private Singleton() {}
public static Singleton Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new Singleton();
}
}
return instance;
}
}
}
[MSDN]This approach ensures that only one instance is created and only when the instance is needed. Also, the variable is declared to be volatile to ensure that assignment to the instance variable completes before the instance variable can be accessed. Lastly, this approach uses a syncRoot instance to lock on, rather than locking on the type itself, to avoid deadlocks. This double-check locking approach solves the thread concurrency problems while avoiding an exclusive lock in every call to the Instance property method. It also allows you to delay instantiation until the object is first accessed.
Above solution looks perfect, however, actually the ‘Double-Checked Locking’ can be still broken under certain multi-processor machine. A recommended way to implement Singleton correctly is below:
public sealed class Singleton
{
Singleton(){}
public static Singleton Instance
{
get
{
return Nested.instance;
}
}
static class Nested
{
internal static readonly Singleton instance = new Singleton();
}
}
Above code looks weird, but correct. It leverages .Net’s Class Loader to achieve thread-safe.
Please check out Scott Bain’s http://www.netobjectivesrepository.com. This is a site where you can find authoritative information about design patterns.
This respository is sponsored by Net Objectives, a Seattle-based organization dedicated to training, coaching, and consulting on software design, agile methodologies, test-driven development, lean software process, and scrum.
You are free to use this material for your edification and study, and, optionally, you may contribute your views on patterns and forces in software development by signing up for a free membership. For other online resources provided by Net Objectives, please visit Net Objectives Online. The site manager is Scott Bain.
Knowing the OO basics doesn’t make you a good OO designer. You need to know OO principles and Patterns. Following is cited from the book Head First Design Patterns.
OO Basics
- Abstraction
- Encapsulation
- Polymorphism
- Inheritance
OO Principles
- Encapsulate variation
- Favor composition over inheritence
- Program to interfaces, not implementations
OO Patterns
- Strategy- defines a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
- Bridge…