Welcome to the Design Patterns Repository! This project provides a hands-on exploration of several essential object-oriented design patterns. Each directory in this repository introduces a distinct design pattern, offering fully implemented classes, interfaces, and methods. Through practical examples, this repository aims to illustrate how design patterns solve common software engineering challenges by improving code reusability, flexibility, and maintainability.
The design patterns covered include:
Each directory includes fully implemented code to demonstrate these patterns in realistic scenarios. Below is a comprehensive breakdown of each directory, detailing the design patterns, core components, and practical applications.
Inheritance
DirectoryAdventure-Game
DirectoryExceptions
DirectoryIterators
DirectoryDecorators
DirectorySingleton-Multiton-Factory
DirectoryObservers
DirectoryModel-View-Controller
DirectoryInheritance
DirectoryPattern: Inheritance Design Pattern
Context: Representing ingredients and portions for sushi recipes.
The Inheritance
directory provides an introductory look at how inheritance organizes shared traits and enables specialized subclasses. The code here simulates a sushi restaurant where ingredients like Avocado, Tuna, and Eel share common properties (e.g., calories, vegetarian status, cost per ounce). The project builds a class hierarchy with ingredients as the base layer, allowing specialized ingredient portions and sushi types to extend these classes.
Components:
Sashimi
, Nigiri
, and Roll
combine ingredients to form distinct sushi dishes.Key Insights: This directory demonstrates the strength of inheritance by organizing shared traits in a superclass and extending it into specific subclasses for different ingredients. The hierarchical structure allows easy reuse of properties and behaviors, keeping the code organized and modular.
Link to Directory: Inheritance
Adventure-Game
DirectoryPattern: Composition & Aggregation Design Patterns
Context: Creating a text-based adventure game.
The Adventure-Game
directory showcases the composition pattern in a modular game design, where objects like Player, Inventory, Position, and Cell interact to form a navigable, treasure-hunting world. Rather than inheriting from a common superclass, each component encapsulates smaller components, forming a more flexible structure that enables independent development of each part.
Components:
Item
, Inventory
, Position
, and Cell
represent the foundational elements of the game world.MapImpl
and GameImpl
aggregate these core components into a coherent game structure.Key Insights: Composition allows for modular and reusable code by assembling objects rather than inheriting properties. This pattern is ideal for games, where components like players, maps, and items must work together flexibly without rigid hierarchical constraints.
Link to Directory: Adventure-Game
Exceptions
DirectoryPattern: Exception Handling
Context: Error handling and robustness in Java applications.
The Exceptions
directory provides structured exercises to practice exception handling, illustrating how to manage and control unexpected behaviors in a program. By using custom exceptions and managing errors with try-catch
blocks, this directory focuses on creating reliable, user-friendly applications that respond gracefully to various error conditions.
Components:
Key Insights: Exception handling patterns enhance code reliability by managing and containing errors, ensuring that unexpected events don’t crash the program. This directory highlights how exceptions can be anticipated, caught, and managed in layered ways to create robust software.
Link to Directory: Exceptions
Iterators
DirectoryPattern: Iterator Design Pattern
Context: Accessing collections of drivers for a ride-sharing app.
The Iterators
directory demonstrates how the iterator design pattern enables controlled, customized access to data. In a ride-sharing application, where the client searches for nearby drivers, the repository includes three custom iterators—each with unique traversal logic, from filtering by proximity to interleaving data from multiple collections.
Components:
ProximityIterator
: Filters drivers based on their proximity to the client.ExpandingProximityIterator
: Iterates through drivers in increasing distance “rings,” prioritizing close drivers.SnakeOrderAcrossPoolsIterator
: Interleaves drivers from multiple pools in a “snake” order to balance selection.Key Insights: The iterator pattern simplifies complex traversal logic by encapsulating it within iterator classes, allowing for flexible, readable, and reusable data access methods without altering the underlying collections.
Link to Directory: Iterators
Decorators
DirectoryPattern: Decorator Design Pattern
Context: Applying visual modifications to digital images.
The Decorators
directory leverages the decorator design pattern to incrementally enhance images with visual effects. Starting with a base image, decorators like SquareDecorator, CircleDecorator, BorderDecorator, and ZoomDecorator apply effects layer-by-layer, allowing the user to customize and compose effects dynamically.
Components:
SolidColorImage
and PictureImage
serve as foundations for decorators.SquareDecorator
: Adds a colored square overlay.CircleDecorator
: Adds a colored circular overlay.BorderDecorator
: Surrounds the image with a solid border.ZoomDecorator
: Scales the image by a specified multiplier.Key Insights: The decorator pattern is particularly effective in scenarios where dynamic, layered modifications are needed. It allows for flexible and modular visual transformations, where each effect builds on the previous one without requiring changes to the base image class.
Link to Directory: Decorators
Singleton-Multiton-Factory
DirectoryPatterns: Singleton, Multiton, and Factory Method Design Patterns
Context: Building a pizza ordering system with standardized ingredients.
The Singleton-Multiton-Factory
directory showcases creational design patterns in a pizza ordering system. By implementing the Singleton and Multiton patterns for standard ingredients and the Factory Method pattern for creating different pizza types, this directory demonstrates how to centralize ingredient management and automate pizza creation.
Components:
Sauce
, Cheese
, and Topping
.IngredientImpl
: Singleton and Multiton classes for ingredient instances.PizzaImpl
: Factory method for creating different pizza types.CheesePizza
, HawaiianPizza
, MeatLoversPizza
, and VeggieSupremePizza
.PizzaFactory
: Centralized factory for creating pizza instances.Key Insights: The Singleton, Multiton, and Factory Method patterns provide a structured approach to managing ingredient instances and creating complex pizza objects. By centralizing ingredient control and automating pizza creation, this directory demonstrates how creational patterns can streamline system design and enhance code maintainability.
Link to Directory: Singleton-Multiton-Factory
Observers
DirectoryPattern: Observer Design Pattern
Context: Implementing a store with events like discounts and promotions.
The Observers
directory explores the observer design pattern in the context of a store that notifies customers of order status changes and promotions. By defining observers like Customer and Receipt that listen for events from the Store, this directory demonstrates how to implement event-driven systems with decoupled components.
Components:
Customer
: Represents a customer placing an order.Store
: Manages customer orders and notifies observers of order status changes.Product
: Represents a pizza product with details like name, price, and ingredients.Receipt
: Contains order details and serves as a receipt for customers.ReceiptItem
: Represents an item in the order receipt.SaleSpawner
: Simulates sales events like discounts and promotions.Key Insights: The observer pattern decouples event producers (subjects) from consumers (observers), allowing for flexible, scalable systems where components can react to changes without direct dependencies. This directory illustrates how to implement event-driven architectures in Java applications.
Link to Directory: Observers
Model-View-Controller
DirectoryPattern: Model-View-Controller (MVC) Design Pattern
Context: Building a simple 2048 game with MVC architecture.
The Model-View-Controller
directory demonstrates the MVC design pattern in a game development context. By separating the game logic (model), user interface (view), and user input (controller), this directory showcases how to create modular, maintainable applications with clear separation of concerns.
Components:
Key Insights: The MVC pattern divides the game into distinct components, making it easier to manage, test, and extend. By separating concerns and defining clear interactions between components, this directory highlights the benefits of using the MVC pattern in software development.
Link to Directory: Model-View-Controller
This repository provides a practical guide to core design patterns, each applied in real-world contexts to solve common software challenges. Here’s a quick recap of each pattern’s application:
Each pattern is implemented with realistic examples and scenarios, illustrating not only how to apply these patterns but also why they matter in building scalable, maintainable, and effective software. Whether you’re looking to improve your understanding of object-oriented programming or seeking examples of design patterns in action, this repository offers a comprehensive, hands-on guide to mastering design patterns in Java.
All subdirectories use Maven as the build tool. To compile and run the code, navigate to the desired directory and use the following commands:
mvn compile
mvn exec:java
mvn test
mvn package
mvn clean
These commands will help you build, test, and run the code in each directory, allowing you to explore the design patterns and their applications in detail.
Alternatively, if you are using an IDE like IntelliJ IDEA or Eclipse, you can import the project as a Maven project and run the code directly from the IDE. Just be sure to configure the project settings to use Maven for building and running the code.
For more in-depth explanations, code snippets, and practical examples of design patterns, check out the individual directories in this repository. Each directory provides detailed insights into the pattern’s application, components, and design considerations, making it a valuable resource for learning and mastering design patterns in Java.
Also, be sure to check out this detailed document (README) in the Design-Patterns-Info
directory to learn more about Design Patterns and their applications in software development: Design-Patterns-Info
Additionally, I also recommend the Gang of Four (GoF)
book, Design Patterns: Elements of Reusable Object-Oriented Software, as an essential reference for understanding design patterns and their applications in software development. The book offers comprehensive insights into the principles, benefits, and implementation strategies of various design patterns, making it a foundational resource for software engineers, architects, and designers.
Design patterns are powerful tools for structuring code, enhancing reusability, and simplifying complex software systems. By exploring the design patterns in this repository, you’ll gain a deeper understanding of how to apply these patterns effectively in your projects, improving code quality, maintainability, and scalability.
Whether you’re a beginner looking to grasp the basics of design patterns or an experienced developer seeking practical examples and insights, this repository offers a structured, hands-on approach to mastering essential design patterns in Java. Dive into the directories, explore the code, and discover how design patterns can transform your software development journey.
This repository is created and maintained by Son Nguyen, a passionate software developer who enjoys crafting clean, efficient code and exploring the intricacies of software design. The examples and explanations in this repository are designed to provide a comprehensive guide to design patterns, offering practical insights and hands-on experience for developers at all levels.
Additionally, resources from the UNC-Chapel Hill Computer Science Department, Prof. Prairie Goodwin at UNC, and the Gang of Four (GoF) book have been instrumental in shaping the content and structure of this repository. Their contributions to software engineering education and design pattern literature have been invaluable in creating this repository. This repository has been deeply influenced and inspired by them.
If you find this repository helpful or have suggestions for improvement, feel free to reach out or contribute to the project. Your feedback and contributions are always welcome and appreciated!
Happy coding and pattern crafting! 🚀🎨