Software Development

Design Patterns: Interview Questions & Answers

Design patterns are reusable solutions to common software design problems that have been tested and proven over time. They provide a standardized approach to solving design problems, and help developers create software that is more maintainable, flexible, and scalable.

Design patterns were first introduced in the book “Design Patterns: Elements of Reusable Object-Oriented Software” by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, which was published in 1994. The authors identified 23 commonly occurring design problems in software development and provided solutions in the form of design patterns.

1. Types of Design Patterns

There are three main types of design patterns:

1.1 Creational patterns

Creational patterns are a type of design pattern that focuses on the process of object creation. They provide ways to create objects in a manner suitable for a given situation, and allow developers to control the creation process of objects to improve code organization, maintainability, and flexibility.

Here are some examples of creational patterns:

  1. Singleton: Ensures that a class has only one instance, and provides a global point of access to that instance.
  2. Factory Method: Defines an interface for creating objects, but allows subclasses to decide which class to instantiate.
  3. Abstract Factory: Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
  4. Builder: Separates the construction of a complex object from its representation, allowing the same construction process to create different representations.
  5. Prototype: Creates new objects by cloning existing ones, rather than creating new objects from scratch.

These patterns can be used in a variety of programming languages and frameworks, and can improve the efficiency and organization of object-oriented programming. However, it’s important to choose the appropriate pattern for the specific situation, and not to overuse them, as doing so can lead to unnecessarily complex code.

1.2 Structural patterns

Structural patterns are a type of design pattern that focus on the composition of classes and objects to form larger structures. They help developers create interfaces between objects to simplify the code, and allow them to solve common design problems related to object composition in a reusable manner.

Here are some examples of structural patterns:

  1. Adapter: Converts the interface of a class into another interface that clients expect, allowing classes with incompatible interfaces to work together.
  2. Bridge: Separates an object’s abstraction from its implementation, allowing both to be modified independently.
  3. Composite: Composes objects into tree structures to represent part-whole hierarchies, allowing clients to treat individual objects and compositions of objects uniformly.
  4. Decorator: Dynamically adds responsibilities to an object by wrapping it in a decorator object that provides the same interface, but with additional functionality.
  5. Facade: Provides a unified interface to a set of interfaces in a subsystem, simplifying the use of the subsystem and reducing its dependencies.
  6. Flyweight: Shares common state among multiple objects, reducing memory usage and improving performance.

These patterns can be used to solve a wide variety of design problems related to object composition, and can help developers create more flexible, maintainable, and scalable software. However, it’s important to choose the appropriate pattern for the specific situation, and to avoid over-engineering or introducing unnecessary complexity.

1.3 Behavioral patterns

Behavioral patterns are a type of design pattern that focus on communication between objects and provide ways to organize objects and algorithms to achieve specific results. They help developers manage complex relationships between objects and simplify the flow of control within a program.

Here are some examples of behavioral patterns:

  1. Chain of Responsibility: Passes requests along a chain of objects, allowing multiple objects to handle the request without specifying which object will handle it.
  2. Command: Encapsulates a request as an object, allowing the request to be parameterized, queued, and logged, and allowing for undo/redo functionality.
  3. Interpreter: Defines a grammar for a language and uses an interpreter to interpret expressions in the language.
  4. Iterator: Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
  5. Mediator: Defines an object that encapsulates how a set of objects interact, reducing dependencies among the objects.
  6. Observer: Defines a one-to-many dependency between objects, so that when one object changes state, all its dependents are notified and updated automatically.
  7. State: Allows an object to alter its behavior when its internal state changes, creating the illusion of different classes.
  8. Strategy: Defines a family of algorithms, encapsulates each one, and makes them interchangeable at runtime.
  9. Template Method: Defines the skeleton of an algorithm in a base class, allowing subclasses to provide specific implementations of some steps.
  10. Visitor: Separates an algorithm from an object structure by moving the algorithm into a separate object, allowing new operations to be added without changing the object structure.

These patterns can be used to manage complex interactions between objects and simplify the flow of control within a program. However, it’s important to choose the appropriate pattern for the specific situation, and to avoid introducing unnecessary complexity or violating the principle of separation of concerns.

