Wednesday, November 29, 2023

Mastering Design Pattern : Navigating Common Pitfalls with & without Design Pattern Code Solutions - Part 3🛡️

  • In the dynamic realm of software development, mastering design patterns is paramount for crafting robust, scalable, and maintainable solutions. While design patterns provide reusable and proven solutions to recurring problems, their effective implementation requires a nuanced understanding to steer clear of potential pitfalls. 
  • Design patterns encapsulate best practices, offering elegant and proven solutions to recurring problems. Whether it's the structural elegance of the Singleton pattern, the flexibility of the Strategy pattern, or the composability of the Composite pattern, each design pattern addresses specific concerns within the software development lifecycle.
  • This post delves into the art of navigating these challenges, both with and without the aid of design pattern code solutions.

Example 1: Observer  Design Pattern- Logging

  • Problem: Directly using System.out.println for logging makes it hard to manage, test, and replace with a different logging framework.
  • Without Design Pattern Code Example:
  • // Good code: Observer pattern for logging import java.util.ArrayList; import java.util.List; public class Logger { private List<LogListener> listeners = new ArrayList<>(); public void addListener(LogListener listener) { listeners.add(listener); } public void log(String message) { for (LogListener listener : listeners) { listener.onLogMessage(message); } } } public interface LogListener { void onLogMessage(String message); } public class UserService { private Logger logger; public UserService(Logger logger) { this.logger = logger; } public void createUser(String username) { // Business logic for creating a user // Logging using observer pattern logger.log("User created: " + username); } } 
  • With Design Pattern  Code Example:
  • // Good code: Observer pattern for logging import java.util.ArrayList; import java.util.List; public class Logger { private List<LogListener> listeners = new ArrayList<>(); public void addListener(LogListener listener) { listeners.add(listener); } public void log(String message) { for (LogListener listener : listeners) { listener.onLogMessage(message); } } } public interface LogListener { void onLogMessage(String message); } public class UserService { private Logger logger; public UserService(Logger logger) { this.logger = logger; } public void createUser(String username) { // Business logic for creating a user // Logging using observer pattern logger.log("User created: " + username); } }
  • The Observer pattern allows for a flexible and extensible logging system. New log listeners can be added without modifying the UserService class, making it easier to manage and test.

Example 2: Builder Design Pattern - Configuration

  • Problem: Configuration parameters are hardcoded within the class, making it challenging to change configurations dynamically or manage them consistently.
  • Without Design Pattern Code Example:
  • // Bad code: Without design pattern (Configuration) public class DatabaseConnection { private String url; private String username; private String password; public DatabaseConnection(String url, String username, String password) { this.url = url; this.username = username; this.password = password; } public void connect() { // Connect to the database using configuration parameters // ... } }
  • With Design Pattern  Code Example:
  • // Good code: Builder pattern for configuration public class DatabaseConnection { private String url; private String username; private String password; private DatabaseConnection(Builder builder) { this.url = builder.url; this.username = builder.username; this.password = builder.password; } public void connect() { // Connect to the database using configuration parameters // ... } public static class Builder { private String url; private String username; private String password; public Builder(String url) { this.url = url; } public Builder setUsername(String username) { this.username = username; return this; } public Builder setPassword(String password) { this.password = password; return this; } public DatabaseConnection build() { return new DatabaseConnection(this); } } }
  • The Builder pattern provides a fluent interface for constructing objects with optional parameters, making it easy to create and manage different configurations for the DatabaseConnection class..

