objects to protect private details: memento and iterator snarf the iterator code for today’s class

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

Upload: chloe-holland

Post on 04-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

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

Objects to Protect Private Details:Memento and Iterator

Snarf the iterator code for today’s class.

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

GE Software Engineering Opportunities

CS & ECE Students are youtired 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!

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

Internship

& Co-op Programs

RANKED #3

Rotational Leadership Programs

Direct-Hire

Discover Your Future

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

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

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

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

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

A ProblemMike 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….

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

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

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

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)

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

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

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

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:

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

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)

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

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

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

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)

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

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

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

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

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

Java’s Iterable• If you have a collection class in Java, you can often make it implement the

interface iterable<Type>• 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<MyObject> iterator = myCollection.iterator();while(iterator.hasNext()) {

MyObject current = iterator.next();//do stuff to current

}

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

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)