Even though at first glance it looks like an obscure and esoteric principle not worth analyzing in detail, the Value Object design pattern is a simple paradigm that permits you to generate, that is, objects whose equality is measured by the values assigned to their fields and not on a unique identity. Unlike other programming languages, PHP doesn’t package by default a lot of native value objects, with the exception of the ones that can be spawned from its built-in ArrayObject and DateTime classes. The good news, though, is that creating them is a extremely easy process that you can tackle even if you have only an average background in the object-oriented approach. To demonstrate how simple it is to build immutable value objects using a custom class, in previous parts of this series I went through the development of a basic one, whose primary responsibility was modeling immutable URL objects. By far, the most relevant facet of this construction process was the implementation of a method within the class, which was tasked with appending additional arguments to its existing query string. Considering the immutable nature of the class, the method acted like a simple factory which returned new URL objects to client code, in this way preserving the values assigned initially to the class’ fields. As you may have already realized, the creation of immutable value objects isn’t limited to modeling well-formed URLs, as the process can easily be extended to other types of objects as well. So, since my goal here is to demonstrate the functionality of the Value Object pattern from a practical point of view, in this penultimate tutorial of the series I’m going to define another basic class, which will be charged with building a few immutable HTML widgets, and more specifically div elements. Are you ready to learn the details of this process? Then keep reading. Getting started building an immutable HTML widget class As I said in the introduction, my purpose in this tutorial is to build a class capable of building immutable div objects. In keeping with this, I'll start by defining a contract for creating generic HTML widgets. Obviously, this can be easily achieved with an interface, so below is the definition of the one that I plan to use. Look at it, please: (HtmlWidgetInterface.php) <?php interface HtmlWidgetInterface Well, to be frank there’s no much that can be said about the above “HtmlWidgetInterface.” It declares a method called “render(),” which naturally must be provided with a concrete definition by all the classes implementing the interface in question. With this simple contract already set, the next step is to create a class responsible for spawning the immutable div objects just mentioned. This class is not surprisingly called “Div,” and its source code is shown below: <?php final class Div implements HtmlWidgetInterface
(HtmlWidgetException.php) <?php class HtmlWidgetException extends Exception{} That was simple to code and read, wasn’t it? Similar to the examples developed in prior parts of this series, the brand new “Div” class doesn’t expose any setter methods to client code, in this way making it possible to create immutable div objects. In addition, the class concretely implements the “render()” method declared by the pertinent “HtmlWidgetInterface” interface, thus fulfilling the contract as expected. So far, so good. At this point, if you give the “Div” class a try, it should work decently well, as each call to its “render()” method would effectively render a basic HTML div per object being created. However, in its current incarnation the behavior of the class is actually very limited; it’d be desirable to provide it with an extended functionality. In line with this idea, in the next section I’m going to add to the class a new method, which will be capable of rendering nested divs. If you’re anything like me, at this moment you’re wondering how this will be done without affecting the class’ immutability, right? Well, to find out the answer to your question, you’ll have to click on the link below and read the following segment.
blog comments powered by Disqus |
|
|
|
|
|
|
|