




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































Shashikala Madumali BEng in Software Engineering - IIC (Cambodia) Dip in Law
Benefit of Using 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
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
ā Controls access to a shared resource (e.g., database connection).
ā Ensures a class has only one instance.
ā Easy to implement.
ā 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(); } }
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(); } }