design patern::adaptor pattern
Post on 16-Feb-2017
61 Views
Preview:
TRANSCRIPT
Adaptor pattern
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
Hello ! I am Kuber Aaganja
Software EngineerJyaasa Technologies
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
Definition:In software engineering, the adapter pattern is a software design pattern that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.
An adapter helps two incompatible interfaces to work together. This is the real world definition for an adapter. Interfaces may be incompatible but the inner functionality should suit the need. The Adapter design pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.
Fig: Class Diagram of adaptor pattern
What this diagram is saying is that the client knows about some target class—as a client, we have a reference to our target object. The client expects the target to have a certain interface. But unknown to the client, the target object is really an adapter, and buried inside of the adapter is a reference to a second object, the adaptee, which actually per- forms the work. Perhaps in a perfect world all interfaces would line up perfectly and the client would talk directly to the adaptee. In the real world, however, we need to build adapters because the interface that the client is expecting is not the interface that the adaptee is offering.
class Renderer def render(text_object)
text = text_object.textsize = text_object.size_inchescolor = text_object.color# render the text …
endend
class TextObject attr_reader :text, :size_inches, :color def initialize(text, size_inches, color)
@text = text@size_inches = size_inches@color = color
endend
class BritishTextObject attr_reader :string, :size_mm, :colour
…………..end
class BritishTextObjectAdapter < TextObject def initialize(bto)
@bto = bto end
def text@bto.string end
def size_inches@bto.size_mm / 25.4 end
def color@bto.colour endend
# Make sure the original class is loadedrequire 'british_text_object'
# Now add some methods to the original classclass BritishTextObject def color
return colour end
def textreturn string end
def size_inchesreturn size_mm / 25.4
end
end
When to use modification?
● The modifications are simple and clear.● 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.
When to use adaptor pattern?
● 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.
Thank you!
facebook.com/paasagithub.com/aaganja
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
top related