某系统中的文本显示组件类和图片显示组件类都继承了组件类,分别用于显示文本内容和图片内容。现要构造有滚动条,或者带有黑色边框,或者既有滚动条又有黑色边框的文本显示组件和图片显示组件。为了减少类的个数,采用装饰器模式来进行设计。请绘制类图并编程实现。
时间: 2023-06-26 09:03:21 浏览: 150
以下是类图:
![decorator_pattern](https://img-blog.csdnimg.cn/20211201172956866.png)
以下是代码实现:
```python
from abc import ABC, abstractmethod
# 抽象组件类
class Component(ABC):
@abstractmethod
def display(self):
pass
# 具体文本显示组件类
class TextComponent(Component):
def __init__(self, text):
self.text = text
def display(self):
print(f"Text: {self.text}")
# 具体图片显示组件类
class ImageComponent(Component):
def __init__(self, image):
self.image = image
def display(self):
print(f"Image: {self.image}")
# 抽象装饰器类
class Decorator(Component):
def __init__(self, component):
self.component = component
def display(self):
self.component.display()
# 滚动条装饰器类
class ScrollDecorator(Decorator):
def display(self):
self.add_scroll()
super().display()
def add_scroll(self):
print("Add scroll bar to the component")
# 黑色边框装饰器类
class BorderDecorator(Decorator):
def display(self):
self.add_border()
super().display()
def add_border(self):
print("Add black border to the component")
# 滚动条和黑色边框装饰器类
class ScrollAndBorderDecorator(Decorator):
def display(self):
self.add_scroll_and_border()
super().display()
def add_scroll_and_border(self):
print("Add scroll bar and black border to the component")
if __name__ == '__main__':
# 创建具体文本显示组件对象
text_component = TextComponent("Hello, world!")
# 创建具体图片显示组件对象
image_component = ImageComponent("https://example.com/image.jpg")
# 创建滚动条装饰器并装饰文本显示组件
scroll_text_component = ScrollDecorator(text_component)
# 创建黑色边框装饰器并装饰图片显示组件
border_image_component = BorderDecorator(image_component)
# 创建滚动条和黑色边框装饰器并装饰文本显示组件
scroll_border_text_component = ScrollAndBorderDecorator(text_component)
# 显示组件
scroll_text_component.display()
print()
border_image_component.display()
print()
scroll_border_text_component.display()
```
输出结果如下:
```
Add scroll bar to the component
Text: Hello, world!
Add black border to the component
Image: https://example.com/image.jpg
Add scroll bar and black border to the component
Add scroll bar to the component
Text: Hello, world!
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)