Design Patern::Adaptor pattern

11
Adaptor pattern http://jyaasa.com Copyright 2016. Jyaasa Technologies.

Transcript of Design Patern::Adaptor pattern

Page 1: Design Patern::Adaptor pattern

Adaptor pattern

http://jyaasa.comCopyright 2016. Jyaasa Technologies.

Page 2: Design Patern::Adaptor pattern

Hello ! I am Kuber Aaganja

Software EngineerJyaasa Technologies

http://jyaasa.comCopyright 2016. Jyaasa Technologies.

Page 3: Design Patern::Adaptor pattern

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.

Page 4: Design Patern::Adaptor pattern

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.

Page 5: Design Patern::Adaptor pattern

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

Page 6: Design Patern::Adaptor pattern

class BritishTextObject attr_reader :string, :size_mm, :colour

…………..end

Page 7: Design Patern::Adaptor pattern

class BritishTextObjectAdapter < TextObject def initialize(bto)

@bto = bto end

def [email protected] end

def [email protected]_mm / 25.4 end

def [email protected] endend

Page 8: Design Patern::Adaptor pattern

# 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

Page 9: Design Patern::Adaptor pattern

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.

Page 10: Design Patern::Adaptor pattern

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.

Page 11: Design Patern::Adaptor pattern

Thank you!

facebook.com/paasagithub.com/aaganja

http://jyaasa.comCopyright 2016. Jyaasa Technologies.