Download presentation
Presentation is loading. Please wait.
Published byAllyson Brigman Modified over 9 years ago
1
SE2811 Week 8 Monday (last drop day) The Command Pattern Lambda Expressions SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1
2
The Super Remote Control Reminder: At its simplest, the command pattern implements a “closure” An anonymous method implementation with associated data (Unlike a closure, there may be multiple methods associated with a command – e.g. undo and redo) Using inner classes Using anonymous inner classes… SE-2811 Dr. Josiah Yoder 2
3
SE-2811 Dr. Mark L. Hornick 3 Command Pattern Example
4
Exercise: Command and Anonymous Inner Classes Write an anonymous inner class that implements the Command interface. You may assume whatever context for the anonymous inner class that you desire. SE-2811 Dr. Mark L. Hornick 4
5
Inner Classes and Lambda Expressions Reminder: What type the anon. inner class should have is determined from context SE-2811 Dr. Mark L. Hornick 5
6
Lambda Expression Syntax Exercise: What parts does a regular method have? Lambda Expression: A variety of syntaxes are available: -> (Scanner in) -> {int x = in.nextInt(); return x+1;} (int x) -> x+1 () -> System.out.println(“hi”) x -> x+1 SE-2811 Dr. Mark L. Hornick 6
7
Exercise Rewrite Car’s getStartCommand method to use a lambda expression instead of an anonymous inner class. SE-2811 Dr. Mark L. Hornick 7
8
Recording Macros The Command pattern is useful for playing back user input. To do this, each command executed by the user is also saved for playback. Then, the commands are restored and played back. SE-2811 Dr. Mark L. Hornick 8
9
Exercise: What data structure(s) would you need to implement command playback? How would you implement saving the commands? How would you implement playback? SE-2811 Dr. Mark L. Hornick 9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.