2. 10 Famous Design Patterns Questions and Answers

2.1. What are design patterns, and why are they important in software development?

Design patterns are reusable solutions to common software design problems that developers encounter when creating software applications. They are generalized solutions that have been found to be effective in solving recurring problems in software design. Design patterns help developers to solve design problems in an efficient and effective way, without having to reinvent the wheel each time.

Design patterns provide a common vocabulary and a shared understanding of best practices, making it easier for developers to communicate and collaborate effectively. They also improve the quality of the software by promoting modularity, flexibility, and scalability. When applied correctly, design patterns can help to make software more maintainable, testable, and extensible, leading to reduced costs and better outcomes for software projects.

Furthermore, design patterns are important because they can help to bridge the gap between software development and business requirements. By providing a common language and a shared understanding of design problems, design patterns can help to align the technical implementation of software with business goals and requirements. This can lead to improved communication between stakeholders and a better alignment of software with business needs.

In summary, design patterns are important in software development because they provide a common vocabulary, promote best practices, improve the quality of software, and help to bridge the gap between technical implementation and business requirements.

2.2. Can you describe the differences between creational, structural, and behavioral design patterns?

The differences between creational, structural, and behavioral design patterns are:

  • Creational patterns: These patterns focus on object creation mechanisms, trying to create objects in a manner that suits the situation best. They hide the creation logic of objects from the client, making the code easier to maintain and modify. Examples of creational patterns include Factory Method, Abstract Factory, Builder, Singleton, and Prototype.
  • Structural patterns: These patterns focus on the composition of classes and objects, and how they can be combined to form larger structures. They make it easier to compose objects into more complex structures while keeping the relationships between them flexible and adaptable. Examples of structural patterns include Adapter, Bridge, Composite, Decorator, Facade, Flyweight, and Proxy.
  • Behavioral patterns: These patterns focus on communication between objects, specifying how objects interact and behave in certain situations. They help to manage complex relationships between objects and simplify the flow of control within a program. Examples of behavioral patterns include Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Observer, State, Strategy, Template Method, and Visitor.

In summary, creational patterns focus on object creation mechanisms, structural patterns focus on composition of classes and objects, and behavioral patterns focus on communication between objects. Each type of pattern is designed to solve specific types of problems in software design, and choosing the right pattern for the situation can help to make software more maintainable, testable, and extensible.

2.3. Can you give an example of a creational design pattern and explain how it works?

one example of a creational design pattern is the Factory Method pattern. The Factory Method pattern defines an interface for creating objects, but lets subclasses decide which classes to instantiate. In other words, it provides an abstract class or interface with a method for creating objects, but the specific type of object created is determined by the subclass.

Here is an example of how the Factory Method pattern works:

Let’s say we have a class hierarchy for different types of pizza, with a base Pizza class and subclasses CheesePizza, PepperoniPizza, and VeggiePizza. We also have a PizzaStore class that creates and sells pizzas. Instead of creating the pizzas directly in the PizzaStore, we use a PizzaFactory class to create the specific type of pizza. The PizzaFactory class is an abstract class or interface that defines the createPizza method, which returns a Pizza object.

Each subclass of PizzaFactory can implement the createPizza method to create a specific type of pizza, such as createPizza() method in ChicagoPizzaFactory may create a ChicagoStyleCheesePizza object, while NYCPizzaFactory may create a NYCStyleCheesePizza object. The PizzaStore can then use the PizzaFactory to create the specific type of pizza needed by passing the type of pizza to the createPizza method of the factory.

Here’s some sample code:

public abstract class PizzaFactory {
    public abstract Pizza createPizza(String type);
}

public class NYCPizzaFactory extends PizzaFactory {
    public Pizza createPizza(String type) {
        if (type.equals("cheese")) {
            return new NYCStyleCheesePizza();
        } else if (type.equals("pepperoni")) {
            return new NYCStylePepperoniPizza();
        } else {
            return null;
        }
    }
}

public class PizzaStore {
    private PizzaFactory factory;

    public PizzaStore(PizzaFactory factory) {
        this.factory = factory;
    }

