Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

NetTuts.com:
SOLID: Part 2 – The Open/Closed Principle
Jan 21, 2014 @ 18:45:29

On NetTuts.com today they continue their look at the SOLID development principles with the next letter in the sequence - O for Open/Closed Principle. The first part of the series, covering "S" (the Single Responsibility Principle) can be found here.

The Open/Closed Principle, OCP in short, is credited to Bertrand Mayer, a French programmer, who first published it in his book Object-Oriented Software Construction in 1988. The principle rose in popularity in the early 2000s when it became one of the SOLID principles defined by Robert C. Martin in his book Agile Software Development, Principles, Patterns, and Practices and later republished in the C# version of the book Agile Principles, Patterns, and Practices in C#. What we are basically talking about here is to design our modules, classes and functions in a way that when a new functionality is needed, we should not modify our existing code but rather write new code that will be used by existing code.

They start with a look at OCP in the overall SOLID context and an example of an obvious violation of the principle. Some example code is provided showing a "Progress" class that's bound to at "File" implementation, not abstracted. They offer three different options for solving the issue:

  • Take Advantage of the Dynamic Nature of PHP
  • Use the Strategy Design Pattern
  • Use the Template Method Design Pattern

Each of the above comes with example code and some with illustrations of the structure they create.

tagged: solid design principles open closed tutorial introduction

Link: http://net.tutsplus.com/tutorials/php/solid-part-2-the-openclosed-principle

Freek Lijten's Blog:
SOLID - The O is for Open Closed Principle
May 07, 2012 @ 15: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.

tagged: solid development principles open closed principle

Link:

Josh Adell's Blog:
Command Invoker Pattern with the Open/Closed Principle
Jan 16, 2012 @ 16: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.

tagged: command designpattern invoke open closed principle solid tutorial

Link:

DZone.com:
Open/Closed Principle on real world code
Jan 13, 2012 @ 15:05:53

In a new post to DZone.com Giorgio Sironi talks about the "open/closed principle" in software development and shows an example based on the design of the PHPUnit_Selenium project.

This article shows an example of how the application of the Open/Closed Principle improved the design of a real project, the open source library PHPUnit_Selenium. These design concepts apply to every object-oriented language, including Java, Ruby or even C++. The Open Closed Principle, part of SOLID set, states that software should be open for extension and at the same time closed for modification.

He starts with a little background on the project, pointing out that there's a Session object it uses for all of its testing with a magic "__call" method that handles any kind of method call to the object. This method has issues (dependencies, strict requirements for use) but can be refactored according to the Open/Closed idea to set up an array of anonymous functions that can be called as a "command". Examples of these types of classes are also included (one for the "click" action on a button and another for getting the current location).

tagged: open closed principle solid design command phpunit selenium

Link:


Trending Topics: