Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Objects to Protect Private Details: Memento and Iterator Snarf the iterator code for today’s class."— Presentation transcript:

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

2 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!

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

4 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

5 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

6 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….

7 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

8 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)

9 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

10 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:

11 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)

12 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

13 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)

14 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

15 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

16 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 }

17 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)


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

Similar presentations


Ads by Google