Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
COMP5028 Object Oriented Analysis and Design  Semester 2, 2004 
1 of 3 
 
 
Laboratory session Five  
Storie Dixson 432A,B 
Wednesday September 1, 2004 
School of Information Technologies 
The University of Sydney 
SOLUTION 
 
 
Objective 
• Understand the Open-Closed Principle 
• Improve an initial design to make it closed against certain changes. 
Tasks 
A. Read the Shape application in week 6's lecture slides 
B. Modify the design to make the displayAll function closed to any creation of new Shape, 
as well as any changes in ordering policy. Show piece of important Java code in your 
design class diagram. 
C. Is the whole system closed against above two possible changes? If not, which 
item is not closed and should we try to close it? Why? 
D. Write sample codes to test your design. 
Resources 
 Assume the system will be developed in Java. A few APIs in Java that might help 
you to design the application is listed below: 
 package java.util; 
 interface Comparator{ 
  public int compare (Object o1, Object o2); 
  pubic boolean equals(Object obj); 
 }  
  
 class Arrays in java.util contains various methods for manipulating arrays 
(including sorting and searching) Below are a few sorting methods you might use: 
 
 public static void sort(Object[] a,Comparator c) 
 public static void sort(Object[] a,int fromIndex,  
    int toIndex,Comparator c) 
 
 To get more info please check corresponding online Java API document.  
COMP5028 Object Oriented Analysis and Design  Semester 2, 2004 
2 of 3 
SOLUTION: 
B. 
  
C. No. The current implementation of Comparator (ShapeComparator in this case) is not closed to 
the two possible changes. If we add a new shape or change the ordering policy, we need to 
modify the implementation. We think this is reasonable violation of the OCP since adding new 
shape or changing ordering policy changes the semantics of that class, we should expect changes 
in that part. However, it is easy to make it closed. We can modify the signature of displayAll 
to void displayAll( Comparator shapeComparator)  
We now pass a Comparator to the displayAll function and we just need to create a new 
Comparator class for any changes in the shape and the ordering policy 
 
D. The test java program is attached blow. Please be reminded that I did not implement the 
ShapeCollection class, instead I use the TestShape class to simulate the function of it. I 
use an array instead of Set or List just to save the effort of initializing the array and the 
converting of a Set or List to an array. Thus we can concentrate on the most important part. 
Also in this stage it is not necessary to implement the actual draw ()method. A print statement 
is enough for our purpose here. 
 
import java.util.*; 
 
abstract class Shape{ 
 abstract public void draw(); 
COMP5028 Object Oriented Analysis and Design  Semester 2, 2004 
3 of 3 
} 
 
class ShapeComparator implements Comparator{ 
 public int compare(Object s1, Object s2){ 
  if (s1 instanceof Circle && s2 instanceof Square) 
   return -1; 
  if (s1 instanceof Square && s2 instanceof Circle) 
   return 1; 
  return 0; 
 } 
 
 public boolean equals(Object s1, Object s2){ 
  return s1.equals(s2); 
 } 
} 
 
 
class Circle extends Shape{ 
 public void draw() 
 { 
  System.out.println("circle"); 
 } 
 
} 
 
class Square extends Shape{ 
 public void draw() 
 { 
  System.out.println("square"); 
 } 
 
} 
 
public class TestShape{ 
 public static void main(String args[]){ 
  Shape[] shapes = {new Circle(), new Square(), new Circle()}; 
  Arrays.sort(shapes, new ShapeComparator()); 
  for (int i = 0; i < shapes.length; i++) 
   shapes[i].draw(); 
 } 
}