Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 313 – Advanced Programming Topics.

Similar presentations


Presentation on theme: "Computer Science 313 – Advanced Programming Topics."— Presentation transcript:

1 Computer Science 313 – Advanced Programming Topics

2 Command Pattern Intent  Decouples action invocation from its execution  Write classes independent of operation executed  Encapsulate operation as an object  Makes possible buffering or logging operations  Undo-ing operations can be supported with ease  Code will support combining or extending actions

3 Command Pattern Intent  Decouples action invocation from its execution  Write classes independent of operation executed  Encapsulate operation as an object  Makes possible buffering or logging operations  Undo-ing operations can be supported with ease  Code will support combining or extending actions

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()); }

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()); } Client

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()); } Command

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()); } ConcreteCommand

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.addActionListener(new Quitter()); quit.doClick(); } Invoker

9 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()); } Receiver

10 Delaying Gratification  Command pattern must encapsulate actions  Can delay executing commands until should act  GUI adds to Queue & execute Command s in FIFO order  Enables splitting action execution & creation  In Java, sometimes see delay between cause & effect  Prevent executing more actions if code contains bugs  Even if listener throws exception, program continues

11 Information Logs  Track Command s executed, by keeping a log  If multiple Invoker s, Command s must refer to log  Keep only one log at a time to record order  To keep this log, could pass references everywhere  Switching the log causes problems, however  Solution: Make Log a singleton class

12 Keeping a Log  Several ways to log executed Command s  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…  System implementation & where changes expected  Exactly which of the details to be logged  Classes most reliable coders on team were writing

13 Improving Performance  Many modern CPUs contain multiple cores  Complete execution unit in each core on chip  Processor executes multiple tasks simultaneously  Need to take advantage to maximize performance  Number of cores varies by processor  Single core in older or cheaper processors  Can also find dual-core, quad-core, & more  One job per core provides best performance  Must write your code to scale to available cores

14 Commands to the Rescue  Simple solution using the command pattern  Interface for Command already in Java public interface Runnable { public void run(); }  Invoker classes available as part of Java, also Executor.newFixedThreadPool() Executor.newScheduledThreadPool()  In your code, can get instance of this class using Executor.newFixedThreadPool() or Executor.newScheduledThreadPool()

15 How Thread Pools Work  Creates pool of threads to perform work  Each of these threads execute command at a time  Executes next command after first is completed  Optimize performance creating thread per core  ConcreteCommand classes left to write  Could be any class implementing Runnable  Invoker calls run() method in instances

16 How Thread Pools Work

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

18 Thread Pool Example public static void main(String[] args){ Runnable command1 = new Lin(); Runnable command2 = new Lin(); Runnable command3 = new Exp(); ExecutorService pool = Executors.newFixedThreadPool(2); pool.execute(command1); pool.execute(command2); pool.execute(command3); pool.shutdown(); pool.awaitTermination(21,TimeUnit.DAYS); }

19 Thread Pool Results  Thread pool started with 1, 2, & 3 threads:

20 Transaction-Based System  Databases also big users of command pattern  Can easily replicate actions in normal programs  Transactions are used to make DBs work  Gather set of changes into single action  Makes all changes in transactions as single action  Result either ALL changes or NO changes made

21 Transaction-Based System

22 For Next Class  Lab #5 due by 11:59AM next Friday  Get busy restructuring code so ready for real world  Will have time in lab, but due before next week’s lab  Next lecture on Adapter Pattern (p. 235-253)  Fir square peg in round hole using code; can we do it?  What does this have to do with legacy code?  No standard electrical plug; why could this be?


Download ppt "Computer Science 313 – Advanced Programming Topics."

Similar presentations


Ads by Google