Adaptor pattern
http://jyaasa.comCopyright 2016. Jyaasa Technologies.
Hello !
I am Kuber Aaganja
Software Engineer
Jyaasa 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.text
size = text_object.size_inches
color = text_object.color
# render the text …
end
end
class TextObject
attr_reader :text, :size_inches, :color
def initialize(text, size_inches, color)
@text = text
@size_inches = size_inches
@color = color
end
end
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
end
end
# Make sure the original class is loaded
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
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/paasa
github.com/aaganja
http://jyaasa.comCopyright 2016. Jyaasa Technologies.

Design Patern::Adaptor pattern

  • 1.
  • 2.
    Hello ! I amKuber Aaganja Software Engineer Jyaasa Technologies http://jyaasa.comCopyright 2016. Jyaasa Technologies.
  • 3.
    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.
  • 4.
    Fig: Class Diagramof 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.
  • 5.
    class Renderer def render(text_object) text= text_object.text size = text_object.size_inches color = text_object.color # render the text … end end class TextObject attr_reader :text, :size_inches, :color def initialize(text, size_inches, color) @text = text @size_inches = size_inches @color = color end end
  • 6.
    class BritishTextObject attr_reader :string,:size_mm, :colour ………….. end
  • 7.
    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 end end
  • 8.
    # Make surethe original class is loaded 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
  • 9.
    When to usemodification? ● 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.
  • 10.
    When to useadaptor 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.
  • 11.