Download presentation
Presentation is loading. Please wait.
1
Behavioral and Structural Patterns
11/20/2018 Programming Design Patterns
2
Computer Game with saved states
A computer game is installed in one of the high performance desktop machines in a lab The game remembers the score and state of the user’s last play The code has a manager and a game-engine modules The game-engine knows the progress of a game at any given time, but the manager module manages saving/restoring progress of each player Design code so that a player’s progress can be restored the next time (s)he plays the game progress can be stored as score and state (both integers) Programming Design Patterns 11/20/2018
3
Programming Design Patterns
Storing Mementos public class MasterGameObject { // data member to store state Object getCurrentState() { // specific code to gather state } void restoreState(Object savedState) { // specific code for the game to restore state // rest of the game code Programming Design Patterns 11/20/2018
4
Programming Design Patterns
Manager Code Object savedState = masterGameObj.getCurrentState(); masterGameObj.restoreState(savedState) Programming Design Patterns 11/20/2018
5
Programming Design Patterns
Memento Code Notice that the manager just stores the memento object How can you extend this code for checkpointing long-running code? need a series of checkpoints at different stages of the computation Use a stack for a series of checkpoints Programming Design Patterns 11/20/2018
6
Memento Pattern objectives
Encapsulate a checkpoint capability Allow undo to a previous state Allow redo Capture an object’s internal state so that an object can be returned to this state at a later stage Note that Memento is a behavioral pattern Programming Design Patterns 11/20/2018
7
Programming Design Patterns
Java Serialization Used to persist Java objects to a file, database, or a remote location Convert an object into a stream of bytes so that it can be re-created at a later time Serialization does not occur for static or transient fields transient is used for local information such as processID that does not need to be saved Objects to be serialized should implement java.io.Serializable or java.io.Externalizable Whats’ the difference between these two? Not in exam syllabus Serializable objects can be written to a file for persistence Java.io.serializable: if you do not define writeObject(…) then the responsibility is not yours to serialize the object (automatically taken care of). Even if writeObject(..) is defined, serializing the superclass is automatically taken care of Java.io.externalizable: everything is up to the programmer on what is serialized and what is not. Programmer has to write the required code. Programming Design Patterns 11/20/2018
8
Java Serialization Example
import java.io.Serializable; public class Student implements Serializable { // standard setX and getX methods } No method needs to be implemented for implementing the Serializable interface what is this kind of interface called? why is it useful? Tag interface So that Java knows that this object needs to be serialized Programming Design Patterns 11/20/2018
9
Programming Design Patterns
Another Pattern Tag interface So that Java knows that this object needs to be serialized Programming Design Patterns 11/20/2018
10
There is a hierarchy of abstractions
One purpose is to implement the Window system on a variety of platforms (X, MS windows, …) There is a hierarchy of abstract classes describing the components in a platform independent way: Window ApplWindow IconWindow TransientWindow Programming Design Patterns 11/20/2018
11
Programming Design Patterns
Design Problem There are three kinds of Windows application windows, transient windows, icon windows Each of these windows is implemented separately LinuxWindowImpl, AppleWindowImpl, MicroWindowImpl The Window abstraction has a method display The client wants to invoke display() on a transient window and use the AppleWindowImpl Draw an inheritance diagram for the various classes Programming Design Patterns 11/20/2018
12
Subclassing mixes two hierarchies
It is not a clear enough layout if we use subclassing to provide the implementations (here only for X): Window ApplWindow IconWindow TransientWindow WindowXImp MicroWindowXImp AppleWindowXImp LinuxWindowXImp Programming Design Patterns 11/20/2018
13
Programming Design Patterns
Obvious problem The possible need to use multiple inheritance (not available in Smalltalk or Java), … Also, it is not very clear when you have some subclasses being specialized abstractions and other subclasses that provide implementations Programming Design Patterns 11/20/2018
14
Programming Design Patterns
Bridge Pattern Decouple an abstraction from its implementation There is an expectation that the abstraction needs one of several implementations, or that the current implementation will be replaced Using subclassing--abstract parent defines interface, subclass defines implementation--is not satisfactory The Bridge Pattern puts the abstract classes and the implementing classes into separate hierarchies Bridge pattern is a structural pattern Programming Design Patterns 11/20/2018
15
Separate the hierarchies
There can be one hierarchy for implementations There can be one hierarch for window categories has-a Window WindowXImpl AppWindow MacWindowXImp IconWindow LinuxWindowXImp TransientWindow MicroWindowXImp Programming Design Patterns 11/20/2018
16
Programming Design Patterns
Bridge Pattern Code CLASSWORK Abstract Base Class Window void display() Code for Class AppWindow Driver code that displays an AppWindow using the MacWindow’s X implementation Programming Design Patterns 11/20/2018
17
ApplicationWindow uses delegation
public class AppWindow extends Window { private WindowXImpl ximpl; private Coordinates xyCoord; // pass concerete implementation in the constructor AppWindow(Coordinates coordIn, WindowXiml ximplementation) { ximpl = ximpementation; xyCoord = coordIn; } void display() { // delegation to the implementation hierarch ximpl.drawWindow(); Programming Design Patterns 11/20/2018
18
Programming Design Patterns
Driver Code // concrete implementation class WindowXImpl macWindow = new MacWindowXImp(); // code to the abstraction Window window = new AppWindow(xyCoord, macWindow) window.display(); Programming Design Patterns 11/20/2018
19
Relation with other patterns
“The Adapter pattern is geared toward making unrelated classes work together. It is usually applied to systems after they are designed Bridge, on the hand, is used up-front in a design to let abstractions and implementations vary independently” “Abstract Factory can create and configure a particular Bridge” How? HW Programming Design Patterns 11/20/2018
20
Programming Design Patterns
Design Problem Binghamton University has students of different types: grad, undergrad, freshman, junior, .., foreign, domestic, funded, full-time, part-time, etc. Design a class hierarchy using a well-known Design pattern that will be useful to display all the details of any given student. decorator Programming Design Patterns 11/20/2018
21
Programming Design Patterns
Design Problem There are many different shapes (circle, square, …) All shapes can be drawn() There are many different drawing tools, each with a different specialization. Assume they have the same API for drawing a given shape Use a pattern to decouple the following: abstraction of shapes client codes to this abstraction to draw shapes implementations using various tools: gimp, photoshop, etc. bridge Programming Design Patterns 11/20/2018
22
Programming Design Patterns
Design Problem Extend the Iterator pattern, using all design principles learned so far, so that a sub-menu can be added for dessert, breakfast, etc. (support menu within a menu). composite Programming Design Patterns 11/20/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.