170 likes | 367 Views
Introduction to Class Design Part 3: Class Design and Decomposition. CSIS 3701: Advanced Object Oriented Programming. Class Decomposition. Often decompose complex class into simpler support classes Why? Easier to write/debug/maintain group of simple classes than one complex class
E N D
Introduction to Class DesignPart 3: Class Design and Decomposition CSIS 3701: Advanced Object Oriented Programming
Class Decomposition • Often decompose complex class into simpler support classes • Why? • Easier to write/debug/maintain group of simple classes than one complex class • Simpler classes can be refactored into general purpose tools usable by others • Refactoring: redesigning classes after an implementation stage to improve modularity, efficiency, etc.
UML and Decomposition • Class Diagram • Represents relationships between classes • Composition relationship • One class composed of others as member variables Class Support class
Example • “Student” object in “registration” system • Data responsible for: Student • Student name • Student BANNER ID • Graduate/undergraduate status • Courses currently registered for • Number of hours currently registered for
Decomposition by Type • Does data represent multiple different concepts? • If so, create separate subclass for each • Member variables in main class refer to object in subclasses StudentInfo • Student name • Student BANNER ID • Graduate/undergraduate Student StudentInfo info ClassList classes CourseList • Courses currently registered for • Number of hours
Aggregation • If an object consists of a group of entities: • Create class for individual entities • Create class which stores list of entities • Usual abilities of aggregate class: • Allows new entities to be added/inserted • Possibly subject to validation (like add in NameList) • Allows access to entities in list • Often by index (like getNameByIndex(int index)) • Often by search • Other methods for ease of use • Validation inspectors, size of list, etc.
UML for Aggregation • Give number of entities aggregate type contains • Can be range (1..4) • Can be unlimited* any number from 0 to ∞+ any number from 1 to ∞ 5..20 4 Wheel Spoke Car * String NameList
CourseList Example StudentInfo • Student name • Student BANNER ID • Graduate/undergraduate Student StudentInfo info ClassList classes CourseList Course[] courses int totalHours Course * • Course name • BANNER course ID • Hours …
CourseList Example public class Course { private String name; private int crn; private int hours; public Course(String n, int c, int h) { name = n; crn = c; hours = h; } public String getName() {return name;} public int getCRN() {return crn;} public int getHours() {return hours;} public boolean equals(Object c) { retrun (crn == (Course)c.crn); } } Simple storage class for Course properties
CourseList Example public class CourseList { private Course[] courses; public CourseList() { courses = new Vector(); count = 0; } public void add(Course c) { if (isIn(c)) return; courses.add(c); } Stores array of Course objects User creates Course objects and passes to the container
CourseList Example • Allows access to courses in list by index or by search public Course getCourseByIndex(int index) { return courses[index]; } public Course getCourseByCRN(int CRN) { for (int i = 0; i < count; i++) if (CRN == courses.get[i].getCRN()) return courses[i]; return null; }
CourseList Example public boolean isIn(Course c) { if (getCourseByCRN(c.getCRN()) != null) return true; return false; } public int getTotalHours() { int total = 0; for (int i = 0; i < count; i++) total += courses[i].getHours(); return total; } Base search on CRN (unique field for Course objects) Additional method for property of entire list of courses
Reuse and Refactoring • Identify other subsystems that might use class • If necessary, add methods they might require Student StudentInterface ClassList classes ClassList allClasses CourseList StudentInterfacealso needs to be able to display a list of all classes for student to choose from Course[] courses int totalHours Course • Course name • BANNER course ID • Hours … *
Refactoring Example • Additional methods to support course display in UI: public class Course { … public String toString() { return name+" section "+crn+" ("+hours+" sh)"; } public class CourseList { …public String getCourseString(int index) { return courses.get(index).toString(); }
Parameter Objects • Often create objects to pass information between other objects • Simpler than passing dozens of parameters • Basic steps: • Sender constructs object containing parameter data as member variables • Receiver uses “get” inspectors to retrieve the data if necessary
UML for Parameter Objects Sender Class ReceiverClass Parameterclass
Example of Parameter Objects CourseInventory StudentInterface Student CourseList Course CourseInventoryDatabase When UI created, it asks the database for list of all courses Returned as a CourseList object When user adds course, UI passes Course object to Student