代理模式

需求:

小明让小李替他追小丽(送洋娃娃,送花,送巧克力)

没有代理的代码:

# -*- 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

只有代理的代码:

# -*- 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

只是把追求者类换成了代理类。

实际的代理模式代码:

# -*- 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


装饰模式
 
需求:

给人搭配不同的服饰

代码版本一

# -*- 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 '领带'
 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

这样写的话,功能是实现了,问题是如果增加“超人”的装扮,就要修改Person类,违反了开放-封闭原则。

 

代码版本二

# -*- 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 '领带'
 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

这样改了之后,如果增加超人装扮,确实不需要去修改Person类。存在的问题是,各种衣服是独立的,并且暴露在外边的,就是一件一件穿的,没有顺序,没有控制。

代码版本三

# -*- 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 '领带'
 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

标签:
Ruby,代理模式,装饰模式,设计模式

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。