Ruby uses the proxy mode and decoration mode code instance in the design mode, and the ruby Design Mode

Source: Internet
Author: User

Ruby uses the proxy mode and decoration mode code instance in the design mode, and the ruby Design Mode

Proxy Mode

Requirements:

James asked Xiao Li to chase Xiao Li for him (send dolls, flowers, and chocolate)

No proxy code:

#-*-Encoding: UTF-8-*-# suer class Pursuit attr_accessor: mm def initialize (mm) @ mm = mm end def give_dolls puts "# {mm. name} Show you the doll "end def give_flowers puts" # {mm. name} send you flowers "end def give_chocolate puts" # {mm. name} Send you chocolate "endend # class Girl attr_accessor: name def initialize (name) @ name = name endxiao_hong = Girl. new ('small red') xiao_ming = Pursuit. new (xiao_hong) xiao_ming.give_dollsxiao_ming.give_flowersxiao_ming.give_chocolate

Only the proxy code is available:

#-*-Encoding: UTF-8-*-# Proxy class Proxy attr_accessor: mm def initialize (mm) @ mm = mm end def give_dolls puts "# {mm. name} Show you the doll "end def give_flowers puts" # {mm. name} send you flowers "end def give_chocolate puts" # {mm. name} Send you chocolate "endend # class Girl attr_accessor: name def initialize (name) @ name = name endxiao_hong = Girl. new ('small red') xiao_ming = Proxy. new (xiao_hong) xiao_ming.give_dollsxiao_ming.give_flowersxiao_ming.give_chocolate

I just changed the suer class to the proxy class.

Actual proxy mode code:

#-*-Encoding: UTF-8-*-# modulemodule GiveGift def give_dolls end def give_flowers end def give_chocolate endend # suer class Pursuit include GiveGift attr_accessor: mm ,: name def initialize (mm) @ mm = mm end def give_dolls puts "# {mm. name} For # {name} Send your doll "end def give_flowers puts" # {mm. name} For # {name} to send you flowers "end def give_chocolate puts" # {mm. name} For # {name} to send you chocolate "endend # Proxy 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 endend # class Girl attr_accessor: name def initialize (name) @ name = name endxiao_hong = Girl. new ('small red') xiao_ming = Proxy. new (xiao_hong) xiao_ming.gg.name = 'xiaoming 'xiao _ ming. give_dollsxiao_ming.give_flowersxiao_ming.give_chocolate


Decoration Mode
 
Requirements:

Match different costumes

Code version 1

#-*-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 'underput' end def wear_sneakers puts 'shoes output' end def wear_suit puts 'wear' 'End def wear_leather_shoes puts 'shoes 'end def show puts "***** dress up # {name} \ n" endendxc = Person. new ('food') puts "******* first dress up" xc. wear_t_shirtsxc.wear_big_trouserxc.wear_sneakersxc.showputs "****** type 2 dress up" xc. wear_suitxc.wear_tiexc.wear_leather_shoesxc.show

In this case, the function is implemented. The problem is that if the "Superman" dress is added, the Person class should be modified, in violation of the open-closed principle.

 

Code Version 2

#-*-Encoding: UTF-8-*-class Person attr_accessor: name def initialize (name) @ name = name enddef show puts "***** dress up # {name} \ n" endendendclass Finery def show endclass TShirts <Finery def show puts 'Big T-shirt 'endendendclass bigTrouser <Finery def show puts 'underput' endendclass Sneakers <Finery def show puts 'underwee' endendendclass Suit <Finery def show puts 'suit 'endclass Tie <Finery def show puts 'tie' endendclass LeatherShoes <Finery def show puts 'leather shoes' endendendxc = Person. new ('food') ts = TShirts. newbt = BigTrouser. newsk = Sneakers. newputs "******* first dress up" ts. showbt. showsk. showxc. showsuit = Suit. newtie = Tie. newls = LeatherShoes. newputs "******* type 2 dress up" suit. showtie. showls. showxc. show

After this change, if you add a Superman dress, you do not need to modify the Person class. The problem is that all kinds of clothes are independent and exposed to the outside. They are worn by one piece without order or control.

Code Version 3

#-*-Encoding: UTF-8-*-class Person attr_accessor: name def initialize (name = nil) @ name = name end def show puts "***** dress up # {name} \ n" endendclass Finery <Person attr_accessor: componet def decorate (componet) @ componet = componet end def show componet. show if componet endendclass TShirts <Finery def show super puts 'Big T-shirt 'endendclass BigTrouser <Finery def show super puts 'underput' endendclass Sneakers <Finery def show super puts 'Broken outputs' endendendclass Suit <Finery def show super puts 'suit 'endendclass Tie <Finery def show super puts 'tie 'endendclass LeatherShoes <Finery def show super puts 'shoes 'endendxc = Person. new ('food') ts = TShirts. newbt = BigTrouser. newsk = Sneakers. newputs "******* first dress up" ts. decorate xcbt. decorate tssk. decorate btsk. showsuit = Suit. newtie = Tie. newls = LeatherShoes. newputs "******* type 2 dress up" suit. decorate xctie. decorate suitls. decorate btls. show

Articles you may be interested in:
  • Detailed description of the structure of the Combination Mode and Its Application in Ruby Design Mode Programming
  • The template method in the design mode applies two examples in Ruby.
  • Example parsing: Use of Strategy Mode in Ruby Design Mode Programming
  • The example explains how Ruby uses the decorator mode in the design mode.
  • Example of using Builder mode in Ruby Design Mode Programming
  • Detailed description of the Application of Singleton mode in Ruby Design Mode Programming
  • Programming in Ruby design mode-Introduction to adapter Mode
  • Ruby uses the simple factory mode and factory method mode in the Design Mode
  • Application instance analysis of appearance mode in Ruby Design Mode Programming

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.