    public Pizza orderPizza(String type) {
        Pizza pizza = factory.createPizza(type);
        pizza.prepare();
        pizza.bake();
        pizza.cut();
        pizza.box();
        return pizza;
    }
}

public class Main {
    public static void main(String[] args) {
        PizzaFactory factory = new NYCPizzaFactory();
        PizzaStore store = new PizzaStore(factory);

        Pizza cheesePizza = store.orderPizza("cheese");
        System.out.println("Ordered a " + cheesePizza.getName());

        Pizza pepperoniPizza = store.orderPizza("pepperoni");
        System.out.println("Ordered a " + pepperoniPizza.getName());
    }
}

In this example, the PizzaStore uses the NYCPizzaFactory to create NYCStyleCheesePizza and NYCStylePepperoniPizza objects. By using the Factory Method pattern, we can create new types of pizza without modifying the PizzaStore class, making our code more modular and extensible.

2.4. Can you give an example of a structural design pattern and explain how it works?

Nn example of a structural design pattern in Java called the Decorator pattern:

The Decorator pattern is used to dynamically add functionality to an object at runtime without changing its original implementation. This pattern is useful when we want to add functionality to an object, but we do not want to create a subclass for every possible combination of added functionality.

Example code:

interface Pizza {
    String getDescription();
    double getCost();
}

class PlainPizza implements Pizza {
    @Override
    public String getDescription() {
        return "Plain pizza";
    }

    @Override
    public double getCost() {
        return 5.00;
    }
}

abstract class ToppingDecorator implements Pizza {
    protected Pizza pizza;

    public ToppingDecorator(Pizza pizza) {
        this.pizza = pizza;
    }

    public String getDescription() {
        return pizza.getDescription();
    }

    public double getCost() {
        return pizza.getCost();
    }
}

class Cheese extends ToppingDecorator {
    public Cheese(Pizza pizza) {
        super(pizza);
    }

    @Override
    public String getDescription() {
        return pizza.getDescription() + ", cheese";
    }

    @Override
    public double getCost() {
        return pizza.getCost() + 1.00;
    }
}

class Pepperoni extends ToppingDecorator {
    public Pepperoni(Pizza pizza) {
        super(pizza);
    }

    @Override
    public String getDescription() {
        return pizza.getDescription() + ", pepperoni";
    }

    @Override
    public double getCost() {
        return pizza.getCost() + 2.00;
    }
}

public class DecoratorPatternDemo {
    public static void main(String[] args) {
        Pizza pizza = new PlainPizza();
        pizza = new Cheese(pizza);
        pizza = new Pepperoni(pizza);

        System.out.println("Description: " + pizza.getDescription());
        System.out.println("Cost: $" + pizza.getCost());
    }
}

In this example, Pizza is the base interface that defines the common methods for all pizza types. PlainPizza is a concrete implementation of Pizza that represents a plain pizza without any toppings.

ToppingDecorator is an abstract class that implements the Pizza interface and contains a reference to another Pizza object. This class serves as the base decorator that can be extended to add additional functionality to the Pizza object. Cheese and Pepperoni are concrete decorators that extend ToppingDecorator to add cheese and pepperoni toppings, respectively.

In the main method of the DecoratorPatternDemo class, we create a PlainPizza object and then decorate it with Cheese and Pepperoni toppings using the pizza reference. When we call the getDescription and getCost methods on the pizza object, the decorators add their functionality to the base PlainPizza object. The output of the program shows the description and cost of the pizza with the added toppings.

The Decorator pattern allows us to add new functionality to an object at runtime by wrapping it with one or more decorator objects. This approach is more flexible than subclassing because it allows us to add functionality to an object without creating a new subclass for every possible combination of added functionality.

2.5. Can you give an example of a behavioral design pattern and explain how it works?

An example of a behavioral design pattern is the Strategy pattern.

The Strategy pattern is used when there are multiple algorithms or strategies that can be used to accomplish a task, and you want to be able to switch between them at runtime.

