Understanding Framework Design: Template Method, Open‑Closed Principle, and Java Annotations
The article explains how a framework acts as a semi‑finished product that relies on abstract classes, the Open‑Closed principle, reflection, XML or annotation configuration, and conventions to let developers extend functionality while keeping the core immutable.
It starts by contrasting the roles of genius programmers who create tools with ordinary programmers who only use them, and encourages readers to try building a framework to experience the creator’s challenges.
The author defines a framework as a "half‑product" that requires developers to fill in missing code, unlike a ready‑to‑use tool. This necessitates clear boundaries between what the framework handles and what the user must implement, following the Open‑Closed principle.
Using the Template Method pattern, the article shows how Java abstract classes (e.g., AbstractAction ) can encapsulate framework logic while forcing subclasses to implement abstract methods such as execute . The abstract class can also contain concrete code that the framework relies on.
An MVC‑style example illustrates how the framework extracts HTTP request parameters, creates a map, and passes it to concrete actions, while keeping the core logic closed for modification.
The discussion then covers the drawbacks of inheritance‑based frameworks: strong coupling to the base class, difficulty in unit testing, and reliance on servlet containers. To mitigate this, the author introduces declarative approaches using XML configuration or Java annotations.
With XML, the framework parses configuration to discover action classes and uses Java reflection to instantiate them and invoke setter methods (e.g., setUsername , setPassword ) based on request parameters, followed by calling execute . The same can be achieved with annotations like @Action and @RequestMapping , which embed metadata directly in source code.
Finally, the article notes that these concepts underpin popular frameworks such as Struts1/Struts2 and Spring MVC, and encourages readers to experiment with building their own lightweight framework.
DevOps
Share premium content and events on trends, applications, and practices in development efficiency, AI and related technologies. The IDCF International DevOps Coach Federation trains end‑to‑end development‑efficiency talent, linking high‑performance organizations and individuals to achieve excellence.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.