Patterns and frameworks test whether a candidate understands reusable design beyond syntax. PSC answers should explain when to use composition, how frameworks invert control and why patterns solve recurring design problems.
Engineering Definitions
Design pattern
Standard definition: A reusable solution template for a recurring software design problem in a context.
Exam meaning: बारम्बार आउने design problem को reusable solution idea।
Framework
Standard definition: A reusable software structure that provides default behavior and extension points for applications.
Exam meaning: Application बनाउन skeleton/control flow दिने reusable platform।
Composition
Standard definition: Building complex objects by combining simpler objects as parts.
Exam meaning: Object ले अर्को object राखेर behavior बनाउने has-a design।
Inversion of control
Standard definition: A design principle where framework/container controls program flow and calls user code.
Exam meaning: Main control framework/container ले राखेर developer code लाई callback/extension point मा call गर्ने principle।
Concept Teaching
Patterns are vocabulary for design decisions. Frameworks are executable architecture. Composition is often safer than inheritance because behavior can be assembled and changed without forcing a rigid hierarchy.
Pattern Categories
Classic patterns are grouped by intent.
| Category | Purpose | Examples |
|---|---|---|
| Creational | Object creation flexibility | Factory, Builder, Singleton |
| Structural | Object/class composition | Adapter, Facade, Decorator |
| Behavioral | Object interaction/algorithm | Strategy, Observer, Command |
Common Patterns
Know intent more than memorized class diagrams.
- Factory hides object creation decision.
- Strategy swaps algorithms behind common interface.
- Observer notifies dependents when subject state changes.
- Adapter converts one interface into another expected interface.
- Facade provides simple interface to complex subsystem.
- Singleton restricts instance count but can harm testing if overused.
Framework vs Library
The control direction is the key distinction.
| Item | Who controls flow? | Example idea |
|---|---|---|
| Library | Your code calls library | Math/date utility |
| Framework | Framework calls your code | Web framework route/controller lifecycle |
| Plugin | Host app loads extension | CMS/plugin architecture |
Composition Over Inheritance
Composition supports flexible runtime assembly.
- Inheritance creates compile-time hierarchy.
- Composition uses object references/delegation.
- Composition avoids fragile base-class problem.
- Strategy pattern is composition-based behavior change.
- Use inheritance only when substitutability is valid.
Engineering Mechanism
- Identify recurring design problem.
- Select pattern by intent and tradeoff.
- Define interface/abstraction.
- Compose objects or plug into framework extension point.
- Keep dependencies pointed toward stable abstractions.
- Test components through interfaces/mocks.
Diagrams / Models To Draw
- Draw Strategy pattern context-interface-concrete strategies.
- Draw Observer subject-observers.
- Draw framework inversion-of-control flow.
- Draw composition object graph.
Formulas, Algorithms and Code Patterns
- Low coupling + high cohesion improves maintainability.
- Composition = object A has object B and delegates behavior.
- Framework principle: do not call us, we call you.
- Dependency inversion: high-level modules should depend on abstractions.
| Concept | Role | Exam trap |
|---|---|---|
| Pattern | Reusable design idea | Not copy-paste code |
| Framework | Reusable architecture/control flow | Not same as library |
| Composition | Has-a reuse | Different from inheritance |
| Strategy | Interchangeable algorithm | Runtime flexibility |
| Observer | Event notification | Can create update complexity |
| Singleton | Single instance | Can harm testability |
Exam Point
- Define pattern with context/problem/solution.
- For framework, mention inversion of control.
- Composition over inheritance is a frequent design principle.
- Give one example pattern and tradeoff.
- Do not present patterns as mandatory rules.
Worked Example
For payment processing, define PaymentStrategy interface with pay(). CardPayment and WalletPayment implement it. Checkout composes a PaymentStrategy and calls pay without knowing the concrete payment type. This is Strategy pattern and composition.
Subjective Answer Pattern
- Define pattern/framework/composition.
- Compare framework and library.
- Explain composition over inheritance.
- Describe two or three patterns with use case.
- Conclude with design benefits and misuse risks.
Common Engineering Mistakes
- Calling every class structure a pattern.
- Confusing framework with library.
- Using Singleton for global state everywhere.
- Using inheritance where composition is better.
- Ignoring tradeoffs of patterns.
MCQ Revision
- Which pattern swaps algorithms?
- Which pattern notifies subscribers?
- What is inversion of control?
- Framework or library: who calls whom?
- What relationship is composition?
- Which pattern adapts interface?
Final Summary
- Patterns are reusable design solutions.
- Frameworks provide control flow and extension points.
- Composition builds behavior from collaborating objects.
- Design should reduce coupling and improve testability.
- Patterns must be used with context and tradeoff awareness.