Design Patterns


public class EnemyFactory {
    public Enemy createEnemy(String type) {
        if (type.equals("Alien")) {
            return new Alien();
        } else if (type.equals("Robot")) {
            return new Robot();
        }
        // More cases...
        return null;
    }
}
  1. Identify the problem with the current approach.

  2. Suggest a design pattern that can solve this problem.

  3. Provide a high-level UML diagram or pseudocode to illustrate your solution.

public interface TextComponent {
    void render();
}

public class PlainText implements TextComponent {
    private String text;

    public PlainText(String text) {
        this.text = text;
    }

    @Override
    public void render() {
        System.out.println(text);
    }
}
  1. Identify the issue with the current implementation.

  2. Suggest a design pattern that can solve this problem.

  3. Provide a high-level UML diagram or pseudocode to illustrate your solution.


// Client code
public class Client {
    public void doSomething() {
        SubsystemA subsystemA = new SubsystemA();
        SubsystemB subsystemB = new SubsystemB();
        SubsystemC subsystemC = new SubsystemC();

        // Complex interactions between subsystems
        subsystemA.operation1();
        subsystemB.operation2(subsystemA.getResult());
        subsystemC.operation3(subsystemA.getResult(), subsystemB.getResult());
    }
}
  1. Identify the problem with the current approach.

  2. Suggest a design pattern that can solve this problem.

  3. Provide a high-level UML diagram or pseudocode to illustrate your solution.


// Remote object interface
public interface RemoteObject {
    void performOperation();
}

// Client code
public class Client {
    public void useRemoteObject(RemoteObject remoteObject) {
        // Code to use the remote object directly
        remoteObject.performOperation();
    }
}
  1. Identify the potential issues with the current approach.

  2. Suggest a design pattern that can solve this problem.

  3. Provide a high-level UML diagram or pseudocode to illustrate your solution.


// Product interfaces
public interface Product { /* ... */ }
public interface Component { /* ... */ }
public interface Accessory { /* ... */ }
public interface Documentation { /* ... */ }

// Concrete product classes
public class ProductA implements Product { /* ... */ }
public class ProductAComponent implements Component { /* ... */ }
public class ProductAAccessory implements Accessory { /* ... */ }
public class ProductADocumentation implements Documentation { /* ... */ }

// Similar classes for ProductB, ProductC, etc.
  1. Identify the potential issue with the current approach.

  2. Suggest a design pattern that can solve this problem.

  3. Provide a high-level UML diagram or pseudocode to illustrate your solution.

public abstract class Duck {
    public abstract void quack();
    public abstract void fly();
}

public class MallardDuck extends Duck {
    @Override
    public void quack() {
        System.out.println("Quack");
    }

    @Override
    public void fly() {
        System.out.println("Flying");
    }
}

public class RubberDuck extends Duck {
    @Override
    public void quack() {
        System.out.println("Squeak");
    }

    @Override
    public void fly() {
        // Rubber ducks cannot fly
        throw new UnsupportedOperationException();
    }
}
  1. Identify the potential issue with the current approach.
  2. Suggest a design pattern that can solve this problem.
  3. Provide a high-level UML diagram or pseudocode to illustrate your solution.

public interface DataSource {
    void writeData(String data);
    String readData();
}

public class FileDataSource implements DataSource {
    private String fileName;

    public FileDataSource(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void writeData(String data) {
        // Write data to file
    }

    @Override
    public String readData() {
        // Read data from file
        return "";
    }
}

public class DatabaseDataSource implements DataSource {
    private String connectionString;

    public DatabaseDataSource(String connectionString) {
        this.connectionString = connectionString;
    }

    @Override
    public void writeData(String data) {
        // Write data to database
    }

    @Override
    public String readData() {
        // Read data from database
        return "";
    }
}
  1. Identify the potential issue with the current approach.

  2. Suggest a design pattern that can solve this problem.

  3. Provide a high-level UML diagram or pseudocode to illustrate your solution.


public interface AuthenticationService {
    boolean authenticate(String username, String password);
}

public class BasicAuthenticationService implements AuthenticationService {
    @Override
    public boolean authenticate(String username, String password) {
        // Perform authentication logic
        return true;
    }
}

public interface AuthorizationService {
    boolean isAuthorized(String username, String resource);
}

public class BasicAuthorizationService implements AuthorizationService {
    @Override
    public boolean isAuthorized(String username, String resource) {
        // Perform authorization logic
        return true;
    }
}
  1. Identify the potential issue with the current approach.

  2. Suggest a design pattern that can solve this problem.

  3. Provide a high-level UML diagram or pseudocode to illustrate your solution.

public interface NotificationService {
    void sendNotification(String message);
}

public class EmailNotificationService implements NotificationService {
    @Override
    public void sendNotification(String message) {
        // Send email notification
    }
}

public class SMSNotificationService implements NotificationService {
    @Override
    public void sendNotification(String message) {
        // Send SMS notification
    }
}
  1. Identify the potential issue with the current approach.

  2. Suggest a design pattern that can solve this problem.

  3. Provide a high-level UML diagram or pseudocode to illustrate your solution