0% found this document useful (0 votes)
6 views10 pages

Design Patterns in Java Examples

The document provides examples of four design patterns: Adapter, Factory, Singleton, and Observer. Each pattern is illustrated with Java code, demonstrating how to implement them in a practical scenario. The Adapter pattern allows a US plug to connect to an EU socket, the Factory pattern creates different shapes, the Singleton pattern ensures a single instance of a database, and the Observer pattern notifies subscribers of news updates.

Uploaded by

prabhatgt421
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)
6 views10 pages

Design Patterns in Java Examples

The document provides examples of four design patterns: Adapter, Factory, Singleton, and Observer. Each pattern is illustrated with Java code, demonstrating how to implement them in a practical scenario. The Adapter pattern allows a US plug to connect to an EU socket, the Factory pattern creates different shapes, the Singleton pattern ensures a single instance of a database, and the Observer pattern notifies subscribers of news updates.

Uploaded by

prabhatgt421
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

Adapter Pattern example

// Target Interface
interface EUPlug {
void plugIn();
}
// Adaptee
class USPlug {
void connect()
{
[Link]("US plug connected to power.");
}
}
// Adapter class
class PlugAdapter implements EUPlug {
private USPlug usPlug;

public PlugAdapter(USPlug usPlug) {


[Link] = usPlug;
}
@Override
public void plugIn() {
[Link]();
}
}
public class AdapterExample {
public static void main(String[] args) {
USPlug usPlug = new USPlug();
EUPlug adapter = new PlugAdapter(usPlug);

[Link]();
}
}
Factory Pattern example
interface Shape {
void draw();
}
class Circle implements Shape {
@Override
public void draw() {
[Link]("Drawing a Circle.");
}
}
class Rectangle implements Shape {
@Override
public void draw() {
[Link]("Drawing a Rectangle.");
}
}
class Square implements Shape {
@Override
public void draw() {
[Link]("Drawing a Square.");
}
}
class ShapeFactory {
public Shape getShape(String shapeType) {
if (shapeType == null) {
return null;
}
if (shapeType == "CIRCLE") {
return new Circle();
} else if (shapeType == "RECTANGLE") {
return new Rectangle();
} else if (shapeType == "SQUARE") {
return new Square();
}
return null;
}
}

public class FactoryPatternExample {


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

Shape shape1 = [Link]("CIRCLE");


[Link]();
Shape shape2 = [Link]("RECTANGLE");
[Link]();
Shape shape3 = [Link]("SQUARE");
[Link]();
}
}
Singleton Pattern Example
class Database {
private static Database instance;
private Database() {
// Private constructor
}
public static Database getInstance() {
if (instance == null) {
instance = new Database();
}
return instance;
}
public void connect() {
[Link]("Connected to the database.");
}
}
public class SingletonPatternExample {
public static void main(String[] args) {
Database db1 = [Link]();
[Link]();
Database db2 = [Link]();
[Link]();
if (db1 == db2)
{
[Link]("Both db1 and db2 are the same instance.");
}
}
}
Observer Pattern Example

NewsAgency: The news agency that notifies subscribers of news updates.


NewsReader: The interface that subscribers must implement to receive updates.
Reader: A specific subscriber that reacts to news updates.

import [Link];

import [Link];

interface NewsReader {

void update(String news);

class NewsAgency {

private List<NewsReader> readers = new ArrayList<>();

private String latestNews;

public void addReader(NewsReader reader) {

[Link](reader);

public void removeReader(NewsReader reader) {

[Link](reader);

}
private void notifyReaders() {

for (NewsReader reader : readers) {

[Link](latestNews);

public void setNews(String news) {

[Link] = news;

notifyReaders();

class Reader implements NewsReader {

private String name;

public Reader(String name) {

[Link] = name;

@Override

public void update(String news) {

[Link](name + " received news update: " +


news);
}

public class ObserverPatternExample {

public static void main(String[] args) {

NewsAgency newsAgency = new NewsAgency();

// Create news readers

NewsReader reader1 = new Reader("Alice");

NewsReader reader2 = new Reader("Bob");

// Register readers with the news agency

[Link](reader1);

[Link](reader2);

// Publish news

[Link]("Breaking News: News Delivered");

You might also like