As I expressed in the section that you just read, the next step involved in the creation of an adapter class consists of defining a concrete directory processor which will implement all the abstract methods that were defined in the respective base class. Sounds logical, right? Okay, having explained that, below I defined the structure of the brand-new “DirectoryProcessor” class that I mentioned before. Please take a look:
As you can see, the above defined “DirectoryProcessor” class simply implements in a concrete way all the methods that were originally declared abstract in the corresponding base class. In addition, the specific methods exposed by this new class perform some simple tasks, like fetching the content of a specified directory, which is directly inputted via the constructor, as well obtaining its name and path. So far, I have at disposal a fully-functional directory handling class, which can be basically used as indicated below:
In this case, provided that the sample “default_path” directory above specified contains the files “file1.txt” and “file2.txt” respectively, the output generated by the previous script corresponds to a logical expectation. However, there are two important things worth noticing about the previous example. Obviously, the first one rests on the fact that the same functionality could have been achieved using the “DirectoryIterator” class that comes with PHP 5, while the second one is even more relevant for this article in particular, since there’s still not a clue of how to create an adapter class. Well, let me answer these questions in the correct order: definitely I could have used the neat “DirectoryIterator” class bundled with the SPL package to work with a highly standard directory handling class, but you shouldn’t worry about this for the moment, since this procedure will be covered in the following article of this series. And finally, with reference to the second question, here is where things get really interesting! Suppose that for whatever reason, you need to expand the functionality of the previous “DirectoryProcessor” class, but without using the goodies of inheritance, in addition to giving the class a predefined structure. How can this be done? This issue can be easily tackled by creating an adapter class that performs the mentioned expansion! Want to learn how this adapter class will be defined? Go ahead and visit the following section. I’ll be there, waiting for you.
blog comments powered by Disqus |