A comprehensive collection of design patterns implemented in Kotlin, demonstrating modern software architecture principles and best practices.
Creational patterns provide various object creation mechanisms, increasing flexibility and reuse of existing code.
File: BuilderPattern.kt
Description: Immutable object construction with readable API. Allows step-by-step construction of complex objects while keeping them immutable and ensuring proper validation.
File: FactoryMethod.kt
Description: Subclass decides product creation. Provides an interface for creating objects but lets subclasses alter the type of objects that will be created.
File: AbstractFactory.kt
Description: Factory of factories for related product families. Creates families of related objects without specifying their concrete classes.
File: SimpleOrStaticFactory.kt
Description: Static method to create products based on type. Encapsulates object creation logic in a single static method for simple use cases.
File: PrototypeUsingDataClass.kt
Description: Clone objects using Kotlin data class copy(). Creates new objects by copying existing instances, leveraging Kotlin's built-in copy functionality.
Files:
SingletonUsingObject.kt- Kotlin object singletonSingletonDoubleChecked.kt- Double-checked locking singleton
Description: Ensures a class has only one instance and provides global access to it. Two implementations: thread-safe Kotlin object and traditional double-checked locking.
Structural patterns explain how to assemble objects and classes into larger structures while keeping the structures flexible and efficient.
File: Adapter.kt
Description: Bridge incompatible interfaces. Allows objects with incompatible interfaces to work together by creating a wrapper that translates one interface to another.
File: Decorator.kt
Description: Add features to objects without altering core functionality. Dynamically adds new behaviors to objects by wrapping them in decorator classes.
File: Facade.kt
Description: Hide complexity behind a simple interface. Provides a simplified interface to a complex subsystem, making it easier to use.
Behavioral patterns focus on communication between objects and the assignment of responsibilities between them.
File: Command.kt
Description: Encapsulate actions as objects. Turns requests into stand-alone objects containing all information about the request, enabling parameterization, queuing, and undo operations.
File: Observer.kt
Description: Pub-sub mechanism for state change notifications. Defines a one-to-many dependency between objects so that when one object changes state, all dependents are notified automatically.
File: StrategyPattern.kt
Description: Interchangeable algorithms at runtime. Defines a family of algorithms, encapsulates each one, and makes them interchangeable without changing client code.
| Category | Count | Purpose |
|---|---|---|
| Creational | 6 | Object creation mechanisms |
| Structural | 3 | Object composition and relationships |
| Behavioral | 3 | Communication patterns and responsibilities |