Now that you know how to make a class implement an interface, I’m going to define the second class, “MySQLCache”, which caches MySQL results based on a time expiry trigger. Its source code is as following: // class MySQLCache Despite the apparently-complicated look of the class, its rationale is fairly simple. Essentially, the tasks performed by the class can be subsumed in the following sequence: first, a MySQL result set is obtained through the “query()” method, once a successful connection has been established. Then, the result set is converted to an array and serialized by the “getSerializedData()” method. Finally, the serialized data is written to a specified cache file, which will be evaluated by the “isCacheValid()” method, in order to force a new cache generation when a given time expiry has been reached. As long as the cache is considered valid, data will be read from the given cache file. Otherwise, a new result set will be retrieved by running a SELECT statement and returned to the application as an array for further processing. Since the class’ logic is fairly understandable, I won’t spend a long time explaining its functionality. However I’d like to strongly point out the implementation of the “DeSerializer” interface within the class. Notice that again the “getSerializedData()” and “getUnserializedData()” are specifically defined within the class, to perform the serialize-unserialize sequence on the data. Although these methods basically perform the same tasks exposed within the first class “PostSaver”, they’re implemented in a different way, as you can appreciate through the lines below: // unserialize data This condition is telling us that, despite the fact that both classes implement the same interface, the corresponding methods are defined differently. Therefore, the generic functionality defined through the interface is specifically implemented in different ways at class level. With reference to the example, there are two types of objects with nothing in common that use the same methods declared in the interface. The example shows clearly the differences in using subclasses derived from the same abstract parent class, due to the mere fact that each object belongs to a different family type. Otherwise, when working with a base parent class, child objects would be of the same type. In either case, as an implementer, it’s important that you know how to spot the difference. At this point, I have demonstrated the functionality of user-defined interfaces. However, in order to reaffirm the concepts that you just learned, I’ll write a couple of functional examples that demonstrate the usage of the previous classes. Thus, let’s get rid of the boring details and jump straight into the examples.
blog comments powered by Disqus |