python学习小技巧:举例讲解Python中的Null模式与桥接模式编程

这篇文章主要介绍了Python中的Null模式与桥接模式编程,Null模式与桥接模式都属于Python的设计模式编程,需要的朋友可以参考下
Null模式
我想每个人都有一种经历,为了获取某属性,但是有时候属性是None,那么需要你做异常处理, 而假如你想节省这样的条件过滤的代码,可以使用Null模式以减少对象是否为None的判断

python的例子
我举个不是很通用的例子,只是为了让大家理解这个模式:我有很多类, 但是不是每个类都有类方法test,所以我调用类方法就要做个异常处理,类似这样

class A(object):
pass

class B(object):
b = 1
@classmethod
def test(cls):
print cls.b

def get_test(x):
try:
return x.test
except AttributeError:
return None

# 我这里只写了2个类,但是其实有很多类
for i in [A, B]:
test = get_test(i)
# 我要判断以下是否获得了这个类方法才能决定是否可以执行
if test:
test()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
但是我用Null方法就可以这样

class Null(object):

def __init__(self, *args, **kwargs):
"忽略参数"
return None

def __call__(self, *args, **kwargs):
"忽略实例调用"
return self

def __getattr__(self, mname):
"忽略属性获得"
return self

def __setattr__(self, name, value):
"忽略设置属性操作"
return self

def __delattr__(self, name):
‘‘‘忽略删除属性操作‘‘‘
return self

def __repr__(self):
return "<Null>"

def __str__(self):
return "Null"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
还是上面的功能

class Null(object):

def __init__(self, *args, **kwargs):
"忽略参数"
return None

def __call__(self, *args, **kwargs):
"忽略实例调用"
return self

def __getattr__(self, mname):
"忽略属性获得"
return self

def __setattr__(self, name, value):
"忽略设置属性操作"
return self

def __delattr__(self, name):
‘‘‘忽略删除属性操作‘‘‘
return self

def __repr__(self):
return "<Null>"

def __str__(self):
return "Null"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
桥接模式
这个模式其实就是把产品类的实现和抽象类分离,能够灵活的变化,假如你记得状态模式,它是修改内部属性, 而桥接模式是指定好内部属性,每个产品类指定这个属性被桥接模式类调用,适用于产品类可能经常调整变化,这样还能减少了产品类之间的耦合

python的例子
这里实现一个打印操作系统名字的功能

class Bridge(object):

def __init__(self):
self.__implementation = None

def someFunctionality(self):
raise NotImplemented()

class UseCase1(Bridge):
# 根据初始化参数传入实现的产品类
def __init__(self, implementation):
self.__implementation = implementation
# 根据传入的产品类的属性打印结果
def someFunctionality(self):
print "UseCase1: ",
self.__implementation.anotherFunctionality()


class UseCase2(Bridge):
def __init__(self, implementation):
self.__implementation = implementation

def someFunctionality(self):
print "UseCase2: ",
self.__implementation.anotherFunctionality()


class ImplementationInterface:

def anotherFunctionality(self):
raise NotImplemented

# 这里其实才是实现的产品类
class Linux(ImplementationInterface):

# 它定义了这个方法,回应操作系统的名字
def anotherFunctionality(self):
print "Linux!"


class Windows(ImplementationInterface):
def anotherFunctionality(self):
print "Windows."


def main():
linux = Linux()
windows = Windows()

useCase = UseCase1(linux)
useCase.someFunctionality()

useCase = UseCase1(windows)
useCase.someFunctionality()

useCase = UseCase2(linux)
useCase.someFunctionality()

useCase = UseCase2(windows)
useCase.someFunctionality()


if __name__ == "__main__":
main()
class Bridge(object):

def __init__(self):
self.__implementation = None

def someFunctionality(self):
raise NotImplemented()

class UseCase1(Bridge):
# 根据初始化参数传入实现的产品类
def __init__(self, implementation):
self.__implementation = implementation
# 根据传入的产品类的属性打印结果
def someFunctionality(self):
print "UseCase1: ",
self.__implementation.anotherFunctionality()


class UseCase2(Bridge):
def __init__(self, implementation):
self.__implementation = implementation

def someFunctionality(self):
print "UseCase2: ",
self.__implementation.anotherFunctionality()


class ImplementationInterface:

def anotherFunctionality(self):
raise NotImplemented

# 这里其实才是实现的产品类
class Linux(ImplementationInterface):

# 它定义了这个方法,回应操作系统的名字
def anotherFunctionality(self):
print "Linux!"


class Windows(ImplementationInterface):
def anotherFunctionality(self):
print "Windows."


def main():
linux = Linux()
windows = Windows()

useCase = UseCase1(linux)
useCase.someFunctionality()

useCase = UseCase1(windows)
useCase.someFunctionality()

useCase = UseCase2(linux)
useCase.someFunctionality()

useCase = UseCase2(windows)
useCase.someFunctionality()


if __name__ == "__main__":
main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
非常感谢你的阅读
大学的时候选择了自学python,工作了发现吃了计算机基础不好的亏,学历不行这是没办法的事,只能后天弥补,于是在编码之外开启了自己的逆袭之路,不断的学习python核心知识,深入的研习计算机基础知识,整理好了,我放在我们的微信公众号《程序员学府》,如果你也不甘平庸,那就与我一起在编码之外,不断成长吧!
————————————————
原文链接:https://blog.csdn.net/daidaiweng/article/details/105826973

https://bcy.net/tag/20509883
https://bcy.net/tag/20510017
https://bcy.net/tag/20510016
https://bcy.net/tag/20510014
https://bcy.net/tag/20510015
https://bcy.net/tag/20509993
https://bcy.net/tag/20509994
https://bcy.net/tag/20509995
https://bcy.net/tag/20509997
https://bcy.net/tag/20509996
https://bcy.net/tag/20509998
https://bcy.net/tag/20509999
https://bcy.net/tag/20510000
https://bcy.net/tag/20510002
https://bcy.net/tag/20510001

相关推荐