-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1 lườt qua conceptual của factory method và thử ứng dụng vào camera …
…drive classes. Chưa thành công và muốn đi ngủ
- Loading branch information
Showing
6 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from __future__ import annotations | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class Creator(ABC): | ||
|
||
@abstractmethod | ||
def factory_method(self): | ||
""" | ||
the Creator also provide some default implementation of the factory method | ||
:return: | ||
""" | ||
pass | ||
|
||
def some_operation(self) -> str: | ||
""" | ||
not creating products. Only contains some core business logic that relies on Product objects. | ||
:return: | ||
""" | ||
|
||
# Call the factory method to create a Product object: | ||
product = self.factory_method() | ||
|
||
# Now, use the product. | ||
result = f"Creator: The same creator's code has just worked with {product.operation()}" | ||
|
||
return result | ||
|
||
|
||
class ConcreteCreator1(Creator): | ||
|
||
def factory_method(self) -> ConcreteProduct1: | ||
return ConcreteProduct1() | ||
|
||
|
||
class ConcreteCreator2(Creator): | ||
|
||
def factory_method(self) -> ConcreteProduct2: | ||
return ConcreteProduct2() | ||
|
||
|
||
class Product(ABC): | ||
|
||
@abstractmethod | ||
def operation(self) -> str: | ||
pass | ||
|
||
|
||
""" | ||
Concrete Products provide various implementations of the Product interface. | ||
""" | ||
|
||
|
||
class ConcreteProduct1(Product): | ||
def operation(self) -> str: | ||
return "{Result of the ConcreteProduct1}" | ||
|
||
|
||
class ConcreteProduct2(Product): | ||
def operation(self) -> str: | ||
return "{Result of the ConcreteProduct2}" | ||
|
||
|
||
def client_code(creator: Creator) -> None: | ||
""" | ||
The client code works with an instance of a concrete creator, albeit through its base interface. As long as the | ||
client keeps working with the creator via the base interface, you can pass it any creator's subclass. | ||
:param creator: | ||
:return: | ||
""" | ||
|
||
print(f"Client: I'm not aware of the creator's class, but it still works. \n" | ||
f"{creator.some_operation()}", end="") | ||
|
||
|
||
if __name__ == "__main__": | ||
print("App: Launched with the ConcreteCreator1") | ||
client_code(ConcreteCreator1()) | ||
print("\n") | ||
|
||
print("App: Launched with the ConcreteCreator2") | ||
client_code(ConcreteCreator2()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class Camera(ABC): | ||
|
||
@abstractmethod | ||
def _open(self): | ||
pass | ||
|
||
@abstractmethod | ||
def _close(self): | ||
pass | ||
|
||
@abstractmethod | ||
def _capture(self): | ||
return f"capturing image .." | ||
|
||
def capture_once(self): | ||
self._open() | ||
|
||
self._image = self._capture() | ||
|
||
self._close() | ||
|
||
return self._image | ||
|
||
|
||
class Image(ABC): | ||
|
||
@abstractmethod | ||
def get_binary(self): | ||
pass | ||
|
||
|
||
class DepthImage(Image): | ||
|
||
def __init__(self, width, height): | ||
self.__width = width | ||
self.__height = height | ||
print("This image has shape of ({}, {})".format(width, height)) | ||
|
||
def get_binary(self): | ||
print("get binary of depth image") | ||
|
||
|
||
class RGBImage(Image): | ||
|
||
def __init__(self, width, height): | ||
self.__width = width | ||
self.__height = height | ||
print("This image has shape of ({}, {})".format(width, height)) | ||
|
||
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) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.