1 / 17

Introduction to Class Design Part 3: Class Design and Decomposition

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

eugene
Download Presentation

Introduction to Class Design Part 3: Class Design and Decomposition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Class DesignPart 3: Class Design and Decomposition CSIS 3701: Advanced Object Oriented Programming

  2. 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.

  3. UML and Decomposition • Class Diagram • Represents relationships between classes • Composition relationship • One class composed of others as member variables Class Support class

  4. 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

  5. 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

  6. 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.

  7. 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

  8. 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 …

  9. 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

  10. 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

  11. 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; }

  12. 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

  13. 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 … *

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

  15. 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

  16. UML for Parameter Objects Sender Class ReceiverClass Parameterclass

  17. 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

More Related