Example 3: Strategy Design Pattern - File Reader

  • Problem: The file reading logic is tightly coupled with the MyFileReader class, making it difficult to extend or replace the file reading implementation.
  • Without Design Pattern Code Example:
  • // Bad code: Lack of service discovery in microservices // Bad code: Without design pattern (File Reader) import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class MyFileReader { public String readFile(String filePath) { StringBuilder content = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { String line; while ((line = reader.readLine()) != null) { content.append(line).append("\n"); } } catch (IOException e) { e.printStackTrace(); } return content.toString(); } }
  • With Design Pattern  Code Example:
  • // Good code: Strategy pattern for file reading import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public interface FileReadingStrategy { String readFile(String filePath) throws IOException; } public class MyFileReader { private FileReadingStrategy fileReadingStrategy; public MyFileReader(FileReadingStrategy fileReadingStrategy) { this.fileReadingStrategy = fileReadingStrategy; } public String readFile(String filePath) throws IOException { return fileReadingStrategy.readFile(filePath); } } public class DefaultFileReadingStrategy implements FileReadingStrategy { @Override public String readFile(String filePath) throws IOException { StringBuilder content = new StringBuilder(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { String line; while ((line = reader.readLine()) != null) { content.append(line).append("\n"); } } return content.toString(); } }
  • The Strategy pattern allows the MyFileReader class to use different file reading strategies. This makes it easy to extend or replace the file reading implementation without modifying the existing code.

Example 4: Repository Design Pattern - Database Access

  • Problem: The database access logic is tightly coupled with the DatabaseService class, making it challenging to change the database or handle different database operations.
  • Without Design Pattern Code Example:
  • // Bad code: Without design pattern (Database Access) import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DatabaseService { public String getUserData(String username) { String query = "SELECT * FROM users WHERE username = ?"; try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "password"); PreparedStatement preparedStatement = connection.prepareStatement(query)) { preparedStatement.setString(1, username); ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { return resultSet.getString("data"); } } catch (SQLException e) { e.printStackTrace(); } return null; } }
  • With Design Pattern  Code Example
  • // Good code: Repository pattern for database access import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public interface UserRepository { String getUserData(String username) throws SQLException; } public class DatabaseService implements UserRepository { private ConnectionProvider connectionProvider; public DatabaseService(ConnectionProvider connectionProvider) { this.connectionProvider = connectionProvider; } @Override public String getUserData(String username) throws SQLException { String query = "SELECT * FROM users WHERE username = ?"; try (Connection connection = connectionProvider.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(query)) { preparedStatement.setString(1, username); ResultSet resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { return resultSet.getString("data"); } } return null; } } public interface ConnectionProvider { Connection getConnection() throws SQLException; } public class MySqlConnectionProvider implements ConnectionProvider { private static final String URL = "jdbc:mysql://localhost:3306/mydb"; private static final String USER = "user"; private static final String PASSWORD = "password"; @Override public Connection getConnection() throws SQLException { return DriverManager.getConnection(URL, USER, PASSWORD); } }
  • The Repository pattern separates the database access logic into a UserRepository interface, making it easier to switch between different database implementations or handle more complex database operations. The ConnectionProvider interface provides flexibility in managing database connections.

Example 5: Adapter Design Pattern - HTTP Client

  • Problem: The HTTP client logic is tightly coupled with the MyHttpClient class, making it challenging to switch to a different HTTP client library or handle various HTTP methods.
  • Without Design Pattern Code Example:
  • // Bad code: Without design pattern (HTTP Client) import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class MyHttpClient { public String fetchData(String url) { try { URL apiUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection(); connection.setRequestMethod("GET"); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); return response.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } }
  • With Design Pattern  Code Example 
  • // Good code: Adapter pattern for HTTP client import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public interface HttpClient { String get(String url); } public class MyHttpClient implements HttpClient { @Override public String get(String url) { try { URL apiUrl = new URL(url); HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection(); connection.setRequestMethod("GET"); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); return response.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } } public class HttpClientAdapter implements HttpClient { private ThirdPartyHttpClient thirdPartyHttpClient; public HttpClientAdapter(ThirdPartyHttpClient thirdPartyHttpClient) { this.thirdPartyHttpClient = thirdPartyHttpClient; } @Override public String get(String url) { return thirdPartyHttpClient.performGetRequest(url); } } public class ThirdPartyHttpClient { public String performGetRequest(String url) { // Implementation of third-party HTTP client logic return "Response from third-party HTTP client"; } }
  • The Adapter pattern allows the MyHttpClient class to adapt to a different HTTP client (ThirdPartyHttpClient) without modifying its code. This makes it easy to switch to a new HTTP client implementation.

