Application instance analysis of appearance mode in Ruby Design Mode Programming, and ruby Design Mode
What is the appearance mode?
The appearance mode provides unified interfaces for a group of different interfaces in the subsystem. The upper-layer interfaces are defined. by reducing complexity and hiding the communication and dependency between subsystems, the subsystems are easier to use.
For example, subsystems have different classes, some of which depend on each other. This makes it difficult for the client to use classes in subsystems, because the client needs to know each class. The appearance serves as the entrance to the entire subsystem. Some clients only need some basic behaviors of subsystems, but do not have too many customization for subsystem classes. The appearance of these clients provides simplified interfaces. Only clients that need to customize more behaviors from the classes of Some subsystems will pay attention to the details behind the appearance.
Appearance mode: provides a unified interface for a group of interfaces in the system. The appearance defines a high-level interface to make the subsystem easier to use.
When do I use the appearance mode?
- Subsystems are becoming increasingly complex. Many classes are produced during the application mode process. You can use the appearance to provide a simple interface for these subsystems.
- You can use the appearance to layer the subsystem. Each subsystem level has an appearance as the entry point. Allowing them to communicate with each other through their appearance can simplify their dependencies.
Ruby exterior mode Application
Requirements:
Investors buy and sell stocks
Preliminary code:
#-*-encoding: utf-8-*-
# Stock1
class Stock1
def buy
puts 'Buy 1'
end
def sell
puts 'Stock 1 sold'
end
end
#Stock 2
class Stock2
def buy
puts 'Buy 2'
end
def sell
puts 'Sell 2'
end
end
#Stock 3
class Stock3
def buy
puts 'Buy 3'
end
def sell
puts 'Sell 3'
end
end
# 国 贷 1
class NationalDebt1
def buy
puts 'Buy Treasury 1'
end
def sell
puts 'Treasury 1 sold'
end
end
#Real estate 1
class Realty1
def buy
puts 'Buy 1 Real Estate'
end
def sell
puts 'Real estate 1 sold'
end
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:
It can be found that users need to know about stocks, government bonds, and real estate. They need to participate in the specific purchase and sale of these projects, which is highly coupled.
Code Improvement
#-*-encoding: utf-8-*-
# Stock1
class Stock1
def buy
puts 'Buy 1'
end
def sell
puts 'Stock 1 sold'
end
end
#Stock 2
class Stock2
def buy
puts 'Buy 2'
end
def sell
puts 'Sell 2'
end
end
#Stock 3
class Stock3
def buy
puts 'Buy 3'
end
def sell
puts 'Sell 3'
end
end
# 国 贷 1
class NationalDebt1
def buy
puts 'Buy Treasury 1'
end
def sell
puts 'Treasury 1 sold'
end
end
#Real estate 1
class Realty1
def buy
puts 'Buy 1 Real Estate'
end
def sell
puts 'Real estate 1 sold'
end
end
# Fund category
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
end
f1 = Fund.new
f1.buy
f1.sell
Benefit: You don't need to know about all kinds of stocks, you just need to buy and sell funds.
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 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