Download presentation
Presentation is loading. Please wait.
Published byEvelyn Malone Modified over 9 years ago
1
I Like Ike I have always found that plans are useless, but planning is indispensable
2
Computer Science 313 – Advanced Programming Topics
3
Command Pattern Intent Encapsulate operation as an object Buffering or logging operations possible Supports reversal (undo) of operations Extend, combine ( party-mode ), modify operations Decouples action invocation from its execution Classes can be independent of operation executed
4
Command Pattern Example public class Quitter implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public class Bob extends JFrame { JMenuItem quit = new JMenuItem(“Quit”); quit.addActionItem(new Quitter()); } public class Bob Client
5
Command Pattern Example public class Quitter implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public class Bob extends JFrame { JMenuItem quit = new JMenuItem(“Quit”); quit.addActionItem(new Quitter()); } implements ActionListener { Command
6
Command Pattern Example public class Quitter implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public class Bob extends JFrame { JMenuItem quit = new JMenuItem(“Quit”); quit.addActionItem(new Quitter()); } public class Quitter ConcreteCommand
7
Command Pattern Example public class Quitter implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public class Bob extends JFrame { JMenuItem quit = new JMenuItem(“Quit”); quit.addActionItem(new Quitter()); } JMenuItem quit = new JMenuItem(“Quit”); quit.addActionitem(new Quitter()); Invoker
8
Command Pattern Example public class Quitter implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public class Bob extends JFrame { JMenuItem quit = new JMenuItem(“Quit”); quit.addActionItem(new Quitter()); } System.exit(0); Receiver
9
Delaying Gratification Command pattern encapsulates actions in objects Enables queuing commands Program produces Command s placed on queue Separate part executes & consumes the Command s Often used to delay actual execution of action Sometimes see delay from pushing button to action Bad programs can prevent further action executions Remember, often does not mean always
10
Information Logs
11
Keeping a Log Several ways to log each executed Command If queued, print Command before executing Rely on Command to print self in execute() Make Invoker call store() method in Command Which is the best choice depends on How the system is implemented What and when you want details logged Which programmers are most reliable
12
Improving Performance Modern CPUs contain multiple cores Each core is complete execution unit Multiple tasks can be performed at once Should write programs to take advantage of this Number of cores varies by processor Slightly older machines are single core Dual-core, quad-core, & more also exist Performs best with one job per core But how to scale to take advantage of this?
13
Commands to the Rescue
14
How Thread Pools Work Creates pool of threads to perform work Each thread executes one command at a time Completes one, then looks to execute another Creating thread per core optimizes performance Need to write ConcreteCommand classes Can be any class implementing Runnable Invoker calls run() method in instances Perfect example of Command Pattern
15
Thread Pool Example public class Lin implements Runnable { public void run() { for (long num = 0; num < 50; num++) System.out.println(“Lin: ” + num); } } public class Exp implements Runnable { public void run() { for (long num = 1; num < 2 50 ; num*=2) { System.out.println(“Exp: ” + num); } }
16
Thread Pool Example public static void main(String[] args){ Runnable command1 = new Lin(); Runnable command2 = new Lin(); Runnable command3 = new Exp(); ExecutorService threadExecutor = Executors.newFixedThreadPool(2); threadExecutor.execute(command1); threadExecutor.execute(command2); threadExecutor.execute(command3); threadExecutor.shutdown(); }
17
Thread Pool Results Thread pool started with 1, 2, & 3 threads:
18
Transaction-Based System Databases also big users of command pattern Normal programs can easily replicate actions Typically work using transactions Gather set of changes into single action Atomically perform all changes in the set Either ALL changes made or NO changes made
19
Transaction-Based System Transactions are just a party ! Also very, very hot area of research Many organizations looking to generalize this Example of MacroCommand s in wide use Command places individual changes into group Apply changes when MacroCommand executed Add checks ensuring one set changed at a time
20
For Next Class Lab #6 now available on web/Angel No class on Friday Spend the time working on the lab Friday’s lecture held over until Monday Will discuss the how code actually compiled What is SSA? What does it do? How does it work? Why does this mean long convoluted lines are silly? How does it help eliminate duplicate code?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.