Over the past several months I have learned a lot in software design and practices across several languages and frameworks. To me, the most attractive and useful designs patterns are those that follow an object-oriented approach. Particularly I am drawn to an approach that promotes reusability, modulariy, understanding (reusing understood code), and more.
The general approach that I find myself using often is one which encapsulates program functionality into separate components with minimal coupling. When I visualize this design I often imagine a hierarchical structure of objects and sub-objects, where each is only aware of its own interface and those of its members. As I understand, this last part is essential to achieving the sought after characteristics I mention above.
Keeping this in mind, for the various projects I have been working on over the past couple of months I have found myself hitting design roadblocks. How exactly do I define an object such that it is able to fulfill its intended functionality within my application while also having a simple and intuitive interface? More specifically (and perhaps in simpler words), how do I implement needed functionality in an object, which also isn't too specific for common applications?
I'll describe a case in which I am facing this issue:
Working with the Java Server Faces (JSF) framework to create a web application, and so my design is centered around page templating. This entails writing XHTML templates that can be reused whenever needed.
I want to create a header template that contains all the header markup I will use across my entire website. Here's an example:
<!-- header.xhtml --> <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html /> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"> <body> <div class="title">My Website</div> </body> </html>
Now, if I want add some flexibility to serve different header titles, I can add an insert tag like so:
<body> <div class="title"> <ui:insert name="insertTitle">My Website</ui:insert> </div> </body>
The details are not important for this discussion, but the insert tag lets pages using this template add a custom title in place of the default
This added functionality to the template seems appropriate and could definitely receive common use.
What if, however, I have a single page on my website that needs a button located in the header? Do I add another insert tag for this very purpose, even though it has single usage? In all other uses of the template this functionality is ignored and so could be considered code waste.
This issue of mine doesn't just present itself when working with html templating but for any approach that involves reusable sub-component design. Any tips or design insight for choosing functionality would be greatly appreciated.