Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
COMP5028 Object Oriented Analysis and Design  Semester 1, 2005 
 
 
Laboratory session Twelve  
Storie Dixson 432 A,B 
Wednesday June 8, 2005 
School of Information Technologies 
The University of Sydney 
 
 
 
Objective 
• Understand COMMAND pattern 
• Understand the message exchange in the COMMAND pattern 
• Being able to implement simple COMMAND pattern design 
Tasks 
A. Figure 12.1 illustrate a design class diagram for a simple calculator. This calculator can 
only perform integer addition and subtraction operations. It keeps a history of all 
operations it has performed so far and can redo or undo previous operations for unlimited 
times. Identify different participants in this Command pattern such as “Invoker”, 
“Receiver”, “Client” and “Concrete Command”.  
 
Figure 12.1 Simple Calculator system 
 
B. Draw a sequence diagram to show the message exchange when client invokes an 
AddCommand in the calculator 
 
 
C. Write a simple implementation of this calculator in Java. The code for TestCalculator is 
given below.  This application is adapted from the C# sample given here 
http://www.dofactory.com/patterns/PatternCommand.aspx You will get some helpful 
hints or even pieces of code from there. You can download and try the demonstration 
from the course web. Run it: java –jar tc.jar.  
 1
COMP5028 Object Oriented Analysis and Design  Semester 1, 2005 
TestCalculator.java: 
 
public class TestCalculator{ 
 public static void main(String argv[]){ 
  // create user and let her compute 
  User user = new User(); 
  user.compute('+', 100); 
  user.compute('-', 50); 
  user.compute('-', 2); 
  user.compute('+', 20); 
   
  // undo the previous 3 operations 
  user.undo(3); 
  // redo the previous 2 operations  
  user.redo(2); 
 } 
} 
 
 2