Example 6:Strategy Design Pattern - HTTP Client

  • Problem: The email sending logic is tightly coupled with the EmailService class, making it challenging to switch to a different email provider or add support for multiple providers.
  • Without Design Pattern Code Example:
  • // Bad code: Without design pattern (Email Service) public class EmailService { public void sendEmail(String to, String subject, String body) { // Implementation of sending an email using a specific email provider // ... } }
  • With Design Pattern  Code Example:
  • // Good code: Strategy pattern for email service public interface EmailProvider { void sendEmail(String to, String subject, String body); } public class DefaultEmailProvider implements EmailProvider { @Override public void sendEmail(String to, String subject, String body) { // Implementation of sending an email using the default email provider // ... } } public class ThirdPartyEmailProvider implements EmailProvider { @Override public void sendEmail(String to, String subject, String body) { // Implementation of sending an email using a third-party email provider // ... } } public class EmailService { private EmailProvider emailProvider; public EmailService(EmailProvider emailProvider) { this.emailProvider = emailProvider; } public void sendEmail(String to, String subject, String body) { emailProvider.sendEmail(to, subject, body); } }
  • The Strategy pattern allows the EmailService class to use different email providers without modifying its code. New email providers can be added without changing the existing code, providing flexibility and extensibility.

Example 7:Command Design Pattern - Task Execution

  • ProblemThe task execution logic is tightly coupled with the TaskExecutor class, making it challenging to support different task types or switch to a different task execution mechanism.
  • Without Design Pattern Code Example:
  • // Bad code: Without design pattern (Task Execution) public class TaskExecutor { public void executeTask(String taskType) { // Implementation of executing a task based on the task type // ... } }
  • With Design Pattern  Code Example:
  • // Good code: Command pattern for task execution public interface Task { void execute(); } public class SimpleTask implements Task { @Override public void execute() { // Implementation of executing a simple task // ... } } public class ComplexTask implements Task { @Override public void execute() { // Implementation of executing a complex task // ... } } public class TaskExecutor { private Task task; public TaskExecutor(Task task) { this.task = task; } public void executeTask() { task.execute(); } }
  • The Command pattern allows the TaskExecutor class to support different tasks without modifying its code. New tasks can be added without changing the existing code, providing flexibility and extensibility.

Example 8:Proxy Design Pattern - Cache Management

  • Problem: The cache management logic is tightly coupled with the CacheManager class, making it challenging to implement different caching strategies or switch to a different caching library.
  • Without Design Pattern Code Example
  • // Bad code: Without design pattern (Cache Management) import java.util.HashMap; import java.util.Map; public class CacheManager { private Map<String, Object> cache = new HashMap<>(); public void addToCache(String key, Object value) { // Implementation of adding to the cache cache.put(key, value); } public Object getFromCache(String key) { // Implementation of getting from the cache return cache.get(key); } }
  • With Design Pattern  Code Example:
  • // Good code: Proxy pattern for cache management public interface DataProvider { Object fetchData(String key); } public class RealDataProvider implements DataProvider { @Override public Object fetchData(String key) { // Implementation of fetching data from the actual source // ... return null; } } public class CachedDataProviderProxy implements DataProvider { private RealDataProvider realDataProvider; private Map<String, Object> cache = new HashMap<>(); public CachedDataProviderProxy(RealDataProvider realDataProvider) { this.realDataProvider = realDataProvider; } @Override public Object fetchData(String key) { // Check if data is in the cache if (cache.containsKey(key)) { System.out.println("Data found in cache"); return cache.get(key); } else { // Fetch data from the real data provider Object data = realDataProvider.fetchData(key); // Add data to the cache cache.put(key, data); System.out.println("Data added to cache"); return data; } } }
  • The Proxy pattern allows the CachedDataProviderProxy class to manage caching without modifying the RealDataProvider class. This makes it easy to switch caching strategies or use different cache implementations.

