换一种方式,在impl中写wrapper,然后调用mapper的方式
时间: 2023-11-17 20:12:46 浏览: 36
可以使用装饰器模式来实现,在wrapper中封装mapper,并在wrapper中实现新的方法,然后在调用时使用wrapper来代替mapper。
示例代码如下:
```python
from my_mapper import MyMapper
class MyWrapper:
def __init__(self, mapper: MyMapper):
self.mapper = mapper
def new_method(self, arg1, arg2):
# 在wrapper中实现新的方法
# 可以使用mapper中的方法来完成具体逻辑
result = self.mapper.old_method(arg1)
result += arg2
return result
# 其他方法可以直接调用mapper中的方法
def old_method(self, arg):
return self.mapper.old_method(arg)
```
在使用时,可以先创建一个mapper对象,然后再使用wrapper来包装:
```python
mapper = MyMapper()
wrapper = MyWrapper(mapper)
result1 = wrapper.new_method('hello', 'world')
result2 = wrapper.old_method('hello')
```
这样就可以使用wrapper来代替mapper了,同时还可以在wrapper中实现新的方法。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)