|
PHP5中文手册
PatternsPatterns are ways to describe best practices and good designs. They show a flexible solution to common programming problems. FactoryThe Factory pattern allows for the instantiation of objects at runtime. It is called a Factory Pattern since it is responsible for "manufacturing" an object. A Parameterized Factory receives the name of the class to instantiate as argument. Example#1 Parameterized Factory Method
<?php Defining this method in a class allows drivers to be loaded on the fly. If the Example class was a database abstraction class, loading a MySQL and SQLite driver could be done as follows:
<?php SingletonThe Singleton pattern applies to situations in which there needs to be a single instance of a class. The most common example of this is a database connection. Implementing this pattern allows a programmer to make this single instance easily accessible by many other objects. Example#2 Singleton Function
<?php This allows a single instance of the Example class to be retrieved.
<?php |