0% found this document useful (0 votes)
7 views4 pages

Lab 6_ Using Design Patterns

Uploaded by

danieltshuma64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Lab 6_ Using Design Patterns

Uploaded by

danieltshuma64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Week 6 Lab Guide: Using Design Patterns

1. Introduction to Design Patterns


Design patterns are reusable solutions to common software design problems. Instead of
reinventing the wheel, developers use proven design patterns to create scalable,
maintainable, and flexible software.

Why Use Design Patterns?

• Improves code reusability and modularity.


• Helps maintain consistent coding structures.
• Enhances scalability and extensibility.
• Reduces code duplication and improves efficiency.

2. Components of a Design Pattern


Each design pattern has a standard structure:

• Context: When and where the pattern applies.


• Problem: The design challenge it solves.
• Forces: Constraints to consider.
• Solution: The approach used to solve the problem.
• Antipatterns (Bad Practices): Mistakes to avoid when implementing the pattern.

3. Commonly Used Design Patterns


Design patterns are divided into three main categories:

A. Creational Patterns (Object Creation Strategies)

1. Singleton Pattern – Ensures that only one instance of a class exists.


2. Factory Pattern – Provides a centralized object creation method.
3. Builder Pattern – Constructs complex objects step-by-step.
B. Structural Patterns (Organizing Classes & Objects)

4. Adapter Pattern – Converts one interface into another compatible interface.


5. Proxy Pattern – Acts as a placeholder for another object.
6. Façade Pattern – Provides a simplified interface to a complex system.

C. Behavioral Patterns (Managing Object Interaction)

7. Observer Pattern – Enables objects to listen for changes in another object.


8. Delegation Pattern – Passes responsibility from one class to another.

4. Example: Singleton Pattern in Java


Context: When we need a single, shared instance of a class.

class Singleton {
private static Singleton instance;

private Singleton() { }

public static Singleton getInstance() {


if (instance == null) {
instance = new Singleton();
}
return instance;
}
}

5. Example: Observer Pattern in Java


Context: When multiple objects need to receive updates from a central object.

import java.util.ArrayList;
import java.util.List;

// Observer interface
interface Observer {
void update(String message);
}
// Concrete observer classes
class SubscriberA implements Observer {
public void update(String message) {
System.out.println("Subscriber A received: " + message);
}
}

class SubscriberB implements Observer {


public void update(String message) {
System.out.println("Subscriber B received: " + message);
}
}

// Observable class
class NewsAgency {
private List<Observer> subscribers = new ArrayList<>();

public void addSubscriber(Observer observer) {


subscribers.add(observer);
}

public void publishNews(String news) {


for (Observer observer : subscribers) {
observer.update(news);
}
}
}

public class ObserverPatternExample {


public static void main(String[] args) {
NewsAgency agency = new NewsAgency();
SubscriberA subA = new SubscriberA();
SubscriberB subB = new SubscriberB();

agency.addSubscriber(subA);
agency.addSubscriber(subB);

agency.publishNews("Breaking News: Java is awesome!");


}
}

6. Lab Task: Implementing the Observer Pattern

Objective:

Students will implement the Observer Pattern, where multiple objects subscribe to
updates from a main object.

Instructions:

1. Create an interface Observer with a method update(String message).


2. Create a class WeatherStation (Observable) that holds a list of observers and
sends weather updates.
3. Create two observer classes (PhoneDisplay and WebDisplay) that implement
Observer.
4. In main(), register PhoneDisplay and WebDisplay to WeatherStation.
5. Call sendWeatherUpdate() in WeatherStation, and both displays should receive
the update.

Expected Output:

Phone Display: Weather Update - Sunny, 25°C


Web Display: Weather Update - Sunny, 25°C

You might also like