week 9, class 3: model-view-controller today happens-before adapter and façade pattern (high-level)...

17
Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose a pattern Refactor code to meet a design goal Discuss how code/pattern meets a design goal Adapter and Façade Patterns (high-level) Synchronization Friday: Review for Final & Class Climate evaluations SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1

Upload: abigayle-gibson

Post on 14-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

Week 9, Class 3:Model-View-Controller Today

Happens-Before Adapter and Façade Pattern (high-level)

Tuesday: Project code due, 11pm Wednesday: Quiz

Choose a pattern Refactor code to meet a design goal Discuss how code/pattern meets a design goal Adapter and Façade Patterns (high-level) Synchronization

Friday: Review for Final & Class Climate evaluationsSE-2811

Slide design: Dr. Mark L. HornickContent: Dr. Hornick

Errors: Dr. Yoder

1

Page 2: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

The rigormeter is at 2 today

https://www.rose-hulman.edu/~bryan/images/rigor.htmlSE-2811Dr.Yoder 2

Page 3: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

Ex.

public void a() {

System.out.println(“x”);

System.out.println(“y”);

}

public void b() {

System.out.println(“x”);

System.out.println(“y”);

}

SE-2811Dr.Yoder 3

Prove the following code is(n’t) free of data races:

Page 4: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

Ex.

public void a() {

x = 5;

System.out.println(“x”);

}

public void a() {

x = 5;

System.out.println(“x”);

}

SE-2811Dr.Yoder 4

Prove the following code is(n’t) free of data races:

Page 5: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

Ex.

public void a() {

synchronized {

x = 5;

System.out.println(“x”);

}

}

SE-2811Dr.Yoder 5

Prove the following code is(n’t) free of data races:

Page 6: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

Ex.

if(theLogger==null){

synchronized (EventLogger.class){

if(theLogger == null){

theLogger = new EventLogger(path);

}

}

}

SE-2811Dr.Yoder 6

Write whether this code contains any data races. Explain your answer. Assume loggers is not volatile.

Page 7: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

Ex.

if(theLogger==null){

synchronized (EventLogger.class){

if(theLogger == null){

theLogger = new EventLogger(path);

}

}

}

SE-2811Dr.Yoder 7

Write whether this code contains any data races. Explain your answer. Assume loggers is volatile.

Page 8: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

Ex.

if(loggers.get(path)==null){

synchronized (loggers){

if(loggers.get(path) == null){

Logger logger = new EventLogger(path);

loggers.put(path, logger);

}

}

}SE-2811Dr.Yoder 8

Write whether this code contains any data races. Explain your answer. Assume loggers is not thread safe.

Page 9: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

Ex.

if(loggers.get(path)==null){

synchronized (loggers){

if(loggers.get(path) == null){

Logger logger = new EventLogger(path);

loggers.put(path, logger);

}

}

}SE-2811Dr.Yoder 9

Write whether this code contains any data races. Explain your answer. Assume loggers is thread safe.

Page 10: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

SE-2811Slide design: Dr. Mark L. Hornick

Most Content: Dr. HornickSome Content and Most Errors: Dr. Yoder

The Façade Pattern

SE-2811Dr. Mark L. Hornick 10

Page 11: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

Watching a Movie...

Use multiple interfaces (remotes) to Turn on Receiver/amplifier Turn on TV/Monitor Turn on DVD player Set the Receiver input to DVD Put the Monitor in HDMI input mode Set the Receiver volume to medium Set Receiver to DTS Surround Start the DVD player.

Interacting with the following classes:• Receiver/Amplifier• TV/Monitor• DVD• Camera with blue

screen

Page 12: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

To decrease the complexity..

We can create a new class TheaterFacade (e.g. a universal remote) which exposes a few methods such as watchMovie().

The façade treats the various components as a sub system and calls on them to implement the watchMovie method.

So to watch a movie, we just call one method, watchMovie and it communicates with the Monitor, DVD, and Receiver for us.

The façade still leaves the subsystem accessible to be used directly. If you need the advanced functionality of the subsystem classes, they are available for use.

Page 13: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

The Problem

Complex system Consisting of multiple

subsystems Each with its own interface,

each with many methods

Difficult for clients (blue) to deal with

Page 14: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

Facade Solution

Solution Centralize subsystem

interface Simplify/reduce number of

centralized methods Façade presents new

unified “face” to clients Facade

Page 15: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

Removing the burden from beginning Java developers with a Façade (WinPlotter)

SE-2811Dr. Mark L. Hornick 15

class winPlotter

DrawingWindow

- currentPenColor: java.awt.Color- items: java.util.ArrayList- contentPane: java.awt.Container- showGrid: boolean- backgroundColor: java.awt.Color- xinc: int- yinc: int- gridColor: java.awt.Color- drawPanel: DrawPanel

