Skip to content

Commit 8034c7d

Browse files
author
Mu5alaf
committed
update
1 parent a316461 commit 8034c7d

File tree

11 files changed

+239
-38
lines changed

11 files changed

+239
-38
lines changed

Python/abstract_factory/main.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Dog:
2+
3+
"""one of the objects to be returuned"""
4+
5+
def speak(self):
6+
return "woof!"
7+
8+
def color(self):
9+
return "black"
10+
11+
def __str__(self):
12+
return "Dog"
13+
14+
class DogFactory:
15+
"""Concrete Factory"""
16+
17+
def get_pet(self):
18+
"""returns a dog objects"""
19+
return Dog()
20+
def get_food(self):
21+
"""returns a dog food objects"""
22+
return "Dog food"
23+
24+
class petStore:
25+
"""petStore houses our abstract factory"""
26+
27+
def __init__(self,pet_factory=None):
28+
"""pet_factory our abstract factory"""
29+
self._pet_factory = pet_factory
30+
31+
def show_pet(self):
32+
"""utility method to display the details of objects returen """
33+
34+
pet = self._pet_factory.get_pet()
35+
pet_food = self._pet_factory.get_food()
36+
37+
print("our pets is '{}'".format(pet))
38+
print("our pets say hello by '{}'".format(pet.speak()))
39+
print("its food is '{}'".format(pet_food))
40+
41+
#create a concrete factory
42+
factory = DogFactory()
43+
44+
#create a pet store housing our abstract factory
45+
shop = petStore(factory)
46+
47+
#invoke the utility method to show the details of our pet
48+
shop.show_pet()

Python/builder/main.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Director():
2+
"""Director"""
3+
4+
def __init__(self, builder):
5+
self._builder = builder
6+
7+
def construct_car(self):
8+
self._builder.create_new_car()
9+
self._builder.add_model()
10+
self._builder.add_tires()
11+
self._builder.add_engine()
12+
13+
def get_car(self):
14+
return self._builder.car
15+
16+
class Builder():
17+
"""Abstract Builder"""
18+
def __init__(self):
19+
self.car = None
20+
def create_new_car(self):
21+
self.car = Car()
22+
23+
class SkyLarkBuilder(Builder):
24+
"""concrete builder >> provide parts and tools to work on the pairs"""
25+
26+
def add_model (self):
27+
self.car.model = "SKylark"
28+
29+
def add_tires(self):
30+
self.car.tires = "Reqular tires"
31+
32+
def add_engine(self):
33+
self.car.engine = "Trubo Engine"
34+
35+
class Car():
36+
"""Product"""
37+
def __init__(self):
38+
self.model = None
39+
self.tires = None
40+
self.engine = None
41+
42+
def __str__(self):
43+
return '{} | {} | {}'.format(self.model, self.tires, self.engine)
44+
45+
builder = SkyLarkBuilder()
46+
director = Director(builder)
47+
director.construct_car()
48+
car = director.get_car()
49+
print(car)

Python/factory/main.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Dog:
2+
3+
"""Dog class"""
4+
5+
def __init__(self,name):
6+
self._name = name
7+
8+
def speak(self):
9+
return "woof!"
10+
def color(self):
11+
return "black"
12+
13+
def get_pet(pet="dog"):
14+
15+
"""The Factory Method"""
16+
17+
pets = dict(dog=Dog("Hope"))
18+
return pets[pet]
19+
20+
class Cat:
21+
22+
"""Cat class"""
23+
24+
def __init__(self,name):
25+
self._name = name
26+
27+
def speak(self):
28+
return "meow!!"
29+
def color(self):
30+
return "yellow"
31+
32+
def get_pet(pet="cat"):
33+
34+
"""The Factory Method"""
35+
36+
pets = dict(dog=Dog("Hope"),cat=Cat("Peace"))
37+
return pets[pet]
38+
39+
d = get_pet("dog")
40+
print(d.speak(),d.color())
41+
42+
c = get_pet("cat")
43+
print(c.speak(),c.color())

