Skip to content

Latest commit

 

History

History
273 lines (215 loc) · 9.92 KB

Design-Patterns.md

File metadata and controls

273 lines (215 loc) · 9.92 KB
layout title sitemap
page
Design Patterns
true

Design Patterns

There are numerous ways to structure the code and project for your web application, and you can put as much or as little thought as you like into architecting. But it is usually a good idea to follow common patterns because it will make your code easier to manage and easier for others to understand.

Factory

One of the most commonly used design patterns is the factory pattern. In this pattern, a class simply creates the object you want to use. Consider the following example of the factory pattern:

{% highlight php %}

vehicleMake = $make; $this->vehicleModel = $model; } public function getMakeAndModel() { return $this->vehicleMake . ' ' . $this->vehicleModel; } } class AutomobileFactory { public static function create($make, $model) { return new Automobile($make, $model); } } // have the factory create the Automobile object $veyron = AutomobileFactory::create('Bugatti', 'Veyron'); print_r($veyron->getMakeAndModel()); // outputs "Bugatti Veyron" {% endhighlight %} This code uses a factory to create the Automobile object. There are two possible benefits to building your code this way; the first is that if you need to change, rename, or replace the Automobile class later on you can do so and you will only have to modify the code in the factory, instead of every place in your project that uses the Automobile class. The second possible benefit is that if creating the object is a complicated job you can do all of the work in the factory, instead of repeating it every time you want to create a new instance. Using the factory pattern isn't always necessary (or wise). The example code used here is so simple that a factory would simply be adding unneeded complexity. However if you are making a fairly large or complex project you may save yourself a lot of trouble down the road by using factories. * [Factory pattern on Wikipedia](https://en.wikipedia.org/wiki/Factory_pattern) ## Singleton When designing web applications, it often makes sense conceptually and architecturally to allow access to one and only one instance of a particular class. The singleton pattern enables us to do this. {% highlight php %} output = $outputType; } public function loadOutput() { return $this->output->load(); } } {% endhighlight %} The calling client class above has a private property which must be set at runtime and be of type 'OutputInterface' once this property is set a call to loadOutput() will call the load() method in the concrete class of the output type that has been set. {% highlight php %} setOutput(new ArrayOutput()); $data = $client->loadOutput(); // Want some JSON? $client->setOutput(new JsonStringOutput()); $data = $client->loadOutput(); {% endhighlight %} * [Strategy pattern on Wikipedia](http://en.wikipedia.org/wiki/Strategy_pattern) ## Front Controller The front controller pattern is where you have a single entrance point for you web application (e.g. index.php) that handles all of the requests. This code is responsible for loading all of the dependencies, processing the request and sending the response to the browser. The front controller pattern can be beneficial because it encourages modular code and gives you a central place to hook in code that should be run for every request (such as input sanitization). * [Front Controller pattern on Wikipedia](https://en.wikipedia.org/wiki/Front_Controller_pattern) ## Model-View-Controller The model-view-controller (MVC) pattern and its relatives HMVC and MVVM lets you break up code into logical objects that serve very specific purposes. Models serve as a data access layer where data is fetched and returned in formats usable throughout your application. Controllers handle the request, process the data returned from models and load views to send in the response. And views are display templates (markup, xml, etc) that are sent in the response to the web browser. MVC is the most common architectural pattern used in the popular [PHP frameworks](https://github.com/codeguy/php-the-right-way/wiki/Frameworks). Learn more about MVC and its relatives: * [MVC](https://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller) * [HMVC](https://en.wikipedia.org/wiki/Hierarchical_model%E2%80%93view%E2%80%93controller) * [MVVM](https://en.wikipedia.org/wiki/Model_View_ViewModel)