P-Patterns!

From Andrey

Revision as of 08:48, 9 December 2013; view current revision
←Older revision | Newer revision→

Contents

P-Patterns

Some patterns I came up with.

The Smart Button

Suppose I bought a futuristic DVD player - a slick black box with a tiny hole for the microphone. It did an amazing job of hearing voice commands and recognizing them. For example, to open the drive bay I would say 'Open drive'. To make a pause, I would say 'Do pause' and so on. I liked it in the beginning, but unfortunately, I had to always consult the product manual because I kept forgetting the exact commands. For example, I might say 'Pause' instead of 'Do pause', and it wouldn't recognize. Finally, I started wanting a more traditional player - the one that would have buttons, so to operate it I wouldn't have to recall any commands. In the end, I took the DVD player back to store and exchanged it for a conventional one. With a several buttons, I had complete control over the thing and trashed that page with fine-printed API wrapped in a translucent sleeve.
Expose a generic object in the interface in order to simplify class use.

This is my case for exposing an object in the interface. I know that there is opposition for doing so, and even a law (Law of Demeter). However, I choose to say that exposure of an object that belongs to a generic, well-known class in the interface provides for a simpler, more intuitive design compared to hiding everything under a monolith interface with numerous methods. If the list of class's methods reads like a laundry list, return it back to the store!

I'm not entirely opposed to the Law of Demeter, however I often felt that it stems from the assumption that exposing an object results in loss of control on actions that are performed on the exposed object. While it is true in some OO languages, there are designs and approaches that provide for such control (Example). In the presence of such mechanisms, the Law of Demeter may be less critical.

On the other hand, I'm also not entirely for breaking encapsulation and exposing any inner object in class interface. In our example with our DVD player it makes sense to export some objects (like buttons, light indicators) while keeping everything else under the hood (transformers, electric motors, microprocessors). Therefore, more precisely, we want to expose some very generic control-like objects (buttons, status strings), while keeping implementation-specific parts still hidden from the user.

Postponed Validation

Say you have a setProperty() method which changes the value of an object field. You typically validate method arguments right in the body of the method; and sometimes you also need a more extensive validation, e.g. if your object is one of a compound type. Such validations may sometimes be taxing on performance in processing-intensive scenarios. Instead, you perform validations in the time of a commit. If a validation rule is broken, perform a rollback. Postponing validations until commit lets you ensure data integrity while maintaining high performance.

Postpone object validation until a checkpoint/commit so that validations do not hinder performance.

One way to implement the postponed validation is Detached Validation. Create a flag in your class to switch validations on and off, and perform a full object validation each time the flag is turned on. While in the 'detached' state (flag is off), skip any validations thus improving performance. Enforce each object to be back to its non-detached (flag on) state before a commit/checkpoint.

Static resolution

Hide a class behind a static interface to improve code brevity and clarity. Instead of creating a Singleton and always calling its getInstance() method, create a class that implements same interface through static methods, and provide one instance of your class as a working horse for that class. The client code which looked like

Logging logging = Logging.getInstance();
logging.error("my message");

becomes this:

Log.error("my message");

where Log is a static-interface class serving as a facade for the Logging class.

Pure Business Objects

Divide model classes into pure business object classes and application-specific implementations inheriting from them. In the model, only deal with pure business object classes, while instantiating only the application-specific implementations for them. Utilize the Bridge design pattern to decouple inheritance graphs of both hierarchies and to separate the model from application-specific behaviors.