Here’s how it works:

  1. The pattern consists of three main parts: the Context, the Strategy, and the Concrete Strategies.
  2. The Context is an object that has a reference to a Strategy.
  3. The Strategy is an interface that defines a method to be implemented by all Concrete Strategies.
  4. The Concrete Strategies are objects that implement the Strategy interface and provide their own implementation of the method.
  5. The Context delegates the task to the current Strategy object, which executes its own algorithm to accomplish the task.
  6. The Context can switch between different Concrete Strategies at runtime by setting its reference to a different Strategy object.

Let’s say we’re building a banking application that needs to calculate interest rates for different types of accounts. We could use the Strategy pattern to encapsulate the different interest calculation algorithms and make them interchangeable at runtime.

First, we’ll define an interface for the interest calculation strategy:

public interface InterestCalculationStrategy {
    double calculateInterest(double accountBalance);
}

Next, we’ll create different implementations of this interface for each type of account. For example, here’s an implementation for a savings account:

public class SavingsAccountInterestStrategy implements InterestCalculationStrategy {
    private static final double INTEREST_RATE = 0.01;

    @Override
    public double calculateInterest(double accountBalance) {
        return accountBalance * INTEREST_RATE;
    }
}

And here’s an implementation for a checking account:

public class CheckingAccountInterestStrategy implements InterestCalculationStrategy {
    private static final double INTEREST_RATE = 0.005;

    @Override
    public double calculateInterest(double accountBalance) {
        return accountBalance * INTEREST_RATE;
    }
}

Now, we can use these strategies in our Account class, which has a balance and an interest calculation strategy:

public class Account {
    private double balance;
    private InterestCalculationStrategy interestStrategy;

    public Account(double balance, InterestCalculationStrategy interestStrategy) {
        this.balance = balance;
        this.interestStrategy = interestStrategy;
    }

    public double calculateInterest() {
        return interestStrategy.calculateInterest(balance);
    }
}

We can create an account with a savings interest strategy like this:

Account savingsAccount = new Account(1000, new SavingsAccountInterestStrategy());

And we can create an account with a checking interest strategy like this:

Account checkingAccount = new Account(5000, new CheckingAccountInterestStrategy());

Now, we can calculate the interest for each account using the calculateInterest() method:

double savingsInterest = savingsAccount.calculateInterest(); // returns 10.0
double checkingInterest = checkingAccount.calculateInterest(); // returns 25.0

This example demonstrates the Strategy pattern in action. By encapsulating the different interest calculation algorithms in separate classes that implement a common interface, we can make them interchangeable at runtime and use them in the Account class without modifying its code.

2.6. Can you describe the Singleton design pattern and give an example of a scenario where it might be useful?

The Singleton pattern is a creational design pattern that ensures that only one instance of a class can be created and provides a global point of access to that instance. This means that multiple instances of the class cannot be created, and all access to the instance happens through a single point of entry.

To implement the Singleton pattern, the class’s constructor is made private, so it cannot be called from outside the class, and a static method is created to return the single instance of the class. The first time this method is called, the instance is created, and subsequent calls to the method simply return the already-created instance.

Here’s an example scenario where the Singleton pattern might be useful:

Let’s say you’re creating a game that has a high score tracker. You want to make sure that only one instance of the high score tracker exists at any given time, so that scores are tracked consistently across the game. In this case, you could use the Singleton pattern to create a class for the high score tracker, and ensure that only one instance of the class is ever created. Anytime a new score is added to the tracker or the high score is retrieved, it can be accessed through the static method provided by the Singleton class.

Here’s a Java example of the Singleton pattern:

public class HighScoreTracker {
   private static HighScoreTracker instance;
   private int highScore;
   
   // Private constructor to prevent instantiation from outside the class
   private HighScoreTracker() {
      highScore = 0;
   }
   
   // Static method to get the single instance of the class
   public static HighScoreTracker getInstance() {
      if (instance == null) {
         instance = new HighScoreTracker();
      }
      return instance;
   }
   
   // Method to add a new score to the high score tracker
   public void addScore(int newScore) {
      if (newScore > highScore) {
         highScore = newScore;
      }
   }
   
   // Method to get the current high score
   public int getHighScore() {
      return highScore;
   }
}

In this example, the HighScoreTracker class has a private constructor and a static getInstance() method that returns the single instance of the class. The addScore() method is used to add a new score to the high score tracker, and the getHighScore() method returns the current high score.

