Design Patterns Project Description
This project demonstrates the application of four fundamental design patterns: Factory, Observer, Singleton, and Decorator. The context revolves around a T-Shirt store offering customizable T-Shirts across different regions (Middle East, Asia, and China) and sizes (Small, Medium, and Large). The project highlights a modular and extensible architecture while maintaining design principles.
- Abstract Class:
TShirtserves as the base class, containing common attributes likenameanddescription. - Concrete Classes: There are nine specific classes extending
TShirt, each representing a combination of size and region, e.g.,AsiaSmallTShirt.
- Abstract Factory:
TShirtStoreacts as the abstract factory, enforcing the implementation of three methods:createSmallTShirt,createMediumTShirt, andcreateLargeTShirt. - Concrete Factories:
MiddleEastTShirtStoreChinaTShirtStoreAsiaTShirtStore
- Factory Method: The
orderTShirtmethod takes the size as input and ensures the appropriate T-Shirt is returned, tailored to the specified region.
- Abstract Decorator:
Featuresclass extendsTShirt, enabling additional customizations. - Concrete Decorators:
LogoTextRedColorBlueColor
- Composition: The
Featuresclass uses composition to wrap around aTShirtobject, allowing dynamic addition of features like colors and text.
- Implementation: The
ShopOwnerclass ensures only a single instance exists by:- Making the constructor private.
- Using a double-checked locking mechanism to manage instance creation.
- Purpose: Centralized control for monitoring sales data and interacting with stores.
- Concrete Subject:
TShirtStorekeeps track of T-Shirt sales. - Concrete Observer:
ShopOwnersubscribes to receive updates on the quantity of T-Shirts sold by size and region. - Interaction: The
ShopOwneris notified whenever a T-Shirt is sold, enabling real-time monitoring.
- Customers can place a single T-Shirt order at a time. Subsequent orders are rejected unless the previous transaction is completed.
- Initialize ShopOwner: Access the single instance of
ShopOwner. - Create a T-Shirt Store: Instantiate a store for the desired region (Middle East, Asia, or China).
- Create a Customer: Instantiate a customer and allow them to place an order.
- Place an Order:
- Specify the region, size, and additional customizations (color, logo, text).
- The system processes the order and notifies the
ShopOwnerof the sale.
This project demonstrates a robust implementation of design patterns to achieve a flexible, reusable, and scalable architecture. It provides a practical showcase of real-world use cases for these patterns in a domain-specific context.