Docsity

Java Design Patterns Full Lecture note, Lecture notes of Computer science

Partial preview of the text

Download Java Design Patterns Full Lecture note and more Lecture notes Computer science in PDF only on Docsity!

Design Patterns

Shashikala Madumali BEng in Software Engineering - IIC (Cambodia) Dip in Law

Introduction

1. What is Java Design Pattern

2. Type of Java Design Pattern

3. Benefit of Using Design Patterns

4. Purpose and main characteristics

5. Creational Design Pattern

6. Structural Design Pattern

7. Behavioral Design Pattern

8. Anti Design Pattern

9. Real World Applications

10. Conclusion

11. Q&A

Type of Java Design Pattern

As per the design pattern reference book Design Patterns -
Elements of Reusable Object-Oriented Software , there are 23
design patterns which can be classified in three categories:
ā— Creational patterns
ā— Structural patterns
ā— Behavioral patterns

Benefit of Using Design Patterns

ā— Reusable Solutions

ā— Improved Code Readability

ā— Facilitate Code Maintenance

ā— Promote Best Practices

ā— Decoupling of Component

ā— Flexibility and Scalability

ā— Efficient Problem Solving

ā— Separation of Concerns

ā— Better Communication and Collaboration

ā— Support for Refactoring

ā— Increased Development Speed

ā— Enhanced Testability

ā— Reduced Technical Debt

ā— Consistency Across Projects

Structural Design Pattern

What is structural

design pattern

structural design

patterns

These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.

Focus: Object Composition

ā— Adapter ā— Bridge ā— Composite ā— Decorator ā— Facade ā— Flyweight ā— Proxy

Behavioral Design Pattern

What is Behavioral

design pattern

Behavioral design

patterns

These patterns are concerned with communication between objects and the distribution of responsibilities. They help manage complex control flows and ensure that objects interact in a manageable and efficient way

Focus: Object Interaction

ā— Chain of Responsibility ā— Command ā— Interpreter ā— Iterator ā— Mediator ā— Memento ā— Observer ā— State ā— Strategy ā— Template Method ā— Visitor

Creational

Design Pattern

Singleton design pattern

Singleton pattern is one of the simplest design patterns in

Java. This type of design pattern comes under creational

pattern as this pattern provides one of the best ways to create

an object.

This pattern involves a single class which is responsible to

create an object while making sure that only single object gets

created. This class provides a way to access its only object

which can be accessed directly without need to instantiate the

object of the class.

ā— Controls access to a shared resource (e.g., database connection).

ā— Ensures a class has only one instance.

ā— Easy to implement.

Advantage & disadvantage

Advantage disadvantage

ā— Hard to test because it introduces global state.

ā— Can violate the Single Responsibility Principle (SRP).

ā— Creates tight coupling if misused.

public class Singleton {

private static Singleton instance;

private Singleton() { }

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

public void displayMessage() { System.out.println("Hello from Singleton!"); } }

public class Main { public static void main(String[] args) { Singleton singleton = Singleton.getInstance(); singleton.displayMessage(); } }

public class Singleton {

private static SingleObject instance = new SingleObject();

private Singleton() { }

public static Singleton getInstance() { return instance; }

public void displayMessage() { System.out.println("Hello from Singleton!"); } }

public class Main { public static void main(String[] args) { Singleton singleton = Singleton.getInstance(); singleton.displayMessage(); } }

Factory Method design pattern

Factory pattern is one of the most used design patterns in Java.

This type of design pattern comes under creational pattern as this

pattern provides one of the best ways to create an object.

In Factory pattern, we create object without exposing the creation

logic to the client and refer to newly created object using a

common interface.

When to Use Factory Method Pattern

ā— Object Creation: When the creation process of an object is complex and requires multiple steps or configurations.

ā— Decoupling: When you want to decouple the object creation process from the code that uses the object, making your code more flexible and easier to maintain.

ā— Dynamic Object Creation: When you want to instantiate different classes at runtime based on provided parameters.

Special Characteristics

ā— Factory Method: A method for creating objects that subclasses can override to create specific types of objects.

ā— Product Interface: Defines the interface for the objects that the factory method creates.

ā— Concrete Products: Implement the product interface and provide the specific behavior.

interface Shape { void draw(); }

class Circle implements Shape { public void draw() { System.out.println("Drawing a Circle"); } }

class Rectangle implements Shape { public void draw() { System.out.println("Drawing a Rectangle"); } }

class ShapeFactory {

public Shape getShape(String shapeType) { if (shapeType == null) { return null; } if (shapeType.equalsIgnoreCase("CIRCLE")) { return new Circle(); } else if (shapeType.equalsIgnoreCase("RECTANGLE")) { return new Rectangle(); } return null; } }

public class Main { public static void main(String[] args) { ShapeFactory shapeFactory = new ShapeFactory();

Shape circle = shapeFactory.getShape("CIRCLE"); circle.draw();

Shape rectangle = shapeFactory.getShape("RECTANGLE"); rectangle.draw(); } }