0% found this document useful (0 votes)
3 views

Java Design Patterns

The document outlines seven major design patterns in Java, including the Singleton, Factory, Builder, Observer, Strategy, Decorator, and Adapter patterns. Each pattern is accompanied by a code example demonstrating its implementation and usage. These patterns are essential for creating flexible and maintainable software architectures.

Uploaded by

Deepak Pal
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)
3 views

Java Design Patterns

The document outlines seven major design patterns in Java, including the Singleton, Factory, Builder, Observer, Strategy, Decorator, and Adapter patterns. Each pattern is accompanied by a code example demonstrating its implementation and usage. These patterns are essential for creating flexible and maintainable software architectures.

Uploaded by

Deepak Pal
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/ 5

7 Major Design Patterns in Java

1. Singleton Pattern

public class Singleton {


private static Singleton instance;

private Singleton() {}

public static Singleton getInstance() {


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

public void showMessage() {


System.out.println("Hello from Singleton!");
}
}

2. Factory Pattern

interface Animal {
void sound();
}

class Dog implements Animal {


public void sound() {
System.out.println("Woof");
}
}

class Cat implements Animal {


public void sound() {
System.out.println("Meow");
}
}

class AnimalFactory {
public static Animal getAnimal(String type) {
if ("dog".equalsIgnoreCase(type)) return new Dog();
else if ("cat".equalsIgnoreCase(type)) return new Cat();
return null;
}
}
7 Major Design Patterns in Java

3. Builder Pattern

class Pizza {
private String base;
private String cheese;
private String toppings;

public static class Builder {


private String base;
private String cheese;
private String toppings;

public Builder base(String base) {


this.base = base;
return this;
}

public Builder cheese(String cheese) {


this.cheese = cheese;
return this;
}

public Builder toppings(String toppings) {


this.toppings = toppings;
return this;
}

public Pizza build() {


Pizza pizza = new Pizza();
pizza.base = this.base;
pizza.cheese = this.cheese;
pizza.toppings = this.toppings;
return pizza;
}
}

public void display() {


System.out.println(base + ", " + cheese + ", " + toppings);
}
}

4. Observer Pattern

import java.util.*;

interface Observer {
7 Major Design Patterns in Java

void update(String message);


}

class User implements Observer {


private String name;
public User(String name) { this.name = name; }

public void update(String message) {


System.out.println(name + " received: " + message);
}
}

class Channel {
private List<Observer> subscribers = new ArrayList<>();

public void subscribe(Observer o) {


subscribers.add(o);
}

public void notifyAllObservers(String msg) {


for (Observer o : subscribers) {
o.update(msg);
}
}
}

5. Strategy Pattern

interface PaymentStrategy {
void pay(int amount);
}

class CreditCardPayment implements PaymentStrategy {


public void pay(int amount) {
System.out.println("Paid " + amount + " with Credit Card");
}
}

class PayPalPayment implements PaymentStrategy {


public void pay(int amount) {
System.out.println("Paid " + amount + " with PayPal");
}
}

class PaymentContext {
private PaymentStrategy strategy;
7 Major Design Patterns in Java

public void setStrategy(PaymentStrategy strategy) {


this.strategy = strategy;
}

public void pay(int amount) {


strategy.pay(amount);
}
}

6. Decorator Pattern

interface Coffee {
String getDescription();
int getCost();
}

class BasicCoffee implements Coffee {


public String getDescription() { return "Basic Coffee"; }
public int getCost() { return 5; }
}

class MilkDecorator implements Coffee {


private Coffee coffee;

public MilkDecorator(Coffee coffee) {


this.coffee = coffee;
}

public String getDescription() {


return coffee.getDescription() + ", Milk";
}

public int getCost() {


return coffee.getCost() + 2;
}
}

7. Adapter Pattern

interface MediaPlayer {
void play(String fileName);
}

class MP3Player implements MediaPlayer {


public void play(String fileName) {
System.out.println("Playing MP3: " + fileName);
7 Major Design Patterns in Java

}
}

class VLCPlayer {
public void playVLC(String fileName) {
System.out.println("Playing VLC: " + fileName);
}
}

class MediaAdapter implements MediaPlayer {


private VLCPlayer vlcPlayer;

public MediaAdapter() {
vlcPlayer = new VLCPlayer();
}

public void play(String fileName) {


vlcPlayer.playVLC(fileName);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy