ooad 2 interfaces and the facade pattern

Post on 23-Dec-2014

1.284 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

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

OOAD 2Interfaces 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

>> 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();

>> 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

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

Facade pattern

• Takes a complex subsystem and makes it a simpler one

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

Subsystem

Class 1Class 1

Class 5Class 5

Class 4Class 4

Class 3Class 3

Class 6Class 6

Class 2Class 2

>> 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

>> 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

>> 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

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

>> 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

>> 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”

top related