Design Pattern - Basic
Design Pattern - Basic
Design patterns are reusable solutions to common software design problems. They provide proven approaches to solving specific design challenges and promote code reusability, maintainability, and flexibility. Here, I'll explain some popular design patterns with Java examples:
1. Singleton Pattern:
The Singleton pattern ensures that only one instance of a class is created and provides global access to it.
```java
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
```
2. Factory Pattern:
The Factory pattern provides an interface for creating objects, but allows subclasses to decide which class to instantiate.
```java
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 createShape(String type) {
if (type.equalsIgnoreCase("circle")) {
return new Circle();
} else if (type.equalsIgnoreCase("rectangle")) {
return new Rectangle();
}
return null;
}
}
public class FactoryPatternExample {
public static void main(String[] args) {
ShapeFactory factory = new ShapeFactory();
Shape circle = factory.createShape("circle");
circle.draw(); // Output: Drawing a circle
Shape rectangle = factory.createShape("rectangle");
rectangle.draw(); // Output: Drawing a rectangle
}
}
```
3. Observer Pattern:
The Observer pattern defines a one-to-many dependency between objects, so that when one object changes state, its dependents are notified and updated automatically.
```java
import java.util.ArrayList;
import java.util.List;
interface Observer {
void update(String message);
}
class Subject {
private List<Observer> observers = new ArrayList<>();
private String message;
public void attach(Observer observer) {
observers.add(observer);
}
public void detach(Observer observer) {
observers.remove(observer);
}
public void setMessage(String message) {
this.message = message;
notifyObservers();
}
private void notifyObservers() {
for (Observer observer : observers) {
observer.update(message);
}
}
}
class ConcreteObserver implements Observer {
private String name;
public ConcreteObserver(String name) {
this.name = name;
}
public void update(String message) {
System.out.println(name + " received message: " + message);
}
}
public class ObserverPatternExample {
public static void main(String[] args) {
Subject subject = new Subject();
Observer observer1 = new ConcreteObserver("Observer 1");
Observer observer2 = new ConcreteObserver("Observer 2");
subject.attach(observer1);
subject.attach(observer2);
subject.setMessage("Hello observers!");
// Output:
// Observer 1 received message: Hello observers!
// Observer 2 received message: Hello observers!
subject.detach(observer2);
subject.setMessage("Goodbye observers!");
// Output:
// Observer 1 received message: Goodbye observers!
}
}
```
These examples demonstrate the Singleton, Factory, and Observer patterns. There are many more design patterns, such as Builder, Decorator, Strategy, and MVC, among others. Each pattern addresses different design concerns and provides effective solutions. It's important to understand when and how to apply these patterns based on the specific requirements of your software design.
Comments
Post a Comment