To use this class in your game, you would call the getInstance() method to get the single instance of the HighScoreTracker class, and then use the addScore() method to add new scores to the tracker and the getHighScore() method to retrieve the current high score. Since the HighScoreTracker class is a Singleton, you can be sure that only one instance of the class exists at any given time, and all access to the instance happens through the getInstance() method.

2.7. Can you describe the Observer design pattern and give an example of a scenario where it might be useful?

The Observer design pattern is a behavioral pattern that allows an object, called the subject, to notify other objects, called observers, when its state changes. The observers register themselves with the subject and are notified automatically when the subject’s state changes. This decouples the subject from the observers, allowing them to be added or removed independently.

In this pattern, there are two main components: the subject and the observers. The subject is the object that maintains its state and notifies the observers when its state changes. The observers are the objects that are interested in the state of the subject and register themselves with the subject to receive notifications.

Here’s an example scenario where the Observer pattern might be useful:

Suppose you have a weather station that measures temperature, humidity, and pressure. You want to create an application that displays this data in real-time on multiple devices, such as a mobile phone, a tablet, and a desktop computer.

In this scenario, you could implement the Observer pattern by making the weather station the subject and the devices the observers. Each device would register itself with the weather station to receive updates when the weather changes. When the weather station measures a change in temperature, humidity, or pressure, it would notify all the registered devices, and they would update their displays accordingly.

This approach allows you to add or remove devices without affecting the weather station’s functionality. It also makes it easy to implement new types of devices that can display the weather data without modifying the weather station’s code.

Here’s a Java example of the Observer pattern:

import java.util.ArrayList;
import java.util.List;

interface Observer {
    void update(float temperature, float humidity, float pressure);
}

interface Subject {
    void registerObserver(Observer o);
    void removeObserver(Observer o);
    void notifyObservers();
}

class WeatherData implements Subject {
    private List<Observer> observers;
    private float temperature;
    private float humidity;
    private float pressure;

    public WeatherData() {
        observers = new ArrayList<>();
    }

    public void registerObserver(Observer o) {
        observers.add(o);
    }

    public void removeObserver(Observer o) {
        int index = observers.indexOf(o);
        if (index >= 0) {
            observers.remove(index);
        }
    }

    public void notifyObservers() {
        for (Observer o : observers) {
            o.update(temperature, humidity, pressure);
        }
    }

    public void setMeasurements(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }

    public void measurementsChanged() {
        notifyObservers();
    }
}

class CurrentConditionsDisplay implements Observer {
    private float temperature;
    private float humidity;
    private Subject weatherData;

    public CurrentConditionsDisplay(Subject weatherData) {
        this.weatherData = weatherData;
        weatherData.registerObserver(this);
    }

    public void update(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        display();
    }

    public void display() {
        System.out.println("Current conditions: " + temperature
            + "F degrees and " + humidity + "% humidity");
    }
}

public class WeatherStation {
    public static void main(String[] args) {
        WeatherData weatherData = new WeatherData();

        CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);

        weatherData.setMeasurements(80, 65, 30.4f);
        weatherData.setMeasurements(82, 70, 29.2f);
        weatherData.setMeasurements(78, 90, 29.2f);
    }
}

In this example, we have a WeatherData class that implements the Subject interface. It has a list of observers that register themselves using the registerObserver method, and are notified of changes in the subject’s state using the notifyObservers method.

The WeatherData class also has a setMeasurements method that updates the temperature, humidity, and pressure measurements, and calls the measurementsChanged method, which in turn calls the notifyObservers method.

We also have a CurrentConditionsDisplay class that implements the Observer interface. It registers itself with the WeatherData subject using its constructor, and is updated whenever the WeatherData object’s state changes by implementing the update method.

In the main method, we create a WeatherData object and a CurrentConditionsDisplay object, and then update the WeatherData object’s state using the setMeasurements method. When the measurements change, the CurrentConditionsDisplay object is automatically updated with the new data and displays the current conditions.

2.8. Can you describe the Factory Method design pattern and give an example of a scenario where it might be useful?