+ DrawingWindow()+ setWindowSize(x :int, y :int) : void+ erase() : void+ setWindowTitle(title :String) : void+ setPenColor(c :Color) : void+ setBackgroundColor(backgroundColor :Color) : void+ drawLineTo(x :int, y :int) : void+ moveTo(x :int, y :int) : void+ drawPointAt(x :int, y :int) : void+ textAt(x :int, y :int, text :String) : void+ setGrid(showGrid :boolean, xinc :int, yinc :int, gridColor :Color) : void+ paint(gr :Graphics) : void

DrawPanel

# paintComponent(g :Graphics) : void+ doPaint(gr :Graphics) : void

DrawItem

+ MOVE: int = 0 {readOnly}+ DRAW: int = 1 {readOnly}+ POINT: int = 2 {readOnly}+ TEXT: int = 3 {readOnly}+ opCode: int+ x: int = 0+ y: int = 0+ text: String+ color: Color

+ DrawItem(x :int, y :int, opCode :int, color :Color)+ DrawItem(x :int, y :int, opCode :int, color :Color, text :String)

FrameWindowConstants

AccessibleRootPaneContainer

TransferHandler.HasGetTransferHandler

swing::JFrame

+ EXIT_ON_CLOSE: int = 3 {readOnly}- defaultLookAndFeelDecoratedKey: Object = new StringBuffe... {readOnly}- defaultCloseOperation: int = HIDE_ON_CLOSE- transferHandler: TransferHandler# rootPane: JRootPane# rootPaneCheckingEnabled: boolean = false# accessibleContext: AccessibleContext = null

+ JFrame()+ JFrame(gc :GraphicsConfiguration)+ JFrame(title :String)+ JFrame(title :String, gc :GraphicsConfiguration)# frameInit() : void# createRootPane() : JRootPane# processWindowEvent(e :WindowEvent) : void+ setDefaultCloseOperation(operation :int) : void+ getDefaultCloseOperation() : int+ setTransferHandler(newHandler :TransferHandler) : void+ getTransferHandler() : TransferHandler+ update(g :Graphics) : void+ setJMenuBar(menubar :JMenuBar) : void+ getJMenuBar() : JMenuBar# isRootPaneCheckingEnabled() : boolean# setRootPaneCheckingEnabled(enabled :boolean) : void# addImpl(comp :Component, constraints :Object, index :int) : void+ remove(comp :Component) : void+ setLayout(manager :LayoutManager) : void+ getRootPane() : JRootPane# setRootPane(root :JRootPane) : void+ setIconImage(image :Image) : void+ getContentPane() : Container+ setContentPane(contentPane :Container) : void+ getLayeredPane() : JLayeredPane+ setLayeredPane(layeredPane :JLayeredPane) : void+ getGlassPane() : Component+ setGlassPane(glassPane :Component) : void+ getGraphics() : Graphics+ repaint(time :long, x :int, y :int, width :int, height :int) : void+ setDefaultLookAndFeelDecorated(defaultLookAndFeelDecorated :boolean) : void+ isDefaultLookAndFeelDecorated() : boolean# paramString() : String+ getAccessibleContext() : AccessibleContext

JComponentAccessible

swing::JPanel

- uiClassID: String = "PanelUI" {readOnly}

+ JPanel(layout :LayoutManager, isDoubleBuffered :boolean)+ JPanel(layout :LayoutManager)+ JPanel(isDoubleBuffered :boolean)+ JPanel()+ updateUI() : void+ getUI() : PanelUI+ setUI(ui :PanelUI) : void+ getUIClassID() : String- writeObject(s :ObjectOutputStream) : void# paramString() : String+ getAccessibleContext() : AccessibleContext

::Lab1App

«facade»WinPlotter

- window: DrawingWindow- width: int- height: int- xmin: int- ymin: int- xmax: int- ymax: int- xscale: float- yscale: float- showGrid: boolean

+ WinPlotter()- logicalToPixel(xlog :int, ylog :int) : Point+ drawTo(x :int, y :int) : void+ erase() : void+ moveTo(x :int, y :int) : void+ drawPoint(x :int, y :int) : void+ printAt(x :int, y :int, text :String) : void+ setBackgroundColor(red :int, green :int, blue :int) : void+ setPenColor(red :int, green :int, blue :int) : void+ setWindowSize(width :int, height :int) : boolean+ setPlotBoundaries(xmin :int, ymin :int, xmax :int, ymax :int) : boolean+ setGrid(showGrid :boolean, xinc :int, yinc :int, gridColor :Color) : boolean+ setWindowTitle(title :String) : void

+items

*

-drawPanel

-window

Page 16: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

Generic Pattern

SE-2811Dr. Mark L. Hornick 16

Page 17: Week 9, Class 3: Model-View-Controller Today Happens-Before Adapter and Façade Pattern (high-level) Tuesday: Project code due, 11pm Wednesday: Quiz Choose

Facade Applications

Interface to existing library Unify or “clean up” complex interface