Null模式
我想每個人都有一種經(jīng)歷,為了獲取某屬性,但是有時候?qū)傩允荖one,那么需要你做異常處理, 而假如你想節(jié)省這樣的條件過濾的代碼,可以使用Null模式以減少對象是否為None的判斷
python的例子
我舉個不是很通用的例子,只是為了讓大家理解這個模式:我有很多類, 但是不是每個類都有類方法test,所以我調(diào)用類方法就要做個異常處理,類似這樣
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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) # 我要判斷以下是否獲得了這個類方法才能決定是否可以執(zhí)行 if test: test() |
但是我用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): "忽略參數(shù)" return None def __call__( self , * args, * * kwargs): "忽略實例調(diào)用" return self def __getattr__( self , mname): "忽略屬性獲得" return self def __setattr__( self , name, value): "忽略設(shè)置屬性操作" 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): "忽略參數(shù)" return None def __call__( self , * args, * * kwargs): "忽略實例調(diào)用" return self def __getattr__( self , mname): "忽略屬性獲得" return self def __setattr__( self , name, value): "忽略設(shè)置屬性操作" return self def __delattr__( self , name): '''忽略刪除屬性操作''' return self def __repr__( self ): return "<Null>" def __str__( self ): return "Null" |
橋接模式
這個模式其實就是把產(chǎn)品類的實現(xiàn)和抽象類分離,能夠靈活的變化,假如你記得狀態(tài)模式,它是修改內(nèi)部屬性, 而橋接模式是指定好內(nèi)部屬性,每個產(chǎn)品類指定這個屬性被橋接模式類調(diào)用,適用于產(chǎn)品類可能經(jīng)常調(diào)整變化,這樣還能減少了產(chǎn)品類之間的耦合
python的例子
這里實現(xiàn)一個打印操作系統(tǒng)名字的功能
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
|
class Bridge( object ): def __init__( self ): self .__implementation = None def someFunctionality( self ): raise NotImplemented() class UseCase1(Bridge): # 根據(jù)初始化參數(shù)傳入實現(xiàn)的產(chǎn)品類 def __init__( self , implementation): self .__implementation = implementation # 根據(jù)傳入的產(chǎn)品類的屬性打印結(jié)果 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 # 這里其實才是實現(xiàn)的產(chǎn)品類 class Linux(ImplementationInterface): # 它定義了這個方法,回應(yīng)操作系統(tǒng)的名字 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() |