mirror of
https://github.com/rjNemo/design-patterns
synced 2026-06-06 02:26:40 +00:00
doc: create and document packages
This commit is contained in:
parent
d72ec61f5d
commit
d99ed01f65
47 changed files with 109 additions and 52 deletions
|
|
@ -23,8 +23,8 @@ and divided into three groups.
|
|||
|
||||
### [Creational Patterns](creational/README.md)
|
||||
|
||||
- [Factory Method](creational/factory-method/README.md)
|
||||
- [Abstract Factory](creational/abstract-factory/README.md)
|
||||
- [Factory Method](creational/factory_method/README.md)
|
||||
- [Abstract Factory](creational/abstract_factory/README.md)
|
||||
- [Builder](creational/builder/README.md)
|
||||
- [Prototype](creational/prototype/README.md)
|
||||
- [Singleton](creational/singleton/README.md)
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
|
||||
|
||||
- [Factory Method](factory-method/README.md)
|
||||
- [Abstract Factory](abstract-factory/README.md)
|
||||
- [Factory Method](factory_method/README.md)
|
||||
- [Abstract Factory](abstract_factory/README.md)
|
||||
- [Builder](builder/README.md)
|
||||
- [Prototype](prototype/README.md)
|
||||
- [Singleton](singleton/README.md)
|
||||
|
|
|
|||
4
creational/__init__.py
Normal file
4
creational/__init__.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
"""
|
||||
Creational patterns provide various object creation mechanisms, which increase
|
||||
flexibility and reuse of existing code.
|
||||
"""
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
from abc import ABC, abstractmethod
|
||||
|
||||
from AbstractProductA import AbstractProductA
|
||||
from AbstractProductB import AbstractProductB
|
||||
from creational.abstract_factory.AbstractProductA import AbstractProductA
|
||||
from creational.abstract_factory.AbstractProductB import AbstractProductB
|
||||
|
||||
|
||||
class AbstractFactory(ABC):
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
from abc import ABC, abstractmethod
|
||||
|
||||
from AbstractProductA import AbstractProductA
|
||||
from creational.abstract_factory.AbstractProductA import AbstractProductA
|
||||
|
||||
|
||||
class AbstractProductB(ABC):
|
||||
4
creational/abstract_factory/__init__.py
Normal file
4
creational/abstract_factory/__init__.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
"""
|
||||
Lets you produce families of related objects without specifying their concrete
|
||||
classes.
|
||||
"""
|
||||
|
|
@ -1,10 +1,6 @@
|
|||
from AbstractFactory import AbstractFactory
|
||||
from products import (
|
||||
ConcreteProductA1,
|
||||
ConcreteProductA2,
|
||||
ConcreteProductB1,
|
||||
ConcreteProductB2,
|
||||
)
|
||||
from creational.abstract_factory.AbstractFactory import AbstractFactory
|
||||
from creational.abstract_factory.products import ConcreteProductA1, \
|
||||
ConcreteProductB1, ConcreteProductA2, ConcreteProductB2
|
||||
|
||||
|
||||
class ConcreteFactory1(AbstractFactory):
|
||||
|
|
@ -1,7 +1,8 @@
|
|||
"""The client code can work with any concrete factory class."""
|
||||
|
||||
from AbstractFactory import AbstractFactory
|
||||
from factories import ConcreteFactory1, ConcreteFactory2
|
||||
from creational.abstract_factory.AbstractFactory import AbstractFactory
|
||||
from creational.abstract_factory.factories import ConcreteFactory1, \
|
||||
ConcreteFactory2
|
||||
|
||||
|
||||
def client_code(factory: AbstractFactory) -> None:
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
"""Concrete Products are created by corresponding Concrete Factories."""
|
||||
|
||||
from AbstractProductA import AbstractProductA
|
||||
from AbstractProductB import AbstractProductB
|
||||
from creational.abstract_factory.AbstractProductA import AbstractProductA
|
||||
from creational.abstract_factory.AbstractProductB import AbstractProductB
|
||||
|
||||
|
||||
class ConcreteProductA1(AbstractProductA):
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
from Builder import Builder
|
||||
from creational.builder.Builder import Builder
|
||||
|
||||
|
||||
class Director:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
"""
|
||||
Lets you construct complex objects step by step. The pattern allows you to
|
||||
produce different types and representations of an object using the same
|
||||
construction code.
|
||||
"""
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
from Builder import Builder
|
||||
from products import Product1
|
||||
from creational.builder.Builder import Builder
|
||||
from creational.builder.products import Product1
|
||||
|
||||
|
||||
class ConcreteBuilder1(Builder):
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ initiates the construction process. The end result is retrieved from the
|
|||
builder object.
|
||||
"""
|
||||
|
||||
from builders import ConcreteBuilder1
|
||||
from Director import Director
|
||||
from creational.builder.Director import Director
|
||||
from creational.builder.builders import ConcreteBuilder1
|
||||
|
||||
director = Director()
|
||||
builder = ConcreteBuilder1()
|
||||
|
|
|
|||
4
creational/factory_method/__init__.py
Normal file
4
creational/factory_method/__init__.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
"""
|
||||
Provides an interface for creating objects in a superclass, but allows
|
||||
subclasses to alter the type of objects that will be created.
|
||||
"""
|
||||
|
|
@ -3,10 +3,9 @@ Concrete Creators override the factory method in order to change the resulting
|
|||
product's type.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ICreator import ICreator
|
||||
from products import ConcreteProduct1, ConcreteProduct2
|
||||
from creational.factory_method.ICreator import ICreator
|
||||
from creational.factory_method.products import ConcreteProduct1, \
|
||||
ConcreteProduct2
|
||||
|
||||
|
||||
class ConcreteCreator1(ICreator):
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
from creators import ConcreteCreator1, ConcreteCreator2
|
||||
from ICreator import ICreator
|
||||
from creational.factory_method.ICreator import ICreator
|
||||
from creational.factory_method.creators import ConcreteCreator1, \
|
||||
ConcreteCreator2
|
||||
|
||||
|
||||
def client_code(creator: ICreator) -> None:
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
Concrete Products provide various implementations of the Product interface.
|
||||
"""
|
||||
|
||||
from IProduct import IProduct
|
||||
from creational.factory_method.IProduct import IProduct
|
||||
|
||||
|
||||
class ConcreteProduct1(IProduct):
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
"""
|
||||
Lets you copy existing objects without making your code dependent on their
|
||||
classes.
|
||||
"""
|
||||
|
|
@ -39,7 +39,8 @@ class SomeComponent:
|
|||
|
||||
# Then, let's clone the object itself, using the prepared clones of the
|
||||
# nested objects.
|
||||
new = self.__class__(self.some_int, some_list_of_objects, some_circular_ref)
|
||||
new = self.__class__(
|
||||
self.some_int, some_list_of_objects, some_circular_ref)
|
||||
new.__dict__.update(self.__dict__)
|
||||
|
||||
return new
|
||||
|
|
@ -60,10 +61,10 @@ class SomeComponent:
|
|||
# First, let's create copies of the nested objects.
|
||||
some_list_of_objects = copy.deepcopy(self.some_list_of_objects, memo)
|
||||
some_circular_ref = copy.deepcopy(self.some_circular_ref, memo)
|
||||
|
||||
# Then, let's clone the object itself, using the prepared clones of the
|
||||
# nested objects.
|
||||
new = self.__class__(self.some_int, some_list_of_objects, some_circular_ref)
|
||||
new = self.__class__(
|
||||
self.some_int, some_list_of_objects, some_circular_ref)
|
||||
|
||||
new.__dict__ = copy.deepcopy(self.__dict__, memo)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import copy
|
||||
|
||||
from components import SelfReferencingEntity, SomeComponent
|
||||
from creational.prototype.components import SelfReferencingEntity, SomeComponent
|
||||
|
||||
list_of_objects = [1, {1, 2, 3}, [1, 2, 3]]
|
||||
circular_ref = SelfReferencingEntity()
|
||||
|
|
|
|||
4
creational/singleton/__init__.py
Normal file
4
creational/singleton/__init__.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
"""
|
||||
Lets you ensure that a class has only one instance, while providing a global
|
||||
access point to this instance.
|
||||
"""
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
"""
|
||||
Structural patterns explain how to assemble objects and classes into larger
|
||||
structures while keeping these structures flexible and efficient.
|
||||
"""
|
||||
1
structural/adapter/__init__.py
Normal file
1
structural/adapter/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
"""Allows objects with incompatible interfaces to collaborate."""
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
"""
|
||||
Lets you split a large class or a set of closely related classes into two
|
||||
separate hierarchies—abstraction and implementation—which can be developed
|
||||
independently of each other.
|
||||
"""
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
from dataclasses import dataclass
|
||||
|
||||
from IImplementation import Implementation
|
||||
from structural.bridge.IImplementation import Implementation
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
Each Concrete Implementation corresponds to a specific platform and implements
|
||||
the Implementation interface using that platform's API.
|
||||
"""
|
||||
from IImplementation import Implementation
|
||||
from structural.bridge.IImplementation import Implementation
|
||||
|
||||
|
||||
class ConcreteImplementationA(Implementation):
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
The client code should be able to work with any pre-configured abstraction-
|
||||
implementation combination.
|
||||
"""
|
||||
from abstractions import Abstraction
|
||||
from IImplementation import Implementation
|
||||
from implementations import ConcreteImplementationA, ConcreteImplementationB
|
||||
from structural.bridge.abstractions import Abstraction
|
||||
from structural.bridge.implementations import ConcreteImplementationA, \
|
||||
ConcreteImplementationB
|
||||
|
||||
|
||||
def client_code(abstraction: Abstraction) -> None:
|
||||
|
|
@ -23,7 +23,6 @@ abstraction = Abstraction(implementation)
|
|||
|
||||
client_code(abstraction)
|
||||
|
||||
|
||||
print("\n")
|
||||
|
||||
implementation = ConcreteImplementationB()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
"""
|
||||
Lets you compose objects into tree structures and then work with these
|
||||
structures as if they were individual objects.
|
||||
"""
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
from dataclasses import dataclass, field
|
||||
from typing import List
|
||||
|
||||
from component import Component
|
||||
from structural.composite.component import Component
|
||||
|
||||
|
||||
@dataclass
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from component import Component
|
||||
from structural.composite.component import Component
|
||||
|
||||
|
||||
class Leaf(Component):
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
from component import Component
|
||||
from composite import Composite
|
||||
from leaf import Leaf
|
||||
from structural.composite.component import Component
|
||||
from structural.composite.composite import Composite
|
||||
from structural.composite.leaf import Leaf
|
||||
|
||||
|
||||
def client_code(component: Component) -> None:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
"""
|
||||
Lets you attach new behaviors to objects by placing these objects inside special
|
||||
wrapper objects that contain the behaviors.
|
||||
"""
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
from component import Component
|
||||
from structural.composite.component import Component
|
||||
|
||||
|
||||
class ConcreteComponent(Component):
|
||||
"""
|
||||
|
|
@ -7,4 +8,4 @@ class ConcreteComponent(Component):
|
|||
"""
|
||||
|
||||
def operation(self) -> str:
|
||||
return "ConcreteComponent"
|
||||
return "ConcreteComponent"
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
from decorator import Decorator
|
||||
from structural.decorator.decorator import Decorator
|
||||
|
||||
|
||||
class ConcreteDecoratorA(Decorator):
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
from component import Component
|
||||
from dataclasses import dataclass
|
||||
|
||||
from structural.decorator.component import Component
|
||||
|
||||
|
||||
@dataclass
|
||||
class Decorator(Component):
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
from component import Component
|
||||
from concrete_component import ConcreteComponent
|
||||
from concrete_decorators import ConcreteDecoratorA, ConcreteDecoratorB
|
||||
from structural.decorator.component import Component
|
||||
from structural.decorator.concrete_component import ConcreteComponent
|
||||
from structural.decorator.concrete_decorators import ConcreteDecoratorA, \
|
||||
ConcreteDecoratorB
|
||||
|
||||
|
||||
def client_code(component: Component):
|
||||
|
|
|
|||
|
|
@ -0,0 +1,4 @@
|
|||
"""
|
||||
Provides a simplified interface to a library, a framework, or any other complex
|
||||
set of classes.
|
||||
"""
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
"""
|
||||
Lets you fit more objects into the available amount of RAM by sharing common
|
||||
parts of state between multiple objects instead of keeping all of the data in
|
||||
each object.
|
||||
"""
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
"""
|
||||
Lets you provide a substitute or placeholder for another object. A proxy
|
||||
controls access to the original object, allowing you to perform something either
|
||||
before or after the request gets through to the original object.
|
||||
"""
|
||||
Loading…
Reference in a new issue