Example 9: Observer Design Pattern - Notification System

  • Problem: The notification system logic is tightly coupled with the NotificationSystem class, making it challenging to support different notification methods or switch to a different notification mechanism.
  • Without Design Pattern Code Example:
  • // Bad code: Without design pattern (Notification System) public class NotificationSystem { public void sendNotification(String message, String recipient) { // Implementation of sending a notification // ... } }
  • With Design Pattern  Code Example:
  • // Good code: Observer pattern for notification system import java.util.ArrayList; import java.util.List; public class NotificationSystem { private List<NotificationListener> listeners = new ArrayList<>(); public void addListener(NotificationListener listener) { listeners.add(listener); } public void sendNotification(String message, String recipient) { for (NotificationListener listener : listeners) { listener.onNotification(message, recipient); } } } public interface NotificationListener { void onNotification(String message, String recipient); } public class EmailNotificationListener implements NotificationListener { @Override public void onNotification(String message, String recipient) { // Implementation of sending an email notification // ... } } public class SMSNotificationListener implements NotificationListener { @Override public void onNotification(String message, String recipient) { // Implementation of sending an SMS notification // ... } }
  • The Observer pattern allows the NotificationSystem class to send notifications without being tied to specific notification methods. New notification listeners can be added without changing the existing code, providing flexibility and extensibility.

Example 10 :Composite Design Pattern -   User Interface

  • Problem: The user interface logic is tightly coupled with the UserInterface class, making it challenging to support different UI elements or switch to a different UI framework.
  • Without Design Pattern Code Example:
  • // Bad code: Without design pattern (User Interface) public class UserInterface { public void display(String content) { // Implementation of displaying content in the user interface // ... } }
  • With Design Pattern  Code Example:
  • // Good code: Composite pattern for user interface import java.util.ArrayList; import java.util.List; public interface UIComponent { void display(); } public class TextComponent implements UIComponent { private String content; public TextComponent(String content) { this.content = content; } @Override public void display() { // Implementation of displaying text content in the UI // ... } } public class ContainerComponent implements UIComponent { private List<UIComponent> components = new ArrayList<>(); public void addComponent(UIComponent component) { components.add(component); } @Override public void display() { for (UIComponent component : components) { component.display(); } } } public class UserInterface { private UIComponent rootComponent; public UserInterface(UIComponent rootComponent) { this.rootComponent = rootComponent; } public void display() { rootComponent.display(); } }
  • The Composite pattern allows the UserInterface class to represent complex UI structures without being tied to specific UI elements. New UI components can be added without changing the existing code, providing flexibility and extensibility

Example 11: Chain of Responsibility Design Pattern -  Data Validation

  • Problem: The data validation logic is tightly coupled with the DataValidator class, making it challenging to support different validation rules or switch to a different validation mechanism.
  • Without Design Pattern Code Example:
  • // Bad code: Without design pattern (Data Validation) public class DataValidator { public boolean validateData(String data) { // Implementation of data validation logic // ... return false; } }
  • With Design Pattern  Code Example:
  • // Good code: Chain of Responsibility pattern for data validation public interface DataValidator { boolean validate(String data); } public class LengthValidator implements DataValidator { private int minLength; public LengthValidator(int minLength) { this.minLength = minLength; } @Override public boolean validate(String data) { // Implementation of length validation // ... return data.length() >= minLength; } } public class FormatValidator implements DataValidator { private String format; public FormatValidator(String format) { this.format = format; } @Override public boolean validate(String data) { // Implementation of format validation // ... return data.matches(format); } } public class DataValidationHandler { private List<DataValidator> validators = new ArrayList<>(); public void addValidator(DataValidator validator) { validators.add(validator); } public boolean validateData(String data) { for (DataValidator validator : validators) { if (!validator.validate(data)) { return false; } } return true; } }
  • The Chain of Responsibility pattern allows the DataValidationHandler class to validate data using a chain of validators without modifying its code. New validation rules can be added without changing the existing code, providing flexibility and extensibility

You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS