Design Patterns
Med Agenda implements 5 design patterns and follows 3 design principles to ensure maintainable, scalable, and extensible code.Implemented Design Patterns
1. Factory Pattern
Purpose: Encapsulate object creation logic with validation Location:factory/PatientFactory.java
Implementation:
- Centralizes validation logic
- Ensures all Patient objects are created with valid data
- Prevents direct constructor usage that might skip validation
- Makes it easy to modify creation logic in one place
2. Strategy Pattern
Purpose: Define a family of algorithms for doctor search and make them interchangeable Location:strategy/ package
Implementation:
Interface (DoctorSearchStrategy.java):
- SearchDoctorByName.java:
- SearchDoctorByCrm.java: Searches by CRM number
- SearchDoctorByEmail.java: Searches by email address
- SearchDoctorBySpecialty.java: Searches by medical specialty
- Easy to add new search criteria without modifying existing code
- Each strategy is independent and testable
- Client code doesn’t need to know implementation details
- Follows Open/Closed Principle
3. Decorator Pattern
Purpose: Dynamically add behavior (urgency) to consultation creation Location:decorator/ package
Implementation:
Component Interface (ConsultationService.java):
ConsultationServiceImpl.java):
UrgentConsultationDecorator.java):
- Add responsibilities to objects dynamically
- More flexible than static inheritance
- Can stack multiple decorators
- Follows Single Responsibility Principle
4. Facade Pattern
Purpose: Provide a simplified interface for consultation creation with decorator logic Location:facade/ConsultationFacade.java
Implementation:
- Hides the complexity of decorator pattern from controllers
- Provides a simple, unified interface
- Makes the system easier to use and understand
- Reduces coupling between client code and subsystems
5. Memento Pattern (History)
Purpose: Maintain consultation history without exposing implementation details Location:history/ package
Implementation:
Segregated Interfaces:
ConsultationHistoryService.java):
- Encapsulates history management
- Allows retrieval of past consultations
- Maintains patient and doctor consultation history
- Supports audit and tracking requirements
Design Principles Applied
1. Interface Segregation Principle (ISP)
Definition: Clients should not be forced to depend on interfaces they don’t use. Implementation: The history interfaces are segregated by responsibility:- A service that only needs to read history doesn’t depend on creation methods
- Reduces coupling and makes the code more maintainable
- Easier to test and mock specific functionality
2. Single Responsibility Principle (SRP)
Definition: A class should have only one reason to change. Examples:- PatientFactory: Only responsible for creating valid Patient objects
- UrgentConsultationDecorator: Only adds urgency flag
- SearchDoctorByName: Only implements name-based search
- EmailService: Only handles email sending
- PatientValidator: Only validates patient data
- Easier to understand and maintain
- Changes to one responsibility don’t affect others
- Better testability
3. Open/Closed Principle (OCP)
Definition: Software entities should be open for extension but closed for modification. Implementation: Strategy Pattern - Adding new search criteria:Pattern Interactions
The patterns work together to create a flexible system:Best Practices
When to Use Each Pattern
Factory Pattern:- ✅ Complex object creation with validation
- ✅ Need to ensure objects are created consistently
- ✅ Want to hide creation logic from clients
- ✅ Multiple algorithms for the same task
- ✅ Need to switch algorithms at runtime
- ✅ Want to avoid conditional statements
- ✅ Need to add responsibilities dynamically
- ✅ Want to avoid creating many subclasses
- ✅ Behavior combinations are needed
- ✅ Simplify complex subsystems
- ✅ Provide a unified interface
- ✅ Reduce coupling between clients and subsystems
- ✅ Need to capture and restore object state
- ✅ Want to maintain history
- ✅ Implement undo/redo functionality
Testing the Patterns
Factory Pattern Test
Strategy Pattern Test
Decorator Pattern Test
Summary
Med Agenda demonstrates professional software engineering through:- 5 Design Patterns: Factory, Strategy, Decorator, Facade, Memento
- 3 Design Principles: ISP, SRP, OCP
- Clean Architecture: Clear separation of concerns
- Extensibility: Easy to add new features without modifying existing code
- Maintainability: Well-organized, testable code