The Adapter is just like the power adapter such as from 220V to 120V. And when the pin does not fit the socket we need the adapter to fit it.
In software system, we already have a Encrypter to encrypt file:
class Encrypter
def initialize(key)
@key = key
end
def encrypt(reader, writer)
key_index = 0
while not reader.eof?
clear_char = reader.getc
encrypted_char = clear_char ^ @key[key_index]
writer.putc(encrypted_char)
key_index = (key_index + 1) % @key.size
end
end
end
reader = File.open('message.txt')
writer = File.open('message.encrypted','w')
encrypter = Encrypter.new('my secret key')
encrypter.encrypt(reader, writer)
And when we want to encrypt a string and we cannot change the Encrypter, we can use the Adapter Pattern:
class StringIOAdapter
def initialize(string)
@string = string
@position = 0
end
def getc
if @position >= @string.length
raise EOFError
end
ch = @string[@position]
@position += 1
return ch
end
def eof?
return @position >= @string.length
end
end
encrypter = Encrypter.new('XYZZY')
reader= StringIOAdapter.new('We attack at dawn')
writer=File.open('out.txt', 'w')
encrypter.encrypt(reader, writer)
In ruby we can just modify the class or the instance to implement adapter like requirement.
modify class:
require 'british_text_object'
# Now add some methods to the original class
class BritishTextObject
def color
return colour
end
def text
return string
end
def size_inches
return size_mm / 25.4
end
end
modify instance:
bto = BritishTextObject.new('hello', 50.8, :blue)
class << bto
def color
colour
end
def text
string
end
def size_inches
return size_mm/25.4
end
end
If you modify the original class or object, you do not need the additional adapter class, nor do you need to worry about wrapping the adapter around the adaptee. Things just work. And yet the modification technique involves serious encapsulation violations: You just dive in and start changing things.
As usual, a pinch of pragmatism seems best. Lean toward modifying the class in the following circumstances:
• The modifications are simple and clear. The method aliasing we did earlier is a prime example of a simple, crystal-clear modification
• You understand the class you are modifying and the way in which it is used. Performing serious surgery on a class without taking a hard look at the class beforehand is probably going to lead to grief.
Lean toward an adapter solution in the following situations:
• The interface mismatch is extensive and complex. For example, you probably would not want to modify a string to look like a Fixnum object.
• You have no idea how this class works. Ignorance is always cause to tread lightly
The example of Adapter Pattern in Ruby is ActiveRecord database apis:
ActiveRecord has to deal with the fact that it needs to talk to a whole crowd of different database systems: MYSQL and Oracle and Postgres, not to mention SQLServer.
All of these database systems provide a Ruby API—which is good.
But all of the APIs are different—which is bad.
ActiveRecord deals with all of these differences by defining a standardized interface,
encapsulated in a class called AbstractAdapter. The AbstractAdapter class
defines the interface to a database that is used throughout ActiveRecord.
分享到:
相关推荐
Addison.Wesley.Design.Patterns.in.Ruby.Dec.2007 高清PDF英文版
《Design Patterns in Ruby Dec 2007》是关于Ruby编程语言中设计模式的一份珍贵资料,这份2007年发布的PDF文档深入探讨了如何在Ruby语言中应用经典的设计模式。设计模式是软件工程中经过实践证明的有效解决方案模板...
design pattern in C# language
Design Patterns in Modern C++: Reusable Approaches for Object-Oriented Software Design English | PDF| 2018 | 312 Pages | ISBN : 1484236025Design Patterns in Modern C++: Reusable Approaches for Object...
Too often design patterns are explained using tricky concepts, when in fact they are easy to use and can enrich your everyday development. Design Patterns in ...
Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....
Alan Ezust在其著作《An Introduction to Design Patterns in C++ with Qt™, 2nd Edition》中探讨了设计模式的这一概念,并且在Qt框架的基础上讲解了如何在C++项目中应用这些设计模式。 C++是一种高性能、多范式的...
App Architecture: iOS Application Design Patterns in Swift 包含Source code 有钱请支持正版 没钱请默默学习 原书地址: https://www.objc.io/books/app-architecture 中文原书地址: ...
Data Structures And Algorithms With Object-oriented Design Patterns In Java.chm
Implement structural patterns such as adapter, bridge, decorator, facade and more Work with the behavioral patterns such as chain of responsibility, command, iterator, mediator and more Apply ...
Design Patterns in Java(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
and applications that run on various software and hardware platforms with little or no change in the underlying codebase, while still being a native application with native capabilities and speed....
Design Patterns in Modern C++ Reusable Approaches for Object-Oriented Software Design 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
Pro Design Patterns in Swift 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...