The Factory Method design pattern is a creational pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. It defines an abstract class or interface for creating objects, and then lets subclasses decide which class to instantiate.

In this pattern, there are two main components: the creator and the product. The creator is an abstract class or interface that declares the factory method, which returns an object of the product type. The product is the object that is created by the factory method.

Here’s an example scenario where the Factory Method pattern might be useful:

Suppose you are building a video game that has multiple levels, each with its own set of enemies. The enemies in each level have different abilities and behaviors, and you want to create them dynamically based on the current level.

In this scenario, you could implement the Factory Method pattern by defining an abstract Enemy class that represents the product, and a Level class that represents the creator. The Level class would define an abstract createEnemy method that returns an Enemy object. Each subclass of Level would implement the createEnemy method to create the appropriate type of enemy for that level.

Here’s a Java code example:

abstract class Enemy {
    abstract void attack();
}

class Level {
    Enemy createEnemy() {
        Enemy enemy = makeEnemy();
        // do additional setup or initialization here
        return enemy;
    }
    
    abstract Enemy makeEnemy();
}

class Level1 extends Level {
    Enemy makeEnemy() {
        return new WeakEnemy();
    }
}

class Level2 extends Level {
    Enemy makeEnemy() {
        return new StrongEnemy();
    }
}

class WeakEnemy extends Enemy {
    void attack() {
        System.out.println("Weak enemy attacks with a sword");
    }
}

class StrongEnemy extends Enemy {
    void attack() {
        System.out.println("Strong enemy attacks with fire breath");
    }
}

public class Game {
    public static void main(String[] args) {
        Level level1 = new Level1();
        Level level2 = new Level2();

        Enemy enemy1 = level1.createEnemy();
        Enemy enemy2 = level2.createEnemy();

        enemy1.attack();
        enemy2.attack();
    }
}

In this example, we have an Enemy abstract class that represents the product. We also have a Level abstract class that represents the creator, which defines an abstract makeEnemy method that returns an Enemy object. The Level class also has a createEnemy method that creates an enemy using the makeEnemy method and does additional setup or initialization as needed.

We also have two concrete Level subclasses, Level1 and Level2, which implement the makeEnemy method to create a WeakEnemy and a StrongEnemy, respectively. Finally, we have WeakEnemy and StrongEnemy concrete classes that extend the Enemy abstract class and implement the attack method.

In the main method, we create a Level1 and a Level2 object and use their createEnemy methods to create WeakEnemy and StrongEnemy objects, respectively. We then call the attack method on each of these objects to simulate the enemies attacking the player.

2.9. Can you describe the Adapter design pattern and give an example of a scenario where it might be useful?

The Adapter design pattern is a structural pattern that allows incompatible classes to work together by converting the interface of one class into another interface that the client expects. It is used when an existing class’s interface does not meet the needs of the client.

In this pattern, there are three main components: the client, the adapter, and the adaptee. The client is the class that needs to use the adaptee’s functionality. The adapter is the class that adapts the interface of the adaptee to the interface expected by the client. The adaptee is the class that provides the functionality that the client needs.

Here’s an example scenario where the Adapter pattern might be useful:

Suppose you are building a music player that can play music from different sources, such as CDs, MP3 files, and streaming services. Each source has a different interface for playing music, and you want to provide a unified interface to the player that can work with any source.

In this scenario, you could implement the Adapter pattern by defining a common MediaPlayer interface that the player can use to play music, and adapter classes for each source that convert the source’s interface to the MediaPlayer interface.

Here’s a Java code 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) {
        // do nothing
    }
}

class Mp4Player implements AdvancedMediaPlayer {
    public void playVlc(String fileName) {
        // do nothing
    }
    
    public void playMp4(String fileName) {
        System.out.println("Playing mp4 file. Name: " + fileName);
    }
}

class MediaAdapter implements MediaPlayer {
    AdvancedMediaPlayer advancedMusicPlayer;
 
    public MediaAdapter(String audioType){
        if (audioType.equalsIgnoreCase("vlc") ){
            advancedMusicPlayer = new VlcPlayer();           
        } else if (audioType.equalsIgnoreCase("mp4")){
            advancedMusicPlayer = new Mp4Player();
        }  
    }
 
