Objects to Protect Private Details: Memento and Iterator Snarf the iterator code for today’s class.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Chapter 8 Improving Structure with Inheritance. The DoME Example The Database of Multimedia Entertainment We will be storing information about CDs and.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Chapter 25 GRASP: More Objects with Responsibilities 1CS6359 Fall 2011 John Cole.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 CMSC 132: Object-Oriented Programming II Software Development III Department of Computer Science University of Maryland, College Park.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
CSE 250: Data Structures Week 3 January 28 – February 1, 2008.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Generics, Proxy, and The Compile Time Type Checking Debate You are either with us or against us. Please snarf the code for today’s class.
Iterators CS 367 – Introduction to Data Structures.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Lecture 18 Page 1 CS 111 Online Design Principles for Secure Systems Economy Complete mediation Open design Separation of privileges Least privilege Least.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Computing Science 1P Large Group Tutorial 17 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Nachos Phase 1 Code -Hints and Comments
Object Oriented Programming Elhanan Borenstein Lecture #4.
Programming using C# Joins SQL Injection Stored Procedures
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
16-Oct-15 JSP Implicit Objects. 2 JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Introduction to Software Architecture.
1 CS161 Introduction to Computer Science Topic #9.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns Introduction
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Introduction to Objects and Encapsulation Computer Science 4 Mr. Gerb Reference: Objective: Understand Encapsulation and abstract data types.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
The Memento Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Motivation for Generic Programming in C++
Data Abstraction: The Walls
Jim Fawcett CSE776 – Design Patterns Summer 2005
Review What is an object? What is a class?
Modern Collections Classes
Chapter 4: Writing Classes
Menu item at a restaurant
Modern Collections Classes
CS2013 Lecture 7 John Hurley Cal State LA.
Presentation transcript:

Objects to Protect Private Details: Memento and Iterator Snarf the iterator code for today’s class.

GE Software Engineering Opportunities CS & ECE Students are you tired of playing with toys? Put your imagination to work on something bigger… Want to learn more about what GE has to offer you? Join us at the E-Social TODAY Bring Your Resume!

Internship & Co-op Programs RANKED #3 Rotational Leadership Programs Direct-Hire Discover Your Future

Internships & Co-ops 3-month summer internships 6-month semester co-op Hands-on engineering and business experience Key path to full-time positions upon graduation

Edison Engineering Development Program (EEDP) 2-3 year technical program 6-12 month long rotations in engineering Intensive technical training Tuition reimbursement for graduate degrees Corporate leadership training

A Problem Mike is building a cool SecretAgent object. He wants the object to be savable in a variety of different forms (e.g. in the cloud, on the local filesystem, etc.). Mike really wants to keep code that deals with data storage out of the SecretAgent class. So he makes a new set of classes to deal with writing SecretAgents to files. But there’s a problem….

We Don’t We Just Add a getRealName() function? It violates the encapsulation we want for the secret agent object Encapsulation: keeping data that we don’t want external folks to know about out of the public interface It’s not about security - it’s about expressing your intentions clearly to the folks that use your classes

We could add a getEncodedString() function The encoded string stores gets all the private data of SecretAgent encoded in a particular way Then we add a new constructor (or Factory Method) to SecretAgent that takes an encoded string as a parameter and recreates the object from the string Still slightly problematical: what if someone starts pulling data out of the encodedString? (again, we’re concerned about mistakes not malicious programmers)

Memento: A Solution Change getEncodedString() to getMemento() – a function that returns a MementoObject The Memento class has no public methods (or at least it seems that way) Now it is more obvious that no one except special folks are intended to get data out of this object Our special constructor (or factory method) now takes the Memento – making it super explicit how the Memento is used to reconstruct the object

How to actually get data out of Memento SecretAgentWriter needs to get data out of the memento. How can it do that if it has no public methods? C++ has a special friend keyword that will let you give access to your private data to certain other classes In Java, there is no official way but you can do tricks like this:

A Problem: Adding undo functionality to ArgoUML ArgoUML has a UMLDrawing with many private members. Your thought is that you will add a new method getState() to the UMLDrawing that returns a memento. Every time a change is made you’ll call getState on the drawing and save the state. Then if the user ever wants to restore the state you can use the memento to do that. How would you use Memento to do all this? What new functions would you need to add to UMLDrawing (I needed to add 2). What new classes/interfaces would you need to add (I needed to add 2)

ArgoUML I added to UML Drawing: – public Memento getState(); – public restoreState(Memento oldState); Class/Interface wise I added: – The interface Memento which has no methods – The private inner class UMLDrawingMemento (implements Memento) which has all sorts of methods to get at that stored state

The Key Idea The memento object encapsulates the private data of a different object The memento can be passed around safely and gives other objects new functionality (in particular the ability to store the state of this object…even if they don’t know what that state is)

Another Problem I’m writing a LinkedList class Oftentimes, in algorithms that use linked lists you want to be looking at several different parts of the linked list at once I don’t want to expose to my clients that my LinkedList class consists of ListNodes (because I might want to change that in the future) BUT I do want my clients to be able to write efficient algorithms that look at several parts of the linked list at once

Iterator Encapsulates the idea or being in a specific place in a data structure Often has methods hasNext and next (which gets the next, and modifies the state) while(iterator.hasNext()) { MyObject current = iterator.next(); //do stuff to current } Sometimes also has methods like remove() or add() which add/remove at the current position

Java’s Iterable If you have a collection class in Java, you can often make it implement the interface iterable You have to implement a method iterator() If you do, you can use your collection like this: for(MyObject current : myCollection) { //do stuff to current } Equvalent to: Iterator iterator = myCollection.iterator(); while(iterator.hasNext()) { MyObject current = iterator.next(); //do stuff to current }

Iterator Problem You’ve got a class StringList which contains a list of strings, which you want to allow folks to iterate over (in the order the strings were added, just like a normal list) The string list might contain duplicates, but when you iterate you want to only visit each unique string once (that is, you want to ignore duplicates)