Top Banner
>> 0 >> 1 >> 2 >> 3 >> 4 >> OOAD 2 Interfaces and the Facade pattern
12
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Ooad 2   Interfaces And The Facade Pattern

>> 0 >> 1 >> 2 >> 3 >> 4 >>

OOAD 2Interfaces and the Facade pattern

Page 2: Ooad 2   Interfaces And The Facade Pattern

>> 0 >> 1 >> 2 >> 3 >> 4 >>

A bit about interfaces

• “Always Program to an Interface”

• We don’t necessarily mean a Java-style Interface language construct

• I guess you could simplify it to:– Always program to a supertype

Page 3: Ooad 2   Interfaces And The Facade Pattern

>> 0 >> 1 >> 2 >> 3 >> 4 >>

“Always Program to an Interface”

To an implementation:

Dog d = new Dog();

d.bark();

To an interface:

Animal a = getAnimal(“dog”);

a.makeSound();

Page 4: Ooad 2   Interfaces And The Facade Pattern

>> 0 >> 1 >> 2 >> 3 >> 4 >>

Advantages?

• We’re not worrying about implementations

• Can deal with things generically (more abstraction), reducing code

• Greater resilience to change

• We’ll see more advantages as we cover more patterns in the coming weeks

Page 5: Ooad 2   Interfaces And The Facade Pattern

>> 0 >> 1 >> 2 >> 3 >> 4 >>

Facade pattern

• Takes a complex subsystem and makes it a simpler one

Page 6: Ooad 2   Interfaces And The Facade Pattern

>> 0 >> 1 >> 2 >> 3 >> 4 >>

Subsystem

Class 1Class 1

Class 5Class 5

Class 4Class 4

Class 3Class 3

Class 6Class 6

Class 2Class 2

Page 7: Ooad 2   Interfaces And The Facade Pattern

>> 0 >> 1 >> 2 >> 3 >> 4 >>

Subsystem

Class 1Class 1

Class 5Class 5

Class 4Class 4

Class 3Class 3

Class 6Class 6

Class 2Class 2

Client classClient class

Page 8: Ooad 2   Interfaces And The Facade Pattern

>> 0 >> 1 >> 2 >> 3 >> 4 >>

Subsystem

Class 1Class 1

Class 5Class 5

Class 4Class 4

Class 3Class 3

Class 6Class 6

Class 2Class 2

FacadeFacadeClient classClient class

Page 9: Ooad 2   Interfaces And The Facade Pattern

>> 0 >> 1 >> 2 >> 3 >> 4 >>

Subsystem

Class 1Class 1

Class 5Class 5

Class 4Class 4

Class 3Class 3

Class 6Class 6

Class 2Class 2

FacadeFacadeClient classClient class Client class 2Client class 2

Page 10: Ooad 2   Interfaces And The Facade Pattern

>> 0 >> 1 >> 2 >> 3 >> 4 >>

Page 11: Ooad 2   Interfaces And The Facade Pattern

>> 0 >> 1 >> 2 >> 3 >> 4 >>

Facade pattern summary

• Takes a complex subsystem and makes it a simpler one

• You can still directly access the underlying classes

• Example: home theatre controller– Simple, unified interface– Most common functions in one place– You can still use the individual controllers

Page 12: Ooad 2   Interfaces And The Facade Pattern

>> 0 >> 1 >> 2 >> 3 >> 4 >>

When to use it?

• To provide an easy interface to lots of subsystems

• Principle of Least Knowledge – reduce interactions between objects– Reduces the amount of “spaghetti”