Application example analysis of the appearance pattern in Ruby design pattern Programming _ruby Special Topic

Source: Internet
Author: User

What is the appearance mode?

The appearance pattern provides a unified interface for a set of different interfaces in a subsystem. The façade defines the upper interface, making subsystems easier to use by reducing complexity and hiding communication and dependencies between subsystems.

For example, there is a different set of classes in the subsystem, some of which depend on each other. This makes it difficult for the client to use the classes in the subsystem because the client needs to know each class. The appearance is the entrance to the whole subsystem. Some clients only need some basic behavior of the subsystem, but the class of the subsystem does not make much customization, and the appearance provides a simplified interface for such clients. Only clients that need to customize more behavior from the classes of some subsystems will be concerned about the details behind the appearance.

Appearance mode: Provides a unified interface for a set of interfaces in a system. The appearance defines a high-level interface that makes the subsystem easier to use.

When do I use skin mode?

    • Subsystems are becoming increasingly complex. There are many classes in the process of applying patterns that can use skins to provide a simpler interface for these subsystems.
    • You can use skins to tier the system. Each subsystem level has a look as an entry point. Allowing them to communicate through their appearance simplifies their dependencies.

Ruby version appearance mode application
Requirements:

Investors buy and sell stocks

Preliminary code:

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

#股票1
class Stock1
  def
    buy puts ' stock 1 ' end
  
  def sell
    puts ' stock 1 sell ' C8/>end
End

#股票2
class Stock2
  def
    buy puts ' stock 2 buying '
  end
  
  def sell
    puts ' stock 2 sell '
  End

#股票3
class Stock3
  def
    buy puts ' stock 3 ' end
  
  def sell
    puts ' Stock 3 Sell '
  end

#国债1
class NationalDebt1
  def
    buy puts ' Bond 1 ' end
  
  def sell
    puts ' Bond 1 sell '
  end

#房地产1
class Realty1
  def
    buy puts ' real estate 1 '
  
  end def sell
    puts ' real estate 1 sell ' end
s1 = stock1.new
s2 = stock2.new
s3 = stock3.new N1 = Nationaldebt1.new
r1 = realty1.new

s1.buy
s2.buy
s3.buy

n1.buy r1.buy S1.sell
S2.sell
S3.sell
N1.sell
R1.sell

Problem:

Can be found that users need to understand the stock, government bonds, real estate situation, need to participate in the specific purchase and sale of these items, high coupling.

Improved code

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

#股票1
class Stock1
  def
    buy puts ' stock 1 ' end
  
  def sell
    puts ' stock 1 sell ' C8/>end
End

#股票2
class Stock2
  def
    buy puts ' stock 2 buying '
  end
  
  def sell
    puts ' stock 2 sell '
  End

#股票3
class Stock3
  def
    buy puts ' stock 3 ' end
  
  def sell
    puts ' Stock 3 Sell '
  end

#国债1
class NationalDebt1
  def
    buy puts ' Bond 1 ' end
  
  def sell
    puts ' Bond 1 sell '
  end

#房地产1
class Realty1
  def
    buy puts ' real estate 1 '
  
  end def sell
    puts ' real estate 1 sell ' end

#基金类
class Fund attr_accessor s1, S2, S3,
  N1, R1
  
  def Initialize
    s1 = stock1.new
    s2 = stock2.new s3 = stock3.new N1
    = nationaldebt1.new R1
    = Realty1.new
  end
  
  def buy
    s1.buy
    s2.buy
    s3.buy
    n1.buy
    r1.buy
  end
  
  def Sell
    S1.sell
    s2.sell
    s3.sell n1.sell R1.sell end F1

= fund.new
F1.buy
F1.sell

Benefits: Users do not need to know all kinds of stocks, just buy and sell funds.

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.