tech, tunes, and other stuff

Basic Design Patterns

Patterns represent a standardized way to structure an application or part of an application in order to solve a specific problem.

Model-View-Controller Pattern

The model-view-controller (MVC) pattern splits an application into three distinct parts: the model, the view, and the controller.   This is done to reduce the complexity of the code and increase its maintainability.

The model is the part of the application responsible for handling the data.  Typically it will read and write data to the database.  The view is the part of the application that presents the application to the user.  It is responsible for the way the application looks, for getting input from the user, and for presenting data to the user.  The controller is responsible for the application’s business logic.

For example, the client goes to a web page with a browser.  The page that comes up is one with text fields for a user name and a password.  Finally there is a submit button for the user to click when finished filling in the user name and password fields.  This is a view.  It gets the input from the user, and when the user clicks the submit button the form passes the user name and password data to a controller.  This controller validates the data passed in, and if it is valid calls on a model to lookup the user data in the database to see if it matches any known users with that user name and password.  It then returns the success or fail code back to the controller who then presents the client with a view based on the result.  If there was no such user found, then the controller might present the client with a view showing an error message.  If the user was found, then the client might be considered logged in and then presented with a view of menu options.

Singleton Pattern

A singleton pattern provides access to a single resource without the possibility of the resource ever being duplicated.  If you go to the PHP manual on patterns here you will see an example of a singleton pattern.  The singleton pattern relies on a static variable.  If the variable is set then a singleton object is already in use and that instance is returned.   If the static variable is not set, then a new object is created and returned.

Use of singleton objects in PHP web applications are controversial for several reasons.  Some have argued for PHP the pattern adds complexity, is unnecessary in an application with no scope beyond the request, and evades garbage collection because of the global static variable used in the pattern.

Factory Pattern

A factory pattern are used to create (manufacture) instances of objects.  A good use for the factory pattern would be for creating database handler objects.  If you go to the PHP manual on patterns here you will see an example of a factory pattern.  In the example the factory is called with a lone parameter telling it to create either a MySQL or SQLite driver object.  The factory instantiates the appropriate  object and returns it to the caller.

Registry Pattern

The registry pattern is a way to store objects within an application.  Basically it is an object used to keep track of other objects in some sort of data structure such as a hash map.  Most of the time it utilizes a Singleton Pattern (called a Singleton Registry), but it doesn’t have to.  The problem with Singleton Registries is that a client that fetches an object from a Singleton Registry is not only coupled to the Registry, it is also coupled to the object it fetches.

A good discussion with an example of the Registry Pattern can be found:  here.

ActiveRecord Pattern

The ActiveRecord Pattern is designed to help how a web application interacts with a database by embedding the knowledge of how to interact with a database directly into the class performing the interaction.  The pattern is easy to implement, but the downside is that it leads to a high degree of coupling between the implementing class to the structure of the database.