设计模式之代理模式(python实现)

原博主地址:(https://blog.csdn.net/LoveLion/article/details/17517213)
代理模式:给某一个对象提供一个代理或占位符,并由代理对象来控制对原对象的访问。
Proxy Pattern: Provide a surrogate or placeholder for another object to control access to it.

代理模式就是兄弟实现了这个方法,那我就在自己的实现中,加上兄弟的实现部分,除此之外,在加上自己的功能
使用python实现

from abc import abstractmethod, ABCMeta


class Searcher(metaclass=ABCMeta):

	@abstractmethod
	def deSearch(self):
		pass


class RealSearcher(Searcher): 

	def deSearch(self):
		print("real搜索")

	
class ProxySearcher(Searcher):	
	rSearcher = RealSearcher();

	def deSearch(self):
		print("让我去搜索")
		self.rSearcher.deSearch();
		print("代理人帮忙后,我还要做别的事情呢")

Test

from 代理模式.Searcher import ProxySearcher
proxySearcher = ProxySearcher()
proxySearcher.deSearch()

猜你喜欢

转载自blog.csdn.net/qq_33321609/article/details/87859654