Skip to main content

Framework and design pattern with example

Short answer

A framework is a reusable semi-complete application skeleton providing architecture and common behavior, customized via inversion of control. Design patterns are named proven solutions to recurring design problems, offering reuse, common vocabulary, better quality, and faster design. The Singleton pattern ensures exactly one instance with global access via private constructor, static instance reference, and static getInstance() method, useful for loggers or database managers.

What is framework? How design pattern is useful? Explain any one design pattern in detail with suitable example.

Framework

A framework is a reusable, semi-complete skeleton of an application: a set of cooperating classes and interfaces that provides the architecture and common behaviour for a family of applications. Developers customise it by writing subclasses or plugging in their own code, and the framework calls that code (inversion of control: "Hollywood principle - don't call us, we'll call you"). Examples: Spring, Hibernate, Struts, .NET, Java Swing, Angular.

How design patterns are useful

A design pattern is a named, tested solution to a commonly occurring design problem in a given context. Usefulness:

  • Reuse of proven design experience instead of reinventing solutions.
  • A common vocabulary ("use a Singleton here") for the team.
  • Better quality: flexible, loosely coupled, maintainable designs.
  • Faster design and better documentation.

Singleton pattern in detail

Problem: exactly one instance of a class is needed, with a global access point (e.g. a database connection manager, logger, or configuration). Solution: make the constructor private, keep a static reference to the single instance, and provide a static method getInstance().

 +--------------------------+
 |        Logger            |
 |--------------------------|
 | - instance : Logger      |  (static)
 | - Logger()               |
 |--------------------------|
 | + getInstance() : Logger |  (static)
 | + log(msg)               |
 +--------------------------+
public class Logger {
    private static Logger instance;       // the one object
    private Logger() { }                  // no outside creation
    public static synchronized Logger getInstance() {
        if (instance == null) instance = new Logger();
        return instance;
    }
    public void log(String msg) { System.out.println(msg); }
}
// use: Logger.getInstance().log("started");

Consequences: controlled access to the sole instance, saves resources, global but managed access; drawbacks: hidden dependency, harder unit testing, needs care in multi-threading.

✓ Answer checked by Claude Sonnet 5.5

All Object Oriented Analysis and Design chapter 3 questions →