Ruby uses the agent mode in design pattern and the code instance of the adornment Pattern _ruby topic

Source: Internet
Author: User

Agent mode

Demand:

Xiaoming let Xiao Li Chase for him (send doll, send flowers, send chocolate)

Code with no agents:

#-*-Encoding:utf-8-*-

#追求者类
class Pursuit
 attr_accessor:mm
 
 def Initialize (mm)
  @mm = mm
 End
 
 def give_dolls
  puts "#{mm.name} send you Doll"
 end
 
 def give_flowers
  puts "#{mm.name} send you flowers"
 end
 
 def give_chocolate
  puts "#{mm.name} send you chocolate"
 end

#被追求者类
class Girl
 attr_ Accessor:name
 
 def Initialize (name)
  @name = name
 end

xiao_hong = girl.new (' Little Red ')

xiao_ming = Pursuit.new (xiao_hong)
xiao_ming.give_dolls
xiao_ming.give_flowers
xiao_ming.give_ Chocolate

Only the code for the agent:

#-*-Encoding:utf-8-*-

#代理类
class Proxy
 attr_accessor:mm
 
 def Initialize (mm)
  @mm = mm
 End
 
 def give_dolls
  puts "#{mm.name} send you Doll"
 end
 
 def give_flowers
  puts "#{mm.name} send you flowers"
 end
 
 def give_chocolate
  puts "#{mm.name} send you chocolate"
 end

#被追求者类
class Girl
 attr_ Accessor:name
 
 def Initialize (name)
  @name = name
 end

xiao_hong = girl.new (' Little Red ')

xiao_ming = Proxy.new (xiao_hong)
xiao_ming.give_dolls
xiao_ming.give_flowers
xiao_ming.give_ Chocolate

Just replaced the pursuer class with a proxy class.

The actual proxy mode code:

 #-*-encoding:utf-8-*-#公共接口module module Givegift def give_dolls end def Give_ Flowers End def give_chocolate End #追求者类 class Pursuit include Givegift attr_accessor:mm,: Name def Initia Lize (mm) @mm = mm End def give_dolls puts "#{mm.name} for #{name} send you Doll" End def Give_flowers puts "#{mm.name} #{name} send you Flowers "End def Give_chocolate puts" #{mm.name} send you chocolate "end end #代理类 class Proxy include #{name a Ttr_accessor:gg def initialize (mm) @gg = pursuit.new (mm) End def give_dolls Gg.give_dolls End def Give_flo
 
 Wers gg.give_flowers End def give_chocolate gg.give_chocolate End #被追求者类 class Girl Attr_accessor:name Def initialize (name) @name = Name End xiao_hong = girl.new (' Little red ') xiao_ming = Proxy.new (xiao_hong) XIAO_MING.GG.N Ame = ' xiaoming ' xiao_ming.give_dolls xiao_ming.give_flowers xiao_ming.give_chocolate 


decoration mode

Demand:

Give people a different outfit

Code version One

#-*-Encoding:utf-8-*-

class person
 attr_accessor:name
 
 def initialize (name)
  @name = name
 end
 
 def wear_t_shirts
  puts ' big T-shirt ' end
 
 def wear_big_trouser
  puts ' pants ' end
 
 def wear_ Sneakers
  puts ' broken sneakers '
 end
 
 def wear_suit
  puts ' suit '
 end
 
 def wear_tie
  puts ' tie '
 End
 
 def wear_leather_shoes
  puts ' leather shoes ' End
 
 def show
  puts "* * * Dress up #{name}\n\n"
 end

End


xc=person.new (' vegetable ')
puts "the first dress of the Hu Jintao"
xc.wear_t_shirts
xc.wear_big_trouser
Xc.wear_sneakers
xc.show

puts "the second dress of Hu Jintao"
xc.wear_suit
xc.wear_tie
xc.wear_leather_ Shoes
Xc.show

This writing, the function is realized, the problem is that if you increase the "Superman" dress, it is necessary to modify the person class, violating the open-closed principle.

Code version Two

#-*-Encoding:utf-8-*-

class person
 attr_accessor:name
 
 def initialize (name)
  @name = name
 enddef Show
  puts "* * * #{name}\n\n"
 end


class finery
 def

Show End Class Tshirts < finery
 def show
  puts ' big T-shirt ' end

class Bigtrouser < finery
 def show
  puts ' down pants '
 end

class Sneakers < finery
 def show
  puts ' broken sneakers '
end

class Suit < finery
 def show
  puts ' suit '
 end

class Tie < finery
 def show< C35/>puts ' tie '
 end


class Leathershoes < finery
 def show
  puts ' leather shoes '
end


xc=person.new (' vegetable ')
ts = tshirts.new
bt = bigtrouser.new
SK = sneakers.new
puts "Hu Jintao The first kind of dress up "
ts.show
bt.show
sk.show
xc.show


suit = suit.new
tie = tie.new
ls = Leathershoes.new
puts "the second dress of Hu Jintao"
suit.show
tie.show
ls.show
xc.show

After this change, if you add Superman dress, you really do not need to modify the person class. The problem is that all kinds of clothes are independent, and exposed to the outside, is one to wear, no order, no control.

Code version Three

#-*-Encoding:utf-8-*-class Person Attr_accessor:name def initialize (name=nil) @name = Name End def show Puts "* * * #{name}\n\n" End class finery < person attr_accessor:componet def decorate (componet) @com Ponet = Componet End def show Componet.show if Componet End Class Tshirts < finery def Show Super puts
  ' Big T-shirt ' End Class Bigtrouser < finery def show Super puts ' pants ' End-class sneakers < finery def show Super puts ' broken sneakers ' End Class Suit < finery def show Super puts ' suit ' End end Class Tie < finery def s How super puts ' tie ' End Class Leathershoes < finery def show Super puts ' leather shoes ' End xc=person.new (' Small dish ') ts = tshirts.new bt = bigtrouser.new SK = Sneakers.new puts "The first dress up" Ts.decorate xc bt.decorate ts sk.decorate b T sk.show suit = suit.new tie = tie.new ls = leathershoes.new puts "Hu Jintao second Dress" Suit.decorate XC tie.decorate suit of LS.D
 Ecorate BT Ls.show

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.