Design Patterns

Tutorial Sheet Week 5 Solutions

  1. Problem Statement: Creating different types of enemies in a game
  1. The problem with the current approach is that it violates the Open/Closed Principle. Whenever a new enemy type needs to be added, the createEnemy method in the EnemyFactory class needs to be modified, which is a violation of the Open/Closed Principle.

  2. The Factory Method design pattern can solve this problem by providing an interface for creating objects (enemies), but allowing subclasses to decide which class to instantiate.

  3. UML Diagram:

  1. Problem Statement: Text formatting in a text editor application
  1. The issue with the current implementation is that it doesn’t support dynamic composition of formatting options. If we need to create a new formatting option that combines existing options, we would need to create a new class for that specific combination.

  2. The Decorator design pattern can solve this problem by allowing dynamic composition of formatting options at runtime.

  3. UML Diagram:

  1. Problem Statement: Complex interactions between subsystems
  1. The problem with the current approach is that the client code is tightly coupled with the implementation details of the subsystems and their interactions. This makes the code difficult to understand, maintain, and extend.

  2. The Facade design pattern can solve this problem by providing a simplified interface that hides the complexity of the subsystem interactions from the client code.

  3. UML Diagram:

  1. Problem Statement: Accessing remote objects or resources
  1. The potential issues with the current approach are:
  • Performance: Accessing remote objects or resources directly can lead to performance issues, especially in distributed systems or over slow networks.
  • Availability: If the remote object or resource is not available, the client code will fail.
  1. The Proxy design pattern can solve this problem by providing a local representation or placeholder for the remote object or resource.

  2. UML Diagram:

  1. Problem Statement: Creating families of related objects
  1. The potential issue with the current approach is that it violates the Open/Closed Principle. Whenever a new product type needs to be added, multiple classes need to be created or modified (e.g., ProductC, ProductCComponent, ProductCAccessory, ProductCDocumentation).

  2. The Abstract Factory design pattern can solve this problem by providing an interface for creating families of related objects without specifying their concrete classes.

  3. UML Diagram:

  1. Problem Statement: Implementing different duck behaviors
  1. The potential issue with the current approach is that it violates the Single Responsibility Principle. The Duck class is responsible for both the quacking and flying behaviors, which can lead to inflexible and rigid code when new behaviors need to be added or modified.

  2. The Strategy design pattern can solve this problem by separating the behavior implementations from the core object (Duck) and allowing them to be easily interchangeable.

  3. UML Diagram:

  1. Problem Statement: Managing weather data sources
  1. The potential issue with the current approach is that it violates the Open/Closed Principle. If we need to add new features like encryption or compression to the data sources, we would need to modify the existing FileDataSource and DatabaseDataSource classes, which can lead to code duplication and increased complexity.

  2. The Decorator design pattern can solve this problem by allowing the addition of new features to the data sources dynamically without modifying their existing code.

  3. UML Diagram:

  1. Problem Statement: Managing user authentication and authorization
  1. The potential issue with the current approach is that it violates the Open/Closed Principle. If we need to add additional security checks or logging mechanisms, we would need to modify the existing BasicAuthenticationService and BasicAuthorizationService classes, which can lead to code duplication and increased complexity.

  2. The Decorator design pattern can solve this problem by allowing the addition of new features to the authentication and authorization services dynamically without modifying their existing code.

  3. UML Diagram:

  1. Problem Statement: Managing different types of notifications
  1. The potential issue with the current approach is that it violates the Open/Closed Principle. If we need to add a new notification channel, we would need to create a new class implementing the NotificationService interface, which can lead to code duplication and increased complexity.

  2. The Strategy design pattern can solve this problem by separating the different notification strategies from the core NotificationService and allowing them to be easily interchangeable.

  3. UML Diagram: