mirror of
https://github.com/codeguy/php-the-right-way.git
synced 2025-08-15 02:04:26 +02:00
Fix line wrapping consistency - round 1
Comply with the 'wrap at 120 chars' style rule stated in the contributing.md document
This commit is contained in:
@@ -5,9 +5,9 @@ title: Design Patterns
|
||||
|
||||
# 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.
|
||||
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.
|
||||
|
||||
* [Architectural pattern on Wikipedia](https://en.wikipedia.org/wiki/Architectural_pattern)
|
||||
* [Software design pattern on Wikipedia](https://en.wikipedia.org/wiki/Software_design_pattern)
|
||||
@@ -15,8 +15,8 @@ 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:
|
||||
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 %}
|
||||
<?php
|
||||
@@ -53,9 +53,9 @@ print_r($veyron->getMakeAndModel()); // outputs "Bugatti Veyron"
|
||||
|
||||
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.
|
||||
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
|
||||
@@ -65,8 +65,8 @@ yourself a lot of trouble down the road by using factories.
|
||||
|
||||
## 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.
|
||||
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 %}
|
||||
<?php
|
||||
@@ -144,20 +144,21 @@ request lifecycle in a web application. This typically occurs when we have globa
|
||||
class) or a shared resource (such as an event queue).
|
||||
|
||||
You should be wary when using the singleton pattern, as by its very nature it introduces global state into your
|
||||
application, reducing testability. In most cases, dependency injection can (and should) be used in place of a
|
||||
singleton class. Using dependency injection means that we do not introduce unnecessary coupling into the design of our
|
||||
application, reducing testability. In most cases, dependency injection can (and should) be used in place of a singleton
|
||||
class. Using dependency injection means that we do not introduce unnecessary coupling into the design of our
|
||||
application, as the object using the shared or global resource requires no knowledge of a concretely defined class.
|
||||
|
||||
* [Singleton pattern on Wikipedia](https://en.wikipedia.org/wiki/Singleton_pattern)
|
||||
|
||||
## Strategy
|
||||
|
||||
With the strategy pattern you encapsulate specific families of algorithms allowing the client class responsible for
|
||||
instantiating a particular algorithm to have no knowledge of the actual implementation.
|
||||
There are several variations on the strategy pattern, the simplest of which is outlined below:
|
||||
With the strategy pattern you encapsulate specific families of algorithms allowing the client class responsible for
|
||||
instantiating a particular algorithm to have no knowledge of the actual implementation. There are several variations on
|
||||
the strategy pattern, the simplest of which is outlined below:
|
||||
|
||||
This first code snippet outlines a family of algorithms; you may want a serialized array, some JSON or maybe just an
|
||||
array of data:
|
||||
|
||||
This first code snippet outlines a family of algorithms; you may want a serialized array, some JSON or maybe
|
||||
just an array of data:
|
||||
{% highlight php %}
|
||||
<?php
|
||||
|
||||
@@ -191,7 +192,7 @@ class ArrayOutput implements OutputInterface
|
||||
}
|
||||
{% endhighlight %}
|
||||
|
||||
By encapsulating the above algorithms you are making it nice and clear in your code that other developers can easily
|
||||
By encapsulating the above algorithms you are making it nice and clear in your code that other developers can easily
|
||||
add new output types without affecting the client code.
|
||||
|
||||
You will see how each concrete 'output' class implements an OutputInterface - this serves two purposes, primarily it
|
||||
@@ -201,6 +202,7 @@ this case 'OutputInterface'.
|
||||
|
||||
The next snippet of code outlines how a calling client class might use one of these algorithms and even better set the
|
||||
behaviour required at runtime:
|
||||
|
||||
{% highlight php %}
|
||||
<?php
|
||||
class SomeClient
|
||||
@@ -222,6 +224,7 @@ class SomeClient
|
||||
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 %}
|
||||
<?php
|
||||
$client = new SomeClient();
|
||||
@@ -249,7 +252,11 @@ and gives you a central place to hook in code that should be run for every reque
|
||||
|
||||
## 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.
|
||||
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).
|
||||
|
||||
|
Reference in New Issue
Block a user