Skip to content

Commit

Permalink
#1 update template method and factory method
Browse files Browse the repository at this point in the history
  • Loading branch information
chuoru committed Jul 30, 2020
1 parent 40a1799 commit 60d2cce
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 20 deletions.
11 changes: 11 additions & 0 deletions DesignPattern/FactoryMethod/FactoryMethod.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@startuml
class Creator{

+ someOperation()
+ createProduct(): Product
}

class ConcreteCreatorA{
+ createProduct()
}
@enduml
6 changes: 6 additions & 0 deletions DesignPattern/FactoryMethod/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Factory Method

- Thuộc vào nhóm chế tạo, liên quan nhiều đến việc tạo instance.


- Interface Python
51 changes: 36 additions & 15 deletions DesignPattern/FactoryMethod/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,36 @@ def capture_once(self):
return self._image


class RGBCamera(Camera):

def __init__(self):
pass

def _open(self):
print("rgb camera open")

def _capture(self):
print("rgb camera capture")

def _close(self):
print("rgb camera close")


class DepthCamera(Camera):

def __init__(self):
pass

def _open(self):
print("depth camera open")

def _capture(self):
print("depth camera capture")

def _close(self):
print("depth camera close")


class Image(ABC):

@abstractmethod
Expand Down Expand Up @@ -54,24 +84,15 @@ def get_binary(self):
print("get binary of rgb image")


class RGBCamera(Camera):

def __init__(self):
pass

def _open(self):
print("rgb camera open")

def _capture(self):
print("rgb camera capture")

def _close(self):
print("rgb camera close")


if __name__ == "__main__":
rgb_camera = RGBCamera()

rgb_image = rgb_camera.capture_once()

print(rgb_image)

depth_camera = DepthCamera()

depth_image = depth_camera.capture_once()

print(depth_image)
28 changes: 28 additions & 0 deletions DesignPattern/TemplateMethod/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Template Method

- Thuộc vào nhóm các pattern liên đến hành vi
- Thường đóng vai trò xương sống khi code các thuật toán.
- Những hàm trong class trên sẽ được override bởi class dưới

Anh Thanh Niên (nghề nghiệp ) -> Hoạ sĩ
-> Nhạc sĩ

- Cách dùng mà sử dụng rất nhiều: code các bước tiến hành một quy trình.


(Đi Đến trường)
Ra khỏi nhà -> Đến trường
(Ăn sáng - hook)

Đi học
Ra khỏi nhà -> Đến trường -> Học


Đi thi
Ra khỏi nhà -> Đến trường -> Thi

Đi học muộn
Ra khỏi nhà -> Đến trường -> Học
(Ăn sáng - buông )

- REST API: query data từ database
15 changes: 10 additions & 5 deletions DesignPattern/TemplateMethod/conceptual.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def base_operation() -> None:

@abstractmethod
def required_operations1(self) -> None:
pass
print("OK")

@abstractmethod
def required_operations2(self) -> None:
Expand All @@ -31,22 +31,26 @@ def hook(self) -> None:
hooks already have default (not empty) implementation.
:return:
"""
pass
print("Old hook")


class ConcreteClass1(AbstractClass):

def required_operations1(self) -> None:
print("Operation 1 of ConcreteClass 1")
pass # print("Operation 1 of ConcreteClass 1")

def required_operations2(self) -> None:
print("Operation 2 of ConcreteClass 2")
print("Operation 2 of ConcreteClass 1")

@staticmethod
def base_operation() -> None:
print("Something in common in ConcreteClass 1")


class ConcreteClass2(AbstractClass):

def required_operations1(self) -> None:
print("Operation 1 of ConcreteClass 1")
print("Operation 1 of ConcreteClass 2")

def required_operations2(self) -> None:
print("Operation 2 of ConcreteClass 2")
Expand All @@ -56,6 +60,7 @@ def hook(self) -> None:


def client_code(abstract_class: AbstractClass) -> None:
# abstract_class: AbstractClass (type)
abstract_class.template_method()


Expand Down

0 comments on commit 60d2cce

Please sign in to comment.