需求:
小明讓小李替他追小麗(送洋娃娃,送花,送巧克力)
沒有代理的代碼:
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
|
# -*- encoding: utf-8 -*- #追求者類 class Pursuit attr_accessor :mm def initialize(mm) @mm = mm end def give_dolls puts "#{mm.name} 送你洋娃娃" end def give_flowers puts "#{mm.name} 送你鮮花" end def give_chocolate puts "#{mm.name} 送你巧克力" end end #被追求者類 class Girl attr_accessor :name def initialize(name) @name = name end end xiao_hong = Girl. new ( '小紅' ) xiao_ming = Pursuit. new (xiao_hong) xiao_ming.give_dolls xiao_ming.give_flowers xiao_ming.give_chocolate |
只有代理的代碼:
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
|
# -*- encoding: utf-8 -*- #代理類 class Proxy attr_accessor :mm def initialize(mm) @mm = mm end def give_dolls puts "#{mm.name} 送你洋娃娃" end def give_flowers puts "#{mm.name} 送你鮮花" end def give_chocolate puts "#{mm.name} 送你巧克力" end end #被追求者類 class Girl attr_accessor :name def initialize(name) @name = name end end xiao_hong = Girl. new ( '小紅' ) xiao_ming = Proxy. new (xiao_hong) xiao_ming.give_dolls xiao_ming.give_flowers xiao_ming.give_chocolate |
只是把追求者類換成了代理類。
實(shí)際的代理模式代碼:
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
|
# -*- encoding: utf-8 -*- #公共接口module module GiveGift def give_dolls end def give_flowers end def give_chocolate end end #追求者類 class Pursuit include GiveGift attr_accessor :mm , :name def initialize(mm) @mm = mm end def give_dolls puts "#{mm.name} 替#{name}送你洋娃娃" end def give_flowers puts "#{mm.name} 替#{name}送你鮮花" end def give_chocolate puts "#{mm.name} 替#{name}送你巧克力" end end #代理類 class Proxy include GiveGift attr_accessor :gg def initialize(mm) @gg = Pursuit. new (mm) end def give_dolls gg.give_dolls end def give_flowers gg.give_flowers end def give_chocolate gg.give_chocolate end end #被追求者類 class Girl attr_accessor :name def initialize(name) @name = name end end xiao_hong = Girl. new ( '小紅' ) xiao_ming = Proxy. new (xiao_hong) xiao_ming.gg.name = '小明' xiao_ming.give_dolls xiao_ming.give_flowers xiao_ming.give_chocolate |
裝飾模式
需求:
給人搭配不同的服飾
代碼版本一
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
|
# -*- encoding: utf-8 -*- class Person attr_accessor :name def initialize(name) @name = name end def wear_t_shirts puts '大T恤' end def wear_big_trouser puts '垮褲' end def wear_sneakers puts '破球鞋' end def wear_suit puts '西裝' end def wear_tie puts '領(lǐng)帶' end def wear_leather_shoes puts '皮鞋' end def show puts "*****裝扮的#{name}\n\n" end end xc=Person. new ( '小菜' ) puts "******第一種裝扮" xc.wear_t_shirts xc.wear_big_trouser xc.wear_sneakers xc.show puts "******第二種裝扮" xc.wear_suit xc.wear_tie xc.wear_leather_shoes xc.show |
這樣寫的話,功能是實(shí)現(xiàn)了,問題是如果增加“超人”的裝扮,就要修改Person類,違反了開放-封閉原則。
代碼版本二
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
|
# -*- encoding: utf-8 -*- class Person attr_accessor :name def initialize(name) @name = name enddef show puts "*****裝扮的#{name}\n\n" end end class Finery def show end end class TShirts < Finery def show puts '大T恤' end end class BigTrouser < Finery def show puts '垮褲' end end class Sneakers < Finery def show puts '破球鞋' end end class Suit < Finery def show puts '西裝' end end class Tie < Finery def show puts '領(lǐng)帶' end end class LeatherShoes < Finery def show puts '皮鞋' end end xc=Person. new ( '小菜' ) ts = TShirts. new bt = BigTrouser. new sk = Sneakers. new puts "******第一種裝扮" ts.show bt.show sk.show xc.show suit = Suit. new tie = Tie. new ls = LeatherShoes. new puts "******第二種裝扮" suit.show tie.show ls.show xc.show |
這樣改了之后,如果增加超人裝扮,確實(shí)不需要去修改Person類。存在的問題是,各種衣服是獨(dú)立的,并且暴露在外邊的,就是一件一件穿的,沒有順序,沒有控制。
代碼版本三
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
|
# -*- encoding: utf-8 -*- class Person attr_accessor :name def initialize(name= nil ) @name = name end def show puts "*****裝扮的#{name}\n\n" end end class Finery < Person attr_accessor :componet def decorate(componet) @componet = componet end def show componet.show if componet end end class TShirts < Finery def show super puts '大T恤' end end class BigTrouser < Finery def show super puts '垮褲' end end class Sneakers < Finery def show super puts '破球鞋' end end class Suit < Finery def show super puts '西裝' end end class Tie < Finery def show super puts '領(lǐng)帶' end end class LeatherShoes < Finery def show super puts '皮鞋' end end xc=Person. new ( '小菜' ) ts = TShirts. new bt = BigTrouser. new sk = Sneakers. new puts "******第一種裝扮" ts.decorate xc bt.decorate ts sk.decorate bt sk.show suit = Suit. new tie = Tie. new ls = LeatherShoes. new puts "******第二種裝扮" suit.decorate xc tie.decorate suit ls.decorate bt ls.show |