java design pattern structural patterns - facade pattern presented by: amit kumar narela ise2007001...

20
JAVA DESIGN PATTERN JAVA DESIGN PATTERN Structural Patterns Structural Patterns - - Facade Pattern Facade Pattern Presented by: Presented by: Amit kumar narela Amit kumar narela Ise2007001<M.tech S.E.> Ise2007001<M.tech S.E.>

Upload: charles-floyd

Post on 19-Jan-2016

231 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

JAVA DESIGN JAVA DESIGN PATTERNPATTERN

Structural PatternsStructural Patterns - - Facade PatternFacade Pattern

Presented by:Presented by:Amit kumar narela Amit kumar narela

Ise2007001<M.tech S.E.>Ise2007001<M.tech S.E.>

Page 2: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

MotivationMotivation

Structuring a system into subsystems helps Structuring a system into subsystems helps reduce complexityreduce complexity

Subsystems are groups of classes, or groups of Subsystems are groups of classes, or groups of classes and other subsystemsclasses and other subsystems

The interface exposed by the classes in a The interface exposed by the classes in a subsystem or set of subsystems can become subsystem or set of subsystems can become quite complexquite complex

One way to reduce this complexity is to One way to reduce this complexity is to introduce a facade object that provides a single, introduce a facade object that provides a single, simplified interface to the more general facilities simplified interface to the more general facilities of a subsystemof a subsystem

Page 3: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

Brief DescriptionBrief Description

>Facade as the name suggests means the >Facade as the name suggests means the face of the building.face of the building.

>It hides the complexities of the system and >It hides the complexities of the system and provides an interface to the client from provides an interface to the client from where the client can access the system.where the client can access the system.

>> The The facade patternfacade pattern is a is a software software engineering design patternengineering design pattern commonly commonly used with Object-oriented programming.used with Object-oriented programming.

Page 4: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

A facade canA facade can:: make a software library easier to use make a software library easier to use

and understand, since the facade has and understand, since the facade has convenient methods for common tasks; convenient methods for common tasks;

make code that uses the library more make code that uses the library more readable, for the same reason; readable, for the same reason;

reduce dependencies of outside code reduce dependencies of outside code on the inner workings of a library, since on the inner workings of a library, since most code uses the facade, thus most code uses the facade, thus allowing more flexibility in developing allowing more flexibility in developing the system; the system;

Page 5: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

IntentIntent

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

Page 6: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

CollaborationsCollaborations

Clients communicate with the subsystem Clients communicate with the subsystem by sending requests to Facade, which by sending requests to Facade, which forwards them to the appropriate forwards them to the appropriate subsystem object(s). Although the subsystem object(s). Although the subsystem objects perform the actual subsystem objects perform the actual work, the facade may have to do work of work, the facade may have to do work of its own to translate its interface to its own to translate its interface to subsystem interfaces. subsystem interfaces.

Clients that use the facade don't have to Clients that use the facade don't have to access its subsystem objects directly access its subsystem objects directly

Page 7: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

Implementation Implementation

Consider the following issues when Consider the following issues when implementing a facadeimplementing a facade

Reducing client-subsystem couplingReducing client-subsystem coupling.. The coupling The coupling between clients and the subsystem can be reduced between clients and the subsystem can be reduced even further by making Facade an abstract class with even further by making Facade an abstract class with concrete subclasses for different implementations of concrete subclasses for different implementations of a subsystem. Then clients can communicate with the a subsystem. Then clients can communicate with the subsystem through the interface of the abstract subsystem through the interface of the abstract Facade class. This abstract coupling keeps clients Facade class. This abstract coupling keeps clients from knowing which implementation of a subsystem from knowing which implementation of a subsystem is used. An alternative to subclassing is to configure is used. An alternative to subclassing is to configure a Facade object with different subsystem objects. To a Facade object with different subsystem objects. To customize the facade, simply replace one or more of customize the facade, simply replace one or more of its subsystem objects. its subsystem objects.

Page 8: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

ApplicabilityApplicability

Use the Facade patternUse the Facade pattern:: To provide a simple interface to a To provide a simple interface to a

complex subsystem. This interface is complex subsystem. This interface is good enough for most clients.good enough for most clients.

To decouple the classes of the To decouple the classes of the subsystem from its clients and other subsystem from its clients and other subsystems.subsystems.

Page 9: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

StructureStructure

Page 10: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

FacadeFacade The facade class interacts Packages 1, The facade class interacts Packages 1,

2, and 3 with the rest of the application.2, and 3 with the rest of the application. ClientsClients

The objects using the Facade Pattern to The objects using the Facade Pattern to access resources from the Packages.access resources from the Packages.

PackagesPackages Software library / API collection Software library / API collection

accessed through the Facade Class.accessed through the Facade Class.

Page 11: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

ConsequencesConsequences

BenefitsBenefits::

It hides the implementation of the subsystem from clients, It hides the implementation of the subsystem from clients, making the subsystem easier to use.making the subsystem easier to use.

It promotes weak coupling between the subsystem and its It promotes weak coupling between the subsystem and its clients. This allows you to change the classes the comprise clients. This allows you to change the classes the comprise the subsystem without affecting the clients.the subsystem without affecting the clients.

It reduces compilation dependencies in large software It reduces compilation dependencies in large software systems.systems.

