 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
Freek Lijten: SOLID - The D is for Dependency Inversion Principle
by Chris Cornutt January 02, 2013 @ 10:25:46
Freek Lijten has posted the last article in his series looking at the SOLID development principles, this time looking at "D", the Dependency Inversion Principle.
The DIP deals with avoiding mixing different levels of abstraction in your code. In this article we will explore the last of these principles in depth. [...] I want to show you how depending on abstractions makes our lives easier in a larger example. Since we're going to be loading, updating and storing bikes a lot in our application I would like an Api for that. It would not be such a good idea to write the same queries or perform the same validation over and over.
He includes two examples - one a bit simplistic involving bike storage and another more complex that uses interfaces to define structure and object injection rather than passing data around. He also tries to help clear up some of the confusion that might happen around dependency inversion and dependency injection.
voice your opinion now!
solid development principle dependency inversion introduction
PHPMaster.com: Inversion of Control - The Hollywood Principle
by Chris Cornutt December 10, 2012 @ 09:43:50
In this new tutorial on PHPMaster.com, Alejandro Gervasio looks at the Inversion of Control methodology and how it's more than just an abstract reference to dependency injection.
Traditionally, application components have been designed to operate on and control the execution environment, an approach that delivers well to some extent. [...] Instead of making the module completely responsible for logging data to multiple endpoints, we can transfer the responsibility straight to the external environment. [...] Not surprisingly, the process of inverting these responsibilities between components and the environment is formally known as Inversion of Control (or in a more relaxed jargon, The Hollywood Principle), and its implementation can be a real boost when it comes to developing extensible, highly-decoupled program modules.
He uses a set of domain objects (Posts and Comments in a typical blog structure) and the Observer pattern to show how mixed up things might get if the application isn't carefully coded. He takes this and updates it to include a "comment notification service" that implements the SplObserver and is attached to the post to be executed on an event (in this case, the setting of a new comment).
voice your opinion now!
inversionofcontrol hollywood principle introduction listener observer tutorial
PHPMaster.com: The Single Responsibility Principle
by Chris Cornutt November 22, 2012 @ 11:58:06
On PHPMaster.com today Alejandro Gervasio has a new tutorial posted about the Single Responsibility Principle - a guideline that states that each class should only have one "area of concern" and not try to do to much.
One of the most notorious consequences of this rational associative process is that, at some point, we effectively end up creating classes that do too much. The so-called "God class" is quite possibly the most extreme and coarse example of a structure that packages literally piles of unrelated operations behind the fence of the same API, but there are other subtle, more furtive situations where assigning of multiple roles to the same class are harder to track down. [...] What the principle attempts to promote is that classes must always be designed to expose only one area of concern and the set of operations they define and implement must be aimed at fulfilling that concern in particular and nothing else.
He starts off with a typical violation of the principle, showing a class that not only handles user data but also includes the functions to work with the database directly as well (insert/update/delete). He refactors this into a few much more manageable classes - a mapping class to handle the database interaction and a "User" class representative of a true user object.
voice your opinion now!
single responsibility principle srp class tutorial refactor
PHPMaster.com: The Open/Closed Principle
by Chris Cornutt November 08, 2012 @ 10:09:32
On PHPMaster.com there's a new post continuing their look at the SOLID development methodologies with the "O" in the acronym - the Open/Closed Principle:
I have to admit the first time I peeked at the academic definition of the Open/Closed Principle, its predicate was surprisingly clear to me. Leaving all of the technical jargon out of the picture, of course, its dictate was pretty much the mantra that we've heard so many times before: "Don't hack the core". Well, admittedly there's a pinch of ambiguity since there are at least two common approaches to keeping the "core" neatly preserved while extending its functionality. The first one (and why I used deliberately the term "extending") would be appealing to Inheritance. [...] The second approach is Composition.
He illustrates the effective application of the principle with the creation of a HTML rendering class. The first version is non-polymorphic and just renders the example DIV and P elements that are passed into it. He changes this up by shifting these element classes into something extending an AbstractHtmlElement class (sharing an interface between them) and updating the renderer to handle these correctly.
voice your opinion now!
solid design principle openclosed tutorial
PHPMaster.com: Constructors and the Myth of Breaking the Liskov Substitution Principle
by Chris Cornutt October 08, 2012 @ 11:53:13
On PHPMaster.com there's a new post in a series looking at the SOLID design principles in PHP development. In this new tutorial they try to dispel the myth that constructors break the Liskov Substitution Principle ("L" in "SOLID").
Rants aside, the morale of the story can be boiled down to the following: "Object construction is not part of the contract honored by its implementers". It's easier to grasp concepts by example rather than reading dull theory, so in this article I'll be demonstrating from a practical standpoint how the implementation of different constructors down the same hierarchy isn't a violation of the Liskov Substitution Principle, which is a reason why you shouldn't fall into the temptation of tainting your interfaces with constructors.
He illustrates the point with a simple PDO class that implements a "DatabaseAdapterInterface" interface that defines its own constructor that follows the defaults of the PDO extension. He goes on and changes the constructor for the class a bit to take in an array of config options rather than the DSN/User/Password combo. Inside of this constructor, those values are then taken and pushed into PDO to create the connection. He also suggests one other solution - the injection of a connection object ("ConnectionDefinition") into the constructor instead of the configuration directly.
voice your opinion now!
solid development liskov substitution principle tutorial constructor
Freek Lijten: SOLID - The L is for Liskov Substitution Principle
by Chris Cornutt September 04, 2012 @ 08:37:46
In this new post to his site Freek Lijten picks back up his series on the SOLID design principles with a look at the "L" in the acronym - the Liskov Substitution Principle.
The Liskov Substitution Principle (LSP) was coined by Barbara Liskov as early as 1987. The principle is very tightly connected to the earlier discussed Open Closed Principle. A good way of adhering to the OCP is understanding and implementing code that uses the Liskov Substitution Principle. In this article we will discover why and how.
He gives a more understandable explanation of what the rule tries to suggest - a standardized interface that each of his "Bike" instances follows to ensure the contract of a consistent API. Then each of the child classes can reliably assume that there are methods they can use because they'll always inherit them.
voice your opinion now!
liskov substitution principle solid design series
PHPMaster.com: The Dependency Inversion Principle
by Chris Cornutt May 10, 2012 @ 08:52:19
Continuing on in their series looking at the SOLID development principles, Alejandro Gervasio picks back up and looks at the "D" in the set - the dependency inversion principle.
While some central concepts in the realm of object-oriented design are generally harder to digest at first, such as separation of concerns and implementation switching, more intuitive and untangled paradigms on the other hand are simpler, like programming to interfaces. Unfortunately, the DIP's formal definition is surrounded by a double-edged curse/blessing that often makes programmers gloss over it, as in many cases there's an implicit assumption that the principle is nothing but a fancy expression for the aforementioned "programming to interfaces" commandment.
He talks about how decoupling your code and working against interfaces is only part of the equation. The other half is the actual "inversion" part of the process - the ownership that the high-level modules must have over the abstractions. He illustrates with an example, a storage module that is highly dependent on a Serializer. Using the DIP principle, he turns this around and refactors his storage class to take in an Encoder object as a part of its construction. Complete source for both versions is included.
voice your opinion now!
dependency inversion principle solid dip tutorial
Freek Lijten's Blog: SOLID - The O is for Open Closed Principle
by Chris Cornutt May 07, 2012 @ 10:45:37
Freek Lijten has posted the second part of his series looking at the SOLID development methodology. In this latest post his looks at the second letter in the acronym - "O" for "Open Closed Principle."
Software that requires an enormous amount of changes to implement one new feature or fix a bug is unstable and should be considered as "bad". Software should be designed so, that in case of a new feature, no existing classes should have to change. In other words: it is closed for modification. Existing software may be extended to achieve new features however.
He starts off with a "What" section explaining a bit more about what this open/closed means for your code and gets into an example showing it in a more practical way. He shows how to take the principle and refactor an API connector class to pass in the object it needs (Bike) and use that to get information (rather than just passing in the data). He uses a Factory to get the object type he needs based on the Bike type.
voice your opinion now!
solid development principles open closed principle
PHPMaster.com: The Liskov Substitution Principle
by Chris Cornutt January 24, 2012 @ 13:40:35
On PHPMaster.com today there's a new post from Alejandro Gervasio about a part of the SOLID development methods - the Liskov Substitution Principle - the idea that objects should be replaceable with instances of their subtypes without a change to the architecture of the application.
Even when the formal definition of the LSP makes eyes roll back (including mine), at its core it boils down to avoiding brittlely-defined class hierarchies where the descendants expose a behavior radically different from the base abstractions consuming the same contract.
He includes an example with a "deleted scene" from the Matrix depicting an attempted override of the PDO functionality with a subclass that, unfortunately, does not match the original's structure/method definitions. The problem was in the difference between the method signature for the "query" method. It help resolve situations like this he recommends creating a "contract" in the form of an interface your code can implement, forcing it to conform to a certain structure. Using this, he provides a rewrite of the "PdoAdapter" class to match the original signature
voice your opinion now!
liskov substitution principle tutorial example solid development pdo interface
Josh Adell's Blog: Command Invoker Pattern with the Open/Closed Principle
by Chris Cornutt January 16, 2012 @ 10:04:42
In a response to a recent post on DZone.com about the "Open/Closed Principle" Josh Adell has posted an example of a " flexible and extendable command invocation solution" implementing this SOLID idea.
Let's overcome some of these issues [with only being able to extend the invoker class and that the invoker needs to know how to create commands], and also make the code even more extensible. I'll use a simplified command invoker to demonstrate.
His code is included - the creation of a "Command" interface and two comments that implement it: "HelloCommand" and "PwdCommand", each with "register" and "execute" methods. His "Invoker" class then only needs to be told how to map these commands and the "register" is called as they're needed. You can find the full example code for this invocation example in this gist.
voice your opinion now!
command designpattern invoke open closed principle solid tutorial
|
Community Events
Don't see your event here? Let us know!
|