diff --git a/README.md b/README.md index cefd5fd..0a491fd 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/__init__.py b/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/creational/README.md b/creational/README.md index 7ec7c96..6776d99 100644 --- a/creational/README.md +++ b/creational/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) diff --git a/creational/__init__.py b/creational/__init__.py new file mode 100644 index 0000000..db800a2 --- /dev/null +++ b/creational/__init__.py @@ -0,0 +1,4 @@ +""" +Creational patterns provide various object creation mechanisms, which increase +flexibility and reuse of existing code. +""" diff --git a/creational/abstract-factory/__init__.py b/creational/abstract-factory/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/creational/abstract-factory/AbstractFactory.py b/creational/abstract_factory/AbstractFactory.py similarity index 81% rename from creational/abstract-factory/AbstractFactory.py rename to creational/abstract_factory/AbstractFactory.py index a877344..64e4b53 100644 --- a/creational/abstract-factory/AbstractFactory.py +++ b/creational/abstract_factory/AbstractFactory.py @@ -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): diff --git a/creational/abstract-factory/AbstractProductA.py b/creational/abstract_factory/AbstractProductA.py similarity index 100% rename from creational/abstract-factory/AbstractProductA.py rename to creational/abstract_factory/AbstractProductA.py diff --git a/creational/abstract-factory/AbstractProductB.py b/creational/abstract_factory/AbstractProductB.py similarity index 90% rename from creational/abstract-factory/AbstractProductB.py rename to creational/abstract_factory/AbstractProductB.py index 055e7f9..0984091 100644 --- a/creational/abstract-factory/AbstractProductB.py +++ b/creational/abstract_factory/AbstractProductB.py @@ -1,6 +1,6 @@ from abc import ABC, abstractmethod -from AbstractProductA import AbstractProductA +from creational.abstract_factory.AbstractProductA import AbstractProductA class AbstractProductB(ABC): diff --git a/creational/abstract-factory/README.md b/creational/abstract_factory/README.md similarity index 100% rename from creational/abstract-factory/README.md rename to creational/abstract_factory/README.md diff --git a/creational/abstract_factory/__init__.py b/creational/abstract_factory/__init__.py new file mode 100644 index 0000000..c70209c --- /dev/null +++ b/creational/abstract_factory/__init__.py @@ -0,0 +1,4 @@ +""" +Lets you produce families of related objects without specifying their concrete +classes. +""" diff --git a/creational/abstract-factory/factories.py b/creational/abstract_factory/factories.py similarity index 80% rename from creational/abstract-factory/factories.py rename to creational/abstract_factory/factories.py index 9501f8a..defbc5e 100644 --- a/creational/abstract-factory/factories.py +++ b/creational/abstract_factory/factories.py @@ -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): diff --git a/creational/abstract-factory/main.py b/creational/abstract_factory/main.py similarity index 82% rename from creational/abstract-factory/main.py rename to creational/abstract_factory/main.py index 760e8f3..dc0180c 100644 --- a/creational/abstract-factory/main.py +++ b/creational/abstract_factory/main.py @@ -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: diff --git a/creational/abstract-factory/products.py b/creational/abstract_factory/products.py similarity index 90% rename from creational/abstract-factory/products.py rename to creational/abstract_factory/products.py index e63e8ce..9c4ce9c 100644 --- a/creational/abstract-factory/products.py +++ b/creational/abstract_factory/products.py @@ -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): diff --git a/creational/builder/Director.py b/creational/builder/Director.py index 5549c47..84a5ac5 100644 --- a/creational/builder/Director.py +++ b/creational/builder/Director.py @@ -1,4 +1,4 @@ -from Builder import Builder +from creational.builder.Builder import Builder class Director: diff --git a/creational/builder/__init__.py b/creational/builder/__init__.py index e69de29..bde98b6 100644 --- a/creational/builder/__init__.py +++ b/creational/builder/__init__.py @@ -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. +""" diff --git a/creational/builder/builders.py b/creational/builder/builders.py index 9b2d748..b520fc5 100644 --- a/creational/builder/builders.py +++ b/creational/builder/builders.py @@ -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): diff --git a/creational/builder/main.py b/creational/builder/main.py index c4e6c4f..a44d25a 100644 --- a/creational/builder/main.py +++ b/creational/builder/main.py @@ -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() diff --git a/creational/factory-method/__init__.py b/creational/factory-method/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/creational/factory-method/ICreator.py b/creational/factory_method/ICreator.py similarity index 100% rename from creational/factory-method/ICreator.py rename to creational/factory_method/ICreator.py diff --git a/creational/factory-method/IProduct.py b/creational/factory_method/IProduct.py similarity index 100% rename from creational/factory-method/IProduct.py rename to creational/factory_method/IProduct.py diff --git a/creational/factory-method/README.md b/creational/factory_method/README.md similarity index 100% rename from creational/factory-method/README.md rename to creational/factory_method/README.md diff --git a/creational/factory_method/__init__.py b/creational/factory_method/__init__.py new file mode 100644 index 0000000..871153e --- /dev/null +++ b/creational/factory_method/__init__.py @@ -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. +""" diff --git a/creational/factory-method/creators.py b/creational/factory_method/creators.py similarity index 80% rename from creational/factory-method/creators.py rename to creational/factory_method/creators.py index 9d4fcbc..63f16cf 100644 --- a/creational/factory-method/creators.py +++ b/creational/factory_method/creators.py @@ -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): diff --git a/creational/factory-method/main.py b/creational/factory_method/main.py similarity index 81% rename from creational/factory-method/main.py rename to creational/factory_method/main.py index cd2edfa..c67f12d 100644 --- a/creational/factory-method/main.py +++ b/creational/factory_method/main.py @@ -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: diff --git a/creational/factory-method/products.py b/creational/factory_method/products.py similarity index 85% rename from creational/factory-method/products.py rename to creational/factory_method/products.py index 94297e2..1445ac7 100644 --- a/creational/factory-method/products.py +++ b/creational/factory_method/products.py @@ -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): diff --git a/creational/prototype/__init__.py b/creational/prototype/__init__.py index e69de29..481c764 100644 --- a/creational/prototype/__init__.py +++ b/creational/prototype/__init__.py @@ -0,0 +1,4 @@ +""" +Lets you copy existing objects without making your code dependent on their +classes. +""" diff --git a/creational/prototype/components.py b/creational/prototype/components.py index 0d854e4..a8f8fc4 100644 --- a/creational/prototype/components.py +++ b/creational/prototype/components.py @@ -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) diff --git a/creational/prototype/main.py b/creational/prototype/main.py index 62a3424..7be6560 100644 --- a/creational/prototype/main.py +++ b/creational/prototype/main.py @@ -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() diff --git a/creational/singleton/__init__.py b/creational/singleton/__init__.py new file mode 100644 index 0000000..b30f5ca --- /dev/null +++ b/creational/singleton/__init__.py @@ -0,0 +1,4 @@ +""" +Lets you ensure that a class has only one instance, while providing a global +access point to this instance. +""" diff --git a/structural/__init__.py b/structural/__init__.py index e69de29..3d8cb52 100644 --- a/structural/__init__.py +++ b/structural/__init__.py @@ -0,0 +1,4 @@ +""" +Structural patterns explain how to assemble objects and classes into larger +structures while keeping these structures flexible and efficient. +""" diff --git a/structural/adapter/__init__.py b/structural/adapter/__init__.py new file mode 100644 index 0000000..a8930ce --- /dev/null +++ b/structural/adapter/__init__.py @@ -0,0 +1 @@ +"""Allows objects with incompatible interfaces to collaborate.""" diff --git a/structural/bridge/__init__.py b/structural/bridge/__init__.py index e69de29..0c17e17 100644 --- a/structural/bridge/__init__.py +++ b/structural/bridge/__init__.py @@ -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. +""" diff --git a/structural/bridge/abstractions.py b/structural/bridge/abstractions.py index 31dc869..24c5381 100644 --- a/structural/bridge/abstractions.py +++ b/structural/bridge/abstractions.py @@ -1,6 +1,6 @@ from dataclasses import dataclass -from IImplementation import Implementation +from structural.bridge.IImplementation import Implementation @dataclass diff --git a/structural/bridge/implementations.py b/structural/bridge/implementations.py index 6791fb2..cbc447e 100644 --- a/structural/bridge/implementations.py +++ b/structural/bridge/implementations.py @@ -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): diff --git a/structural/bridge/main.py b/structural/bridge/main.py index 762aff4..cf4ab71 100644 --- a/structural/bridge/main.py +++ b/structural/bridge/main.py @@ -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() diff --git a/structural/composite/__init__.py b/structural/composite/__init__.py index e69de29..c3a522b 100644 --- a/structural/composite/__init__.py +++ b/structural/composite/__init__.py @@ -0,0 +1,4 @@ +""" +Lets you compose objects into tree structures and then work with these +structures as if they were individual objects. +""" diff --git a/structural/composite/composite.py b/structural/composite/composite.py index 8ed7485..df3c29d 100644 --- a/structural/composite/composite.py +++ b/structural/composite/composite.py @@ -1,7 +1,7 @@ from dataclasses import dataclass, field from typing import List -from component import Component +from structural.composite.component import Component @dataclass diff --git a/structural/composite/leaf.py b/structural/composite/leaf.py index 158f5c1..bd86417 100644 --- a/structural/composite/leaf.py +++ b/structural/composite/leaf.py @@ -1,4 +1,4 @@ -from component import Component +from structural.composite.component import Component class Leaf(Component): diff --git a/structural/composite/main.py b/structural/composite/main.py index 4915d09..2763719 100644 --- a/structural/composite/main.py +++ b/structural/composite/main.py @@ -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: diff --git a/structural/decorator/__init__.py b/structural/decorator/__init__.py index e69de29..17052aa 100644 --- a/structural/decorator/__init__.py +++ b/structural/decorator/__init__.py @@ -0,0 +1,4 @@ +""" +Lets you attach new behaviors to objects by placing these objects inside special +wrapper objects that contain the behaviors. +""" diff --git a/structural/decorator/concrete_component.py b/structural/decorator/concrete_component.py index 7c8fdc9..f07017b 100644 --- a/structural/decorator/concrete_component.py +++ b/structural/decorator/concrete_component.py @@ -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" \ No newline at end of file + return "ConcreteComponent" diff --git a/structural/decorator/concrete_decorators.py b/structural/decorator/concrete_decorators.py index e440393..7cdbcae 100644 --- a/structural/decorator/concrete_decorators.py +++ b/structural/decorator/concrete_decorators.py @@ -1,4 +1,4 @@ -from decorator import Decorator +from structural.decorator.decorator import Decorator class ConcreteDecoratorA(Decorator): diff --git a/structural/decorator/decorator.py b/structural/decorator/decorator.py index bd2893b..1aee3ca 100644 --- a/structural/decorator/decorator.py +++ b/structural/decorator/decorator.py @@ -1,6 +1,7 @@ -from component import Component from dataclasses import dataclass +from structural.decorator.component import Component + @dataclass class Decorator(Component): diff --git a/structural/decorator/main.py b/structural/decorator/main.py index b9744db..3c23d77 100644 --- a/structural/decorator/main.py +++ b/structural/decorator/main.py @@ -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): diff --git a/structural/facade/__init__.py b/structural/facade/__init__.py index e69de29..c516525 100644 --- a/structural/facade/__init__.py +++ b/structural/facade/__init__.py @@ -0,0 +1,4 @@ +""" +Provides a simplified interface to a library, a framework, or any other complex +set of classes. +""" diff --git a/structural/flyweight/__init__.py b/structural/flyweight/__init__.py index e69de29..0ae7476 100644 --- a/structural/flyweight/__init__.py +++ b/structural/flyweight/__init__.py @@ -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. +""" diff --git a/structural/proxy/__init__.py b/structural/proxy/__init__.py index e69de29..cc44e2e 100644 --- a/structural/proxy/__init__.py +++ b/structural/proxy/__init__.py @@ -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. +"""