From 7ffdefbd8f378fb1b1adcf3cee430197d9e197d9 Mon Sep 17 00:00:00 2001 From: Steven Benner Date: Fri, 13 Jul 2012 17:33:59 -0700 Subject: [PATCH 1/5] Added "Application Design Patterns" section. --- .../13-01-01-Application-Design-Patterns.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 _posts/13-01-01-Application-Design-Patterns.md diff --git a/_posts/13-01-01-Application-Design-Patterns.md b/_posts/13-01-01-Application-Design-Patterns.md new file mode 100644 index 0000000..8545fba --- /dev/null +++ b/_posts/13-01-01-Application-Design-Patterns.md @@ -0,0 +1,36 @@ +# Application Design Patterns + +There are numerous ways to structure your web applications, and you can put as much or as little thought as you like +into architecting your code. But it is usually a good idea to follow to one of the common patterns because it will make +your code easier to manage and easier for others to understand. + +The most common patterns used in PHP development today are the front controller pattern and the MVC pattern (and its +relatives). + +* [Architectural patterns on Wikipedia](https://en.wikipedia.org/wiki/Architectural_pattern) +* [Software design pattern on Wikipedia](https://en.wikipedia.org/wiki/Software_design_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 let you break up code into logical objects that +serve very specific purposes. Models serve as a data access layer where data it 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 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) From 86ce8a330e1ac64515336b3d2ac193c6b9690dfc Mon Sep 17 00:00:00 2001 From: Steven Benner Date: Sun, 15 Jul 2012 04:39:23 -0700 Subject: [PATCH 2/5] Rename Application Design Patterns section. --- ...Application-Design-Patterns.md => 05-03-01-Design-Patterns.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename _posts/{13-01-01-Application-Design-Patterns.md => 05-03-01-Design-Patterns.md} (100%) diff --git a/_posts/13-01-01-Application-Design-Patterns.md b/_posts/05-03-01-Design-Patterns.md similarity index 100% rename from _posts/13-01-01-Application-Design-Patterns.md rename to _posts/05-03-01-Design-Patterns.md From f19396576f75e106425756060f7c1a2e802d629f Mon Sep 17 00:00:00 2001 From: Steven Benner Date: Sun, 15 Jul 2012 04:43:12 -0700 Subject: [PATCH 3/5] Added isChild block. --- _posts/05-03-01-Design-Patterns.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/_posts/05-03-01-Design-Patterns.md b/_posts/05-03-01-Design-Patterns.md index 8545fba..562ddaf 100644 --- a/_posts/05-03-01-Design-Patterns.md +++ b/_posts/05-03-01-Design-Patterns.md @@ -1,3 +1,7 @@ +--- +isChild: true +--- + # Application Design Patterns There are numerous ways to structure your web applications, and you can put as much or as little thought as you like From a4e0494ddf10babc92593c2d0133464da6d98e87 Mon Sep 17 00:00:00 2001 From: Steven Benner Date: Sun, 15 Jul 2012 05:02:06 -0700 Subject: [PATCH 4/5] Rewrite of Design Patterns sub-section. --- _posts/05-03-01-Design-Patterns.md | 39 ++++++------------------------ 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/_posts/05-03-01-Design-Patterns.md b/_posts/05-03-01-Design-Patterns.md index 562ddaf..6e87109 100644 --- a/_posts/05-03-01-Design-Patterns.md +++ b/_posts/05-03-01-Design-Patterns.md @@ -2,39 +2,16 @@ isChild: true --- -# Application Design Patterns +## Design Patterns -There are numerous ways to structure your web applications, and you can put as much or as little thought as you like -into architecting your code. But it is usually a good idea to follow to one of the common patterns because it will make -your code easier to manage and easier for others to understand. +When you are building your application it is helpful to use common patterns in your code and common patterns for the +overall structure of your project. Using common patterns is helpful because it makes it much easier to manage your code +and lets other developers quickly understand how everything fits together. -The most common patterns used in PHP development today are the front controller pattern and the MVC pattern (and its -relatives). +If you use a framework then most of the higher level code and project structure will be based on that framework, so a +lot of the pattern decisions are made for you. But it is still up to you to pick out the best patterns to follow in the +code you build on top of the framework. If, on the other hand, you are not using a framework to build your application +then you have to find the patterns that best suit the type and size of application that you're building. * [Architectural patterns on Wikipedia](https://en.wikipedia.org/wiki/Architectural_pattern) * [Software design pattern on Wikipedia](https://en.wikipedia.org/wiki/Software_design_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 let you break up code into logical objects that -serve very specific purposes. Models serve as a data access layer where data it 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 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) From 2cb5960c434a8fe78fcdae94d614e08658e674eb Mon Sep 17 00:00:00 2001 From: Steven Benner Date: Fri, 20 Jul 2012 11:38:00 -0700 Subject: [PATCH 5/5] Added Design Patterns page. --- _posts/05-03-01-Design-Patterns.md | 3 +- pages/Design-Patterns.md | 88 ++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 pages/Design-Patterns.md diff --git a/_posts/05-03-01-Design-Patterns.md b/_posts/05-03-01-Design-Patterns.md index 6e87109..211049d 100644 --- a/_posts/05-03-01-Design-Patterns.md +++ b/_posts/05-03-01-Design-Patterns.md @@ -13,5 +13,4 @@ lot of the pattern decisions are made for you. But it is still up to you to pick code you build on top of the framework. If, on the other hand, you are not using a framework to build your application then you have to find the patterns that best suit the type and size of application that you're building. -* [Architectural patterns on Wikipedia](https://en.wikipedia.org/wiki/Architectural_pattern) -* [Software design pattern on Wikipedia](https://en.wikipedia.org/wiki/Software_design_pattern) +* Continue reading on [Design Patterns](/pages/Design-Patterns.html) diff --git a/pages/Design-Patterns.md b/pages/Design-Patterns.md new file mode 100644 index 0000000..81df23e --- /dev/null +++ b/pages/Design-Patterns.md @@ -0,0 +1,88 @@ +--- +layout: page +title: Design Patterns +--- + +# Design Patterns + +There are numerous ways to structure the code and project for you 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 to common patterns because it will make +your code easier to manage and easier for others to understand. + +* [Architectural pattern on Wikipedia](https://en.wikipedia.org/wiki/Architectural_pattern) +* [Software design pattern on Wikipedia](https://en.wikipedia.org/wiki/Software_design_pattern) + +## Factory + +One of the most commonly used design patterns is the factory pattern. This is a pattern is simply a class that creates +the object you want to use. Consider the following example of the factory pattern: + +{% highlight php %} +vehicle_make = $make; + $this->vehicle_model = $model; + } + + public function get_make_and_model() + { + return $this->vehicle_make . ' ' . $this->vehicle_model; + } +} + +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->get_make_and_model()); // 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) + +## 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 let you break up code into logical objects that +serve very specific purposes. Models serve as a data access layer where data it 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)