Python/prototype/main.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import copy
2+
3+
class Prototype:
4+
5+
def __init__(self):
6+
self._objects = {}
7+
8+
def register_object(self, name, obj):
9+
"""Register an object"""
10+
self._objects[name] = obj
11+
12+
def unregister_object(self, name):
13+
"unregister object"
14+
del self._objects[name]
15+
16+
def clone(self,name, **attr):
17+
"""Clone a registerd object and update its attributes"""
18+
obj = copy.deepcopy(self._objects.get(name))
19+
obj.__dict__.update(attr)
20+
return obj
21+
22+
class Car:
23+
def __init__(self):
24+
self.name = "Skylark"
25+
self.color = "red"
26+
self.option = "EX"
27+
def __str__(self):
28+
return "{} | {} | {}".format(self.name,self.color,self.option)
29+
30+
c = Car()
31+
Prototype = Prototype()
32+
Prototype.register_object('skylark', c)
33+
34+
c1 = Prototype.clone('skylark')
35+
print(c1)

Python/singletone/main.py

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,23 @@
1-
class SingletonMeta(type):
1+
class Borg:
22
"""
3-
The Singleton class can be implemented in different ways in Python. Some
4-
possible methods include: base class, decorator, metaclass. We will use the
5-
metaclass because it is best suited for this purpose.
3+
the Borg design pattern
64
"""
7-
8-
_instances = {}
9-
10-
def __call__(cls, *args, **kwargs):
11-
"""
12-
Possible changes to the value of the `__init__` argument do not affect
13-
the returned instance.
14-
"""
15-
if cls not in cls._instances:
16-
instance = super().__call__(*args, **kwargs)
17-
cls._instances[cls] = instance
18-
return cls._instances[cls]
19-
20-
21-
class Singleton(metaclass=SingletonMeta):
22-
def some_business_logic(self):
23-
"""
24-
Finally, any singleton should define some business logic, which can be
25-
executed on its instance.
26-
"""
27-
28-
# ...
29-
30-
31-
if __name__ == "__main__":
32-
# The client code.
33-
34-
s1 = Singleton()
35-
s2 = Singleton()
36-
37-
if id(s1) == id(s2):
38-
print("Singleton works, both variables contain the same instance.")
39-
else:
40-
print("Singleton failed, variables contain different instances.")
5+
_shared_data = {} # attribute dictionary
6+
7+
def __init__(self):
8+
self.__dict__ = self._shared_data # make an attribute dictionary
9+
10+
class Singleton(Borg):
11+
"""the singleton class"""
12+
def __init__(self, **kwargs):
13+
Borg.__init__(self)
14+
self._shared_data.update(kwargs) #update the attribute dictionary by inserting a new key value pair
15+
16+
def __str__(self):
17+
return str(self._shared_data) # returns the attribute dictionary for printing
18+
19+
x = Singleton()
20+
s1 = Singleton(HTTP = "Hyper Test Transfer Protocol")
21+
s2 = Singleton(HTTPS = "Hyper Test Transfer Protocol Secure")
22+
23+
print(x)

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,46 @@
1717

1818
- Behavioral patterns
1919
These patterns are concerned with algorithms and the assignment of responsibilities between objects.
20+
21+
## Creational patterns
22+
23+
<img src="./image/factory-method-en.png" alt="Picture" width="800" height="600">
24+
25+
1- Factory pattern
26+
- is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
27+
28+
**Check**
29+
[title](https://refactoring.guru/design-patterns/factory-method)
30+
31+
<img src="./image/abstract-factory-en.png" alt="Picture" width="800" height="600">
32+
33+
2- Abstract Factory
34+
- is a creational design pattern that lets you produce families of related objects without specifying their concrete classes.
35+
36+
**Check**
37+
[title](https://refactoring.guru/design-patterns/abstract-factory)
38+
39+
40+
<img src="./image/singleton.png" alt="Picture" width="800" height="600">
41+
42+
3- Singleton
43+
- is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.
44+
45+
**Check**
46+
[title](https://refactoring.guru/design-patterns/singleton)
47+
48+
<img src="./image/builder-en.png" alt="Picture" width="800" height="600">
49+
50+
4- Builder
51+
- is a creational design pattern that 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.
52+
53+
**Check**
54+
[title](https://refactoring.guru/design-patterns/builder)
55+
56+
<img src="./image/prototype.png" alt="Picture" width="800" height="600">
57+
58+
5- Prototype
59+
- is a creational design pattern that lets you copy existing objects without making your code dependent on their classes.
60+
61+
**Check**
62+
[title](https://refactoring.guru/design-patterns/prototype)

image/abstract-factory-en.png

48.9 KB
Loading

image/builder-en.png

26 KB
Loading

image/factory-method-en.png

51.3 KB
Loading

image/prototype.png

23.2 KB
Loading

0 commit comments

Comments
 (0)