Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

17
Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone

Transcript of Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Page 1: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Adapter and Façade Patterns

By Wode Ni and Leonard Bacon-Shone

Page 2: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Outline: What we’ll cover

Façade Pattern Motivation

Definition of a Façade

Advantages of Façade Pattern

The principle of least knowledge

Adapter Pattern Motivation

Object Adapter

Class Adapter

Real World Adapter: Enumeration to Iterator

Adapter vs Façade vs Decorator

Implementation of Class adapter: C++ and Java

Page 3: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Façade Pattern: Motivation

Suppose we have a complex system, but we want a sequence of actions to be executed on one keystroke.

Example: a Computer

The procedure of starting a computer:  After the power button is clicked. A series of operation are done by the CPU, RAM, and HDD, and then the operating system is booted.

Do it individually or do it collectively?

Page 4: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

http://www.techspot.com/news/52073-infographic-what-happens-when-your-computer-turns-on.html

Page 5: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Definition of Facade

“A unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.”

Interface - does not only refer to a Java interface.

Real-world example: Compiler.

When you hit compile, or do “javac”, a sequence of instructions got executed in side a complex java compiler.

Page 6: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Advantages of having a Facade

No need to change my way of turning a computer on!

Regardless of the details of the computer: change of CPU, OS, Motherboard etc.

Decouple the client code(pressing the power button) from any one subsystem(Hardware of the computer)!

Code example Still have an option to gain control of individual

components.

You can change the setting for your brand new Razor mouse!

The façade pattern leaves you the access to all the classes

Page 7: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

The principle of least knowledge

Definition: Talk only to your immediate friends

Guidelines: In a method of a class, only invoke methods that belong to

The object that the class itself represents

Objects passed in as a parameter to the method

Any object the method creates or instantiates

Any components of the object

How does the Façade pattern apply this knowledge?

We are not breaking the rules!

Page 8: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Adapter Pattern: Motivation

Existing code incompatible with other code you want to use

To allow existing code to use other code, create an adapter

Imagine a person from Hong Kong trying to charge their phone

Hong Kong people use a different socket to the US

In order to charge their phone, they must use an adapter

Adapter Pattern allows for clients to use existing software to connect to incompatible classes

Page 9: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Adapter Pattern: Definition

“The Adapter Pattern converts the interface of a class into another interface the client expects. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.” (p.243)

Terms explained:

Vendor class = adaptee = the US socket

Client code = Hong Kong plug

Adapter = HK to US converter

Naming Convention in the book: Adaptee’s name + “Adapter”

Plain English: We want the functionality of the adaptee via the language of the target interface, which is not supported by the adaptee.

Page 10: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.
Page 11: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Object Adapter

Object adapter uses composition to store the object of the adaptee and implement the target interface (the interface used by the client)

Because of polymorphism, all the subclasses of the adaptee can be adapted by the object adapter

Imagine a Chinese person traveling to the US, and wanting to speak like an American

Coding Example

Page 12: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Class Adapter

Class adapter uses multiple inheritance

Generally considered not possible in Java

While it is possible to emulate multiple inheritance in Java 8, using the default method in interfaces

Discussed further at the end of the presentation

Class adapters inherit both the client and adaptee classes

Class adapters mean that we don’t have to reimplement the entire interface because of inheritance

Page 13: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Object Adapter

Class Adapter

Page 14: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Enumeration to Iterator

Some legacy code may still use the Enumerator interface, but a client who needs the functionality of the legacy code has an existing system with an Iterator

In order to access the functionality of the legacy code, an adapter is needed

Some methods are very easily adapted, e.g. hasNext() to hasMoreElements()

But what about a method that doesn’t exist in Enumeration, like the remove() method?

The best that can be done about non-existing related methods is throw a runtime exception (fortunately, the Iterator class already does this with UnsupportedOperationException)

Page 15: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Adapter vs Façade vs Decorator

Difference of intents:

Pattern Intent

Adapter Convert one interface to another

Facade Makes an interface simpler

Decorator Doesn’t alter the interface, but adds responsibility

Page 16: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Thank you!

谢谢

AmericanAdapter adapter = new AmericanAdapter(american);adapter.xiexie();

Page 17: Adapter and Façade Patterns By Wode Ni and Leonard Bacon-Shone.

Asked by ~everybody: implementation of class adapter! C++ version:

multiple inheritance. Even two way adapter!

Java approximation: new feature in Java 8 = default methods in interface