5 Lecture 2 Patterns - UPDATED
5 Lecture 2 Patterns - UPDATED
in OOP
(GoF, Model-View-Controller)
Learning Objectives
By the end of this lecture, you will be able to:
❑Design Patterns, in banking system
❑Model-View-Controller
➢ Low Cohesion: Class handles unrelated functionalities, leading to maintenance challenges. (Bad design)
➢ High Cohesion: Class has focused single responsibilities, improving clarity and ease of maintenance. (Good Design)
•High Cohesion: Account class responsible for just account-related functionalities like deposits and withdrawals. (Good Design)
What is Coupling?
• Coupling is how much one class/module knows about another.
• Higher the coupling, the harder it is to change one class without affecting another.
Uncoupled: No dependencies Loosely coupled: Some dependencies Highly coupled: Many dependencies
Software Design, Tactics and Design Patterns in OOP
It is a conceptual, structural, logical organization of a computer-based system.
Software design:
▪ Planning, process of defining or guidelines to produce high quality products.
Tactics:
◦ Use single mechanism, or structure.
Design Patterns:
➢ Reusable solution: recurring solutions to common problems in a given context in OOP.
➢ Description.
➢ Template.
Software design
• Defining the architecture, components/interfaces/characteristics of a system/component.
1. High-Level Design (Architectural Design): Focuses on how the system, components, and modules
interact with each other. It defines system architecture, data flow, and protocols.
2. Low-Level Design (Detailed Design): Focuses on the internal structure of individual components. It
includes detailed algorithms, data structures, and coding standards.
Example: In a high-level design for an Banking application, you might define modules like user
management, account management, notifications , and payment gateway. Each module's interactions
and data flow are defined at this stage.
Software tactics
• Low-level strategies, ensure the system's architecture follow certain quality attributes.
• To achieve specific quality attributes (like performance, security, maintainability) in a software design.
• Gang of Four" (GoF) design patterns comes from the book titled "Design Patterns: Elements of Reusable
Object-Oriented Software" written by four authors in 1994, 23 DPs:
Erich Gamma
Richard Helm
Ralph Johnson
John Vlissides
• They are best practices that the developer can use to solve common problems while designing a software
application or system.
• They are more about templates to solve certain design problems in a particular context.
Software Patterns Design Patterns in OOP Software Tactics
Applies to both architecture and Applied during the detailed design Applied at a tactical level to
Application
design levels. phase. achieve quality goals.
High-level patterns for architecture Focuses on specific design Focuses on achieving desired
Focus
and system design. problems. quality attributes.
Why are these Important?
• Making them easier to maintain, understand, and scale (S/W Pattern).
• Help solve recurring problems and improve communication among developers by providing a shared
vocabulary (DP).
• Help to achieve desired qualities in a system (e.g., performance, security) and address specific concerns
efficiently(S/W Tactics).
Component Role
Model Handles data and business
rules (e.g., Account)
View Displays data to the user (e.g.,
Balance screen)
Screen/receipt clerk Controller Manages user input and
updates Model/View
Component Role
Model Handles data and business
rules (e.g., Account)
View Displays data to the user (e.g.,
Balance screen)
Screen/receipt clerk Controller Manages user input and
updates Model/View
Component Role
Model Handles data and business
rules (e.g., Account)
View Displays data to the user (e.g.,
Balance screen)
Screen/receipt clerk Controller Manages user input and
updates Model/View
Component Role
Model Handles data and business
rules (e.g., Account)
View Displays data to the user (e.g.,
Balance screen)
Screen/receipt clerk Controller Manages user input and
updates Model/View
Screen/receipt clerk
Data
Business
logic-1
Business
logic-2
Model – Account.java
View (Screen/receipt) for Banking System
• The View class handles all display/output logic
Display logic
View- AccountView.java
Controller (clerk) for Banking System
• The Controller class is middle-man of Model and View
Model logic
View logic
Object of both
Model and View
Controller- AccountController.java
MVC concept
Banking App:
•Different views for customer and admin.
Testability:
❑ Model tested easily.
❑ Controllers tested for user actions.
MVC concept
•It was introduced in Smalltalk in the 1970s
•But became popular in modern UI frameworks and web development (e.g., Java
Swing,Django).
•MVC is not a framework —it's a design pattern. Frameworks (pre-built collection of tools,
libraries) implement it.
Extensions:
▪ MVVM (Model-View-ViewModel): Used in Android.
Explanation:
A: Correct – The Controller handles user actions (like button clicks), and based on that, modifies the Model.
B: Incorrect – Formatting data is generally the View’s responsibility.
C: Incorrect – While the Controller can trigger updates to the View indirectly, it should not directly modify it.
D: Correct – The Controller acts as an intermediary, helping to decouple the View from the Model.
https://onurdesk.com/why-we-need-design-patterns-in-3-category/
1. Creational patterns deal with object creation logic, hiding complexities and making code more flexible.
2. Structural patterns help build relationships between different parts of the system.
Creational How objects are created Factory Producing object Creating a single config object
Creational How objects are created Factory Producing object Creating a single config object
Creational How objects are created Factory Producing object Creating a single config object
Creational How objects are created Factory Producing object Creating a single config object
• To achieve this, we store that single instance in a static variable, so it can be shared globally and
accessed without creating new objects.
Limitation
• Tightly coupled code
• Concurrency issues
Quick Question (1 min) (more than 1 correct)
Which of the following are characteristics or consequences of using the Singleton pattern?
A. Ensures a class has only one instance
B. Increases testability due to easy mocking
C. Provides global access to the instance
D. May introduce hidden dependencies and global state
Explanation:
A: Correct – Singleton restricts instantiation to only one object.
B: Incorrect – In fact, Singleton decreases testability because it is difficult to replace with a mock or
stub.
C: Correct – Singleton provides a globally accessible instance via a static method.
D: Correct – This global access can lead to hidden dependencies and tight coupling, harming
testability and maintainability.
Correct Answers: A, C, and D
Structural Pattern – Adapter Pattern
(How classes/objects are organized)
• Sometimes we want to use an existing class, but its interface doesn’t
match.
Limitations
•Adds a layer of indirection (more classes)
•Can become complex if too many adapters are needed.
Quick Question (1 min) (more than 1 correct)
Which of the following statements about the Adapter Design Pattern are correct?
A. The Adapter pattern allows incompatible interfaces to work together
B. The Adapter modifies the legacy system's source code to match the new interface.
C. The Adapter implements the expected interface and internally calls the legacy methods.
D. Adapter pattern can only be used if the legacy system implements the new interface.
Explanation:
A) Correct. This is the fundamental purpose of the Adapter pattern.
B) Incorrect. The Adapter does not modify legacy code; it wraps it.
C) Correct. The Adapter class implements the new interface and delegates calls to the legacy system.
D) Incorrect. The legacy system need not always implement the new interface.
Correct answer: A, C
Behavioral Pattern – Strategy Pattern
(How objects communicate)
Scenario: Imagine a bank app that can calculate interest differently for different account types — say:
• SavingsAccount
• CurrentAccount
• FixedDepositAccount
• No need of hardcoding.
• It lets the algorithm vary independently from the clients that use it.
Strategy Pattern (context, interface,
strategies)
Structure:
1. Context: The class that uses a strategy (e.g., BankAccount).
Strategy Interface
Concrete Strategy 2
Strategy Pattern cont..
Main Class
Strategy Pattern cont..
Benefits
❑Add new strategies without changing existing code.
Drawbacks
• More classes.
• May add slight overhead due to indirection.
Quick Question (1 min) (more than 1 correct)
Which of the following are advantages of using the Strategy design pattern?
A. It eliminates the need for inheritance by using composition
B. It allows algorithms to be selected at runtime
C. It tightly couples the context class with specific strategies
D. It promotes the adding new strategies with modifying others.
Explanation:
A: Correct – Strategy promotes composition over inheritance, enabling behavior change
without subclassing.
B: Correct – Different strategies (algorithms) can be swapped at runtime.
C: Incorrect – Strategy reduces coupling by separating algorithms from the context class.
D: Correct – You can add new strategies without modifying existing ones.
Correct Answers: A, B, and D
Key Takeaways
• Use design patterns to solve common design problems cleanly.