It simplifies porting systems to other platforms, because it's It simplifies porting systems to other platforms, because it's less likely that building one subsystem requires building all less likely that building one subsystem requires building all others.others.

It does not prevent sophisticated clients from accessing the It does not prevent sophisticated clients from accessing the underlying classesunderlying classes

Facade does not add any functionality, it just simplifies Facade does not add any functionality, it just simplifies interfacesinterfaces

Page 12: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

Example: Home Theater System

Imagine a home theater system represented as a bunch of objects

> You might have objects like Amplifier, tuner, DVDPlayer, Projector, CDPlayer,

TheaterLights, Screen,and PopcornPopper

>To watch a DVD, you might have to: Turn the popcorn popper on Start making popcorn Dim the lights Put the screen down Turn the projector on Set the projector input to DVD

Page 13: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001
Page 14: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001
Page 15: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001
Page 16: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

public class HomeTheaterTestDrive {public class HomeTheaterTestDrive {public static void main(String[] args) {public static void main(String[] args) {

Amplifier amp = new Amplifier("Top-O-Line Amplifier");Amplifier amp = new Amplifier("Top-O-Line Amplifier");Tuner tuner = new Tuner("Top-O-Line AM/FM Tuner", amp);Tuner tuner = new Tuner("Top-O-Line AM/FM Tuner", amp);DvdPlayer dvd = new DvdPlayer("Top-O-Line DVD Player", DvdPlayer dvd = new DvdPlayer("Top-O-Line DVD Player",

amp);amp);CdPlayer cd = new CdPlayer("Top-O-Line CD Player", amp);CdPlayer cd = new CdPlayer("Top-O-Line CD Player", amp);Projector projector = new Projector("Top-O-Line Projector", Projector projector = new Projector("Top-O-Line Projector",

dvd);dvd);TheaterLights lights = new TheaterLights("Theater Ceiling TheaterLights lights = new TheaterLights("Theater Ceiling

Lights");Lights");Screen screen = new Screen("Theater Screen");Screen screen = new Screen("Theater Screen");PopcornPopper popper = new PopcornPopper("Popcorn PopcornPopper popper = new PopcornPopper("Popcorn

Popper");Popper");

HomeTheaterFacade homeTheater = HomeTheaterFacade homeTheater = new HomeTheaterFacade(amp, tuner, dvd, cd, new HomeTheaterFacade(amp, tuner, dvd, cd,

projector, screen, lights, popper);projector, screen, lights, popper);

homeTheater.watchMovie("Raiders of the Lost Ark");homeTheater.watchMovie("Raiders of the Lost Ark");homeTheater.endMovie();homeTheater.endMovie();

}}}}

Page 17: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

OUTPUTOUTPUT Get ready to watch a movie...Get ready to watch a movie... Popcorn Popper onPopcorn Popper on Popcorn Popper popping popcorn!Popcorn Popper popping popcorn! Theater Ceiling Lights dimming to 10%Theater Ceiling Lights dimming to 10% Theater Screen going downTheater Screen going down Top-O-Line Projector onTop-O-Line Projector on Top-O-Line Projector in widescreen mode (16x9 aspect ratio)Top-O-Line Projector in widescreen mode (16x9 aspect ratio) Top-O-Line Amplifier onTop-O-Line Amplifier on Top-O-Line Amplifier setting DVD player to Top-O-Line DVD PlayerTop-O-Line Amplifier setting DVD player to Top-O-Line DVD Player Top-O-Line Amplifier surround sound on (5 speakers, 1 subwoofer)Top-O-Line Amplifier surround sound on (5 speakers, 1 subwoofer) Top-O-Line Amplifier setting volume to 5Top-O-Line Amplifier setting volume to 5 Top-O-Line DVD Player onTop-O-Line DVD Player on Top-O-Line DVD Player playing "Raiders of the Lost Ark"Top-O-Line DVD Player playing "Raiders of the Lost Ark" Shutting movie theater down...Shutting movie theater down... Popcorn Popper offPopcorn Popper off Theater Ceiling Lights onTheater Ceiling Lights on Theater Screen going upTheater Screen going up Top-O-Line Projector offTop-O-Line Projector off Top-O-Line Amplifier offTop-O-Line Amplifier off Top-O-Line DVD Player stopped "Raiders of the Lost Ark"Top-O-Line DVD Player stopped "Raiders of the Lost Ark" Top-O-Line DVD Player ejectTop-O-Line DVD Player eject Top-O-Line DVD Player offTop-O-Line DVD Player off

Page 18: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

Related PatternsRelated Patterns

Abstract Factory can be used with Abstract Factory can be used with Facade to provide an interface for Facade to provide an interface for creating subsystem objects in a creating subsystem objects in a subsystem-independent way.subsystem-independent way.

Mediator is similar to Facade in that Mediator is similar to Facade in that it abstracts functionality of existing it abstracts functionality of existing classes. However, Mediator's purpose classes. However, Mediator's purpose is to abstract arbitrary is to abstract arbitrary communication between colleague communication between colleague objects objects

Page 19: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001

QUESTIONS??QUESTIONS????

Page 20: JAVA DESIGN PATTERN Structural Patterns - Facade Pattern Presented by: Amit kumar narela Ise2007001 Ise2007001