Example of using Builder mode in Ruby Design Mode Programming, rubybuilder

Source: Internet
Author: User

Example of using Builder mode in Ruby Design Mode Programming, rubybuilder

Let's first review the basic concepts of the design model:
Definition
Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
The builder hides how the product is assembled, so if you need to change the internal representation of a product, you only need to redefine a builder.
Applicability
1. When creating complex objects, algorithms should be independent of the components of the objects and their assembly methods.
2. When the constructor must allow different representations of the constructed object.
Role
In such a design model, there are several roles:
1. builder: specify an abstract interface for each part of a product object.
2. ConcreteBuilder: implements the Builder interface to construct and assemble each part of the product, define and define the representation it creates, and provide an interface for retrieving the product.
3. Director: Construct an object using the Builder interface.
4. Product: a complex object to be constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process, including the class that defines the components, including the interfaces that assemble these components into the final product.
Let's look at an example:
Requirements:

Draw a villain with a head and a body. Just put both hands on your feet.

Initial code:

#-*-Encoding: UTF-8-*-# The first puuts: the slimming puts: put both hands 'put' for a person: feet '# puts put' this is the second villain 'puput' for a person: First 'puput' for a person: fat body 'put' for a person: put both hands 'put' Second: Dual feet'

Problem: the code is repeated and prone to errors.

Improved code:

#-*-Encoding: UTF-8-*-# class PersionBuilder def head puts ''end def body puts ''end def arm puts ''end def leg puts ''endend # class PersionThinBuilder <PersionBuilder def head puts 'head 'end def body puts 'slimming body' end def arm puts 'hands 'end def leg puts 'feet 'endend # Second class PersionFatBuilder <PersionBuilder def head puts 'head' end def body puts 'fat body' end def arm puts 'Hands 'end def leg puts' feet 'endend # conductor class PersionDirect def initialize (persion) @ persion = persion end def create_persion @ persion. head @ persion. body @ persion. arm @ persion. leg endendputs 'this is the first villain 'p1 = PersionThinBuilder. newpd1 = PersionDirect. new (p1) pd1.create _ persionputs 'This is the second villain 'p2 = PersionThinBuilder. newpd2 = PersionDirect. new (p2) pd2.create _ persion

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.
  • Detailed description of the Application of Singleton mode in Ruby Design Mode Programming
  • Programming in Ruby design mode-Introduction to adapter Mode
  • Ruby uses code instances in the proxy mode and decoration mode in the Design 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.