YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
  • 1. Future Endeavors in Automated Refactoring of Legacy Java Software to Enumerated Types* Raffi Khatchadourian, Jason Sawin, and Atanas Rountev PRESTO: Program Analyses and Software Tools Research Group, Ohio State University * Work supported in part by NSF

2. Motivation 2 3. Motivation Software changes over time: 2 4. Motivation Software changes over time: Requirements evolve 2 5. Motivation Software changes over time: Requirements evolve Different platforms (e.g., mobile devices) 2 6. Motivation Software changes over time: Requirements evolve Different platforms (e.g., mobile devices) New framework versions (e.g., XML vs. annotation-based) 2 7. Motivation 8. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: 9. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: Tedious:May require changing many lines of code. 10. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: Tedious:May require changing many lines of code. Adding a parameter to a method 11. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: Tedious:May require changing many lines of code. Error-prone: Changes may be implemented incorrectly. 12. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: Tedious:May require changing many lines of code. Error-prone: Changes may be implemented incorrectly. Removing a method parameter may alter overloading to overriding 13. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: Tedious:May require changing many lines of code. Error-prone: Changes may be implemented incorrectly. Omission- prone: May opportunities to produce better code. 14. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: Tedious:May require changing many lines of code. Error-prone: Changes may be implemented incorrectly. Omission- prone: May opportunities to produce better code. HashTable vs. HashMap 15. Motivation Changing and/or maintaining large, complex software systems can be non-trivial: Tedious:May require changing many lines of code. Error-prone: Changes may be implemented incorrectly. Omission- prone: May opportunities to produce better code. 16. Solution? 17. Solution? Approaches made to provide mechanical assistance in evolution tasks. 18. Solution? Approaches made to provide assistance in evolution tasks. Typically in the form of plug-ins to IDEs. 19. Solution? Approaches made to provide assistance in evolution tasks. Typically in the form of plug-ins to IDEs. Ease the burden of software maintenance and evolution. 20. Solution? Approaches made to provide assistance in evolution tasks. Typically in the form of plug-ins to IDEs. Ease the burden of software maintenance and evolution. Restrict workspace to only displays elements relevant to the task 21. Solution? Approaches made to provide assistance in evolution tasks. Typically in the form of plug-ins to IDEs. Ease the burden of software maintenance and evolution. Restrict workspace to only displays elements relevant to the task Restructure code while preserving semantics (i.e., refactoring) 22. Introduction 23. Introduction Java 5 introduced a rich set of new features such as generics, metadata annotations, boxing/unboxing, and type-safe enumerations. 24. Introduction Java 5 introduced a rich set of new features such as generics, metadata annotations, boxing/unboxing, and Highlight an automated semantics-preserving approach for migrating legacy Java code to take advantage of the new language enumerated type constructs. 25. Introduction Java 5 introduced a rich set of new features such as generics, metadata annotations, boxing/unboxing, and Highlight an automated semantics-preserving approach for migrating legacy Java code to take advantage of the new language enumerated type constructs. Present experimental results from research prototype as an Eclipse IDE plug-in. 26. Introduction Java 5 introduced a rich set of new features such as generics, metadata annotations, boxing/unboxing, and Highlight an automated semantics-preserving approach for migrating legacy Java code to take advantage of the new language enumerated type constructs. Present experimental results from research prototype as an Eclipse IDE plug-in. In progress to be included with the standard distribution of Eclipse. 27. Introduction Java 5 introduced a rich set of new features such as generics, metadata annotations, boxing/unboxing, and Highlight an automated semantics-preserving approach for migrating legacy Java code to take advantage of the new language enumerated type constructs. Present experimental results from research prototype as an Eclipse IDE plug-in. In progress to be included with the standard distribution of Eclipse. Discuss directions for future work. 28. Motivating Example class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; 29. Motivating Example Weak Enum Pattern class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; 30. Motivating Example Type Safety class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; 31. Motivating Example class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Manual Enumeration 32. Motivating Example class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Namespacing 33. Motivating Example class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Brittle 34. Motivating Example class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; 35. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; 36. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; 37. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; 38. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; 39. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; 40. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Language Enum 41. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Type Safety 42. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Singletons in Natural Order 43. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Prexed 44. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; Supports Separate Compilation 45. Motivating Example Revisited class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; private static final int MAX_SPEED = 140; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public enum Color {RED, YELLOW, GREEN}; /* Current color of the trafc signal, initially red by default */ private Color color = Color.RED; /* Accessor for the lights current color */ public Color getColor() {return this.color;}} class Automobile { private enum Action {IDLE, INCREASE_SPEED, DECREASE_SPEED, STOP}; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; class TrafficSignal { public static final int RED = 0; public static final int YELLOW = 1; public static final int GREEN = 2; / Current color of the trafc signal, initially red by default / private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; 46. Traffic Signal Client / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) return INCREASE_SPEED; else return STOP; case TrafficSignal.GREEN: // no change return this.currentAction; default: throw new IllegalArgumentException ("Invalid traffic color");}} // required public void drive() { TrafficSignal aSignal = ... ; int reaction = this.react(aSignal); private int color = RED; / Accessor for the lights current color / public int getColor() {return this.color;}} class Automobile { private static final int IDLE = 0; private static final int INCREASE_SPEED = 1; private static final int DECREASE_SPEED = 2; private static final int STOP = 3; private static final int MAX_SPEED = 140; / The action this automobile is currently performing, idle by default / private int currentAction = IDLE; / The current speed of the automobile, initially 5 mph. / private int currentSpeed = 5; private int react(TrafficSignal signal) { switch(signal.getColor()) { case TrafficSignal.RED: return STOP; case TrafficSignal.YELLOW: // decide whether to stop or go if (this.shouldGo()) this.currentAction && INCREASE_SPEED || tSpeed


Related Documents