Behavioral and Structural Patterns

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

CSC 243 – Java Programming, Spring 2013 March 26, 2013 Week 8, java.lang.Clonable & java.io.Serializable Interfaces.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Bridge Pattern.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 8, Object Design Introduction to Design Patterns
Chapter 8 Object Design Reuse and Patterns. Finding Objects The hardest problems in object-oriented system development are: –Identifying objects –Decomposing.
Design Pattern – Bridge (Structural) References Yih-shoung Chen, Department of Information Engineering, Feng Chia University,Taiwan, R.O.C. The Bridge.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
More OOP Design Patterns
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Object Oriented Programming
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
1 CSE 331 Memento Pattern and Serialization slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
1/23/2018 Design Patterns David Talby.
Unit II-Chapter No. : 5- design Patterns
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Final and Abstract Classes
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
Java Beans Sagun Dhakhwa.
Observer Design Pattern
Chapter 8, Design Patterns Bridge
Week 4 Object-Oriented Programming (1): Inheritance
object oriented Principles of software design
Object-Oriented Programming
Software Engineering Lecture 7 - Design Patterns
Object-Oriented Programming
Informatics 122 Software Design II
Week 6 Object-Oriented Programming (2): Polymorphism
Object Oriented Design Patterns - Structural Patterns
Interfaces.
Review: Design Pattern Structure
BRIDGE PATTERN.
Structural Patterns: Adapter and Bridge
Lesson 5: More on Creational Patterns
Constructors, GUI’s(Using Swing) and ActionListner
Chapter 14 Abstract Classes and Interfaces
Informatics 122 Software Design II
11. MVC SE2811 Software Component Design
Object-Oriented PHP (1)
Final and Abstract Classes
11. MVC SE2811 Software Component Design
Software Design Lecture : 28.
Presentation transcript:

Behavioral and Structural Patterns 11/20/2018 Programming Design Patterns

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

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

Programming Design Patterns Manager Code Object savedState = masterGameObj.getCurrentState(); masterGameObj.restoreState(savedState) Programming Design Patterns 11/20/2018

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

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

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

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

Programming Design Patterns Another Pattern Tag interface So that Java knows that this object needs to be serialized Programming Design Patterns 11/20/2018

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

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

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

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

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

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

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

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

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

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

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

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

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