    public void play(String audioType, String fileName) {
        if (audioType.equalsIgnoreCase("vlc")){
            advancedMusicPlayer.playVlc(fileName);
        } else if(audioType.equalsIgnoreCase("mp4")){
            advancedMusicPlayer.playMp4(fileName);
        }
    }
}

class AudioPlayer implements MediaPlayer {
    MediaAdapter mediaAdapter; 
 
    public void play(String audioType, String fileName) {
        if(audioType.equalsIgnoreCase("mp3")){
            System.out.println("Playing mp3 file. Name: " + fileName);           
        } else if(audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")){
            mediaAdapter = new MediaAdapter(audioType);
            mediaAdapter.play(audioType, fileName);
        } else {
            System.out.println("Invalid media. " + audioType + " format not supported");
        }
    }
}

public class MusicPlayer {
    public static void main(String[] args) {
        AudioPlayer audioPlayer = new AudioPlayer();

        audioPlayer.play("mp3", "beyond_the_horizon.mp3");
        audioPlayer.play("mp4", "alone.mp4");
        audioPlayer.play("vlc", "far_far_away.vlc");
        audioPlayer.play("avi", "mind_me.avi");
    }
}

In this example, we have a MediaPlayer interface that represents the interface expected by the client. We also have an AdvancedMediaPlayer interface that represents the interface of the adaptee. We have two concrete classes that implement the `AdvancedMediaPlayer

2.10. What are some common anti-patterns, and how can they be avoided in software development?

Anti-patterns are common mistakes or bad practices in software development that can lead to inefficient code, maintenance problems, or even project failures. Here are some common anti-patterns and how they can be avoided:

  1. Big ball of mud – This anti-pattern refers to a system that has become so complex that it is impossible to maintain or modify. It usually results from a lack of structure or design in the original system.

To avoid this anti-pattern, it’s important to invest time in planning and designing the system before starting to code. Use best practices such as modularization and abstraction to break down the system into manageable parts.

  1. Copy and paste programming – This anti-pattern involves copying and pasting code from one place to another rather than creating reusable code. This can result in code duplication and maintenance problems.

To avoid this anti-pattern, use code reuse techniques such as creating libraries or using inheritance and polymorphism. Also, invest time in refactoring the code to eliminate duplication whenever possible.

  1. God object – This anti-pattern refers to a class that has too many responsibilities and controls too many aspects of the system. This can result in a system that is difficult to maintain and modify.

To avoid this anti-pattern, use the single responsibility principle and separate concerns into smaller, more manageable classes. Use dependency injection to avoid creating classes that depend on too many other classes.

  1. Magic number – This anti-pattern refers to hard-coding numerical values into the code rather than using named constants or enums. This can result in code that is difficult to read and modify.

To avoid this anti-pattern, use named constants or enums for all numerical values. This makes the code more readable and easier to modify in the future.

  1. Spaghetti code – This anti-pattern refers to code that is poorly structured and difficult to follow. It usually results from a lack of planning or design in the original system.

To avoid this anti-pattern, use design patterns and best practices such as modularization and abstraction to break down the system into manageable parts. Invest time in refactoring the code to eliminate complexity and improve readability.

To conclude, it’s important to be aware of these common anti-patterns and to take steps to avoid them in software development. By investing time in planning and design, using best practices, a

3. Wrapping Up

To sum up, design patterns are proven solutions to common software development problems that can help developers write more efficient, maintainable, and scalable code.

By using design patterns, developers can leverage the knowledge and experience of others to solve common problems in a standardized and proven way. This can lead to more robust and maintainable code, better system performance, and increased developer productivity. However, it’s important to remember that design patterns should be used judiciously and only when they are appropriate for the specific problem at hand.

Java Code Geeks

JCGs (Java Code Geeks) is an independent online community focused on creating the ultimate Java to Java developers resource center; targeted at the technical architect, technical team lead (senior developer), project manager and junior developers alike. JCGs serve the Java, SOA, Agile and Telecom communities with daily news written by domain experts, articles, tutorials, reviews, announcements, code snippets and open source projects.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button