logologo
  • AI Tools

    DB Query GeneratorMock InterviewResume Builder
  • XpertoAI
  • MVP Ready
  • Resources

    CertificationsTopicsExpertsCoursesArticlesQuestionsVideosJobs
logologo

Elevate Your Coding with our comprehensive articles and niche courses.

Useful Links

  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation
  • About Us

Resources

  • Xperto-AI
  • Certifications
  • Python
  • GenAI
  • Machine Learning

Interviews

  • DSA
  • System Design
  • Design Patterns
  • Frontend System Design
  • ReactJS

Procodebase © 2024. All rights reserved.

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Design Patterns in Object-Oriented Programming with Java

author
Generated by
Krishna Adithya Gaddam

11/12/2024

AI GeneratedJava

Introduction to Design Patterns

Design patterns are proven solutions to common software design problems, facilitating code reusability, maintainability, and scalability. In the context of object-oriented programming (OOP), they provide a way to write code that others (or you in the future!) can easily understand and work with.

Categories of Design Patterns

Design patterns in OOP are generally divided into three main categories:

  1. Creational Patterns - These patterns deal with object creation mechanisms.
  2. Structural Patterns - These patterns focus on class and object composition.
  3. Behavioral Patterns - These patterns are concerned with object collaboration and communication.

Let’s break down each category with examples in Java.

1. Creational Design Patterns

a. Singleton Pattern

The Singleton Pattern ensures that a class has only one instance and provides a global point of access to it. This is particularly useful when managing shared resources, like database connections.

Example:

public class Singleton { private static Singleton instance; private Singleton() { // private constructor to restrict instantiation } public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }

Usage:

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

b. Factory Method Pattern

The Factory Method Pattern defines an interface for creating an object but allows subclasses to alter the type of created objects. This pattern promotes loose coupling.

Example:

abstract class Vehicle { public abstract void drive(); } class Car extends Vehicle { public void drive() { System.out.println("Driving a car"); } } class Bike extends Vehicle { public void drive() { System.out.println("Riding a bike"); } } abstract class VehicleFactory { public abstract Vehicle createVehicle(); } class CarFactory extends VehicleFactory { public Vehicle createVehicle() { return new Car(); } } class BikeFactory extends VehicleFactory { public Vehicle createVehicle() { return new Bike(); } }

Usage:

public class Main { public static void main(String[] args) { VehicleFactory factory = new CarFactory(); Vehicle vehicle = factory.createVehicle(); vehicle.drive(); // Output: Driving a car } }

2. Structural Design Patterns

a. Adapter Pattern

The Adapter Pattern allows incompatible interfaces to work together. It acts as a bridge between two incompatible interfaces.

Example:

interface MediaPlayer { void play(String audioType, String fileName); } interface AdvancedMediaPlayer { void playVlc(String fileName); void playMp4(String fileName); } class VlcPlayer implements AdvancedMediaPlayer { public void playVlc(String fileName) { System.out.println("Playing vlc file. Name: " + fileName); } public void playMp4(String fileName) {} } class Mp4Player implements AdvancedMediaPlayer { public void playVlc(String fileName) {} public void playMp4(String fileName) { System.out.println("Playing mp4 file. Name: " + fileName); } } class MediaAdapter implements MediaPlayer { AdvancedMediaPlayer advancedMediaPlayer; public MediaAdapter(String audioType) { if (audioType.equalsIgnoreCase("vlc")) { advancedMediaPlayer = new VlcPlayer(); } else if (audioType.equalsIgnoreCase("mp4")) { advancedMediaPlayer = new Mp4Player(); } } public void play(String audioType, String fileName) { if (audioType.equalsIgnoreCase("vlc")) { advancedMediaPlayer.playVlc(fileName); } else if (audioType.equalsIgnoreCase("mp4")) { advancedMediaPlayer.playMp4(fileName); } } }

Usage:

public class Main { public static void main(String[] args) { MediaPlayer player = new MediaAdapter("vlc"); player.play("vlc", "song.vlc"); } }

3. Behavioral Design Patterns

a. Observer Pattern

The Observer Pattern defines a one-to-many dependency between objects, allowing one object (the subject) to notify multiple observers about changes in its state.

Example:

import java.util.ArrayList; import java.util.List; interface Observer { void update(String 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); } } class Subject { private List<Observer> observers = new ArrayList<>(); public void attach(Observer observer) { observers.add(observer); } public void notify(String message) { for (Observer observer : observers) { observer.update(message); } } }

Usage:

public class Main { 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.notify("Hello Observers!"); } }

b. Strategy Pattern

The Strategy Pattern allows for the algorithm to vary independently from the clients that use it. In simpler terms, it enables you to select an algorithm's behavior at runtime.

Example:

interface Strategy { void execute(); } class ConcreteStrategyA implements Strategy { public void execute() { System.out.println("Executing Strategy A"); } } class ConcreteStrategyB implements Strategy { public void execute() { System.out.println("Executing Strategy B"); } } class Context { private Strategy strategy; public void setStrategy(Strategy strategy) { this.strategy = strategy; } public void executeStrategy() { strategy.execute(); } }

Usage:

public class Main { public static void main(String[] args) { Context context = new Context(); context.setStrategy(new ConcreteStrategyA()); context.executeStrategy(); // Output: Executing Strategy A context.setStrategy(new ConcreteStrategyB()); context.executeStrategy(); // Output: Executing Strategy B } }

Wrap-Up

Since design patterns play a pivotal role in enhancing software design in Java, understanding and implementing these patterns will undoubtedly improve your coding practices. Remember that each pattern has its use case, and selecting the appropriate pattern can help streamline your development process.

As you progress through your journey in object-oriented programming, you'll find these design patterns are not just theoretical concepts but practical tools that can significantly enhance your coding efficiency and project maintainability.

Popular Tags

Javadesign patternsobject-oriented programming

Share now!

Like & Bookmark!

Related Courses

  • Spring Boot CRUD Mastery with PostgreSQL

    30/10/2024 | Java

  • Advanced Java Memory Management and Garbage Collection

    16/10/2024 | Java

  • Java Essentials and Advanced Concepts

    23/09/2024 | Java

  • Mastering Object-Oriented Programming in Java

    11/12/2024 | Java

  • Spring Boot Mastery from Basics to Advanced

    24/09/2024 | Java

Related Articles

  • Building Real-World Applications with OOP in Java

    11/12/2024 | Java

  • Best Practices for Writing Clean Code in Java

    23/09/2024 | Java

  • Integrating Java with Modern Frontend Frameworks

    03/09/2024 | Java

  • Understanding Generics in Java

    11/12/2024 | Java

  • Mastering Spring Boot with JPA and Hibernate

    24/09/2024 | Java

  • Demystifying Lambda Expressions in Java

    23/09/2024 | Java

  • Best Practices in Java Concurrency

    16/10/2024 | Java

Popular Category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design