06 Class Design
06 Class Design
Design Classes
ITSS SOFTWARE DEVELOPMENT
Project Specific
6. CLASS DESIGN Guidelines
Class
Design
Design Model
Supplementary
Specifications
Analysis Classes
Some slides extracted from IBM coursewares
1 2
2. Define Operations/Methods
<<control>>
3. Define Relationships Between Classes <<subsystem>>
Subsystem
4. Define States
<<entity>>
5. Define Attributes
6. Class Diagram
<<boundary>>
<<subsystem>>
Subsystem
Many-to-Many Mapping
3 4
1
6
5 6
7 8
How Many Classes Are Needed? Strategies for Designing Boundary Classes
• Many, simple classes means that each class
• Encapsulates less of the overall system intelligence • User interface (UI) boundary classes
• What user interface development tools will be used?
• Is more reusable
• How much of the interface can be created by the
• Is easier to implement
development tool?
• A few, complex classes means that each class • External system interface boundary classes
• Encapsulates a large portion of the overall system • Usually model as subsystem
intelligence
• Is less likely to be reusable MainWindow SubWindow
7 8
2
9 10
Strategies for Designing Entity Classes Strategies for Designing Control Classes
• Entity objects are often passive and persistent • What happens to Control Classes?
• Performance requirements may force some re-factoring • Are they really needed?
Analysis Design • Should they be split?
<< Entity >> FatClass
FatClass - privateAttr • How do you decide?
- privateAttr
+ getCommonlyUsedAttr1()
- commonlyUsedAttr1
- commonlyUsedAttr2 + getCommonlyUsedAttr2() • Complexity
- rarelyUsed1 + getRarelyUsedAtt1()
- rarelyUsed2 + getRarelyUsedAtt2() • Change probability
1 0..1
• Distribution and performance
FatClassDataHelper FatClassLazyDataHelper • Transaction management
- commonlyUsedAttr1 - rarelyUsedAttr1
- commonlyUsedAttr2 - rarelyUsedAttr2
9 10
• A general purpose mechanism for organizing elements and services the system uses
Package B
into groups
Package A
• A model element which can contain other model
elements
Package
Name
11 12
3
Packaging Tips: Boundary Classes Packaging Tips:
If it is likely the system interface If it is unlikely the system interface
Functionally Related Classes
will undergo considerable changes will undergo considerable changes • Criteria for determining if classes are functionally
related:
• Changes in one class' behavior and/or structure
necessitate changes in another class
• Removal of one class impacts the other class
• Two objects interact with a large number of messages
or have a complex intercommunication
• A boundary class can be functionally related to a
particular entity class if the function of the boundary
class is to present the entity class
Boundary classes placed in Boundary classes packaged • Two classes interact with, or are affected by changes in
separate packages with functionally related classes the same actor
13 14
OO Principle: Encapsulation
15 16
4
Package Coupling: Tips
Example: Registration Package
• Packages should not be A B
cross-coupled
X MainStudentForm MainRegistrarForm
1 1
Upper A
0..1
• Packages in lower layers 0..1
Layer <<boundary>> <<boundary>>
should not be dependent RegisterForCoursesForm CloseRegistrationForm
upon packages in upper X X
Lower
layers Layer
B 1
1 1
<<control>> <<control>>
RegistrationController
• In general, dependencies CloseRegistrationController
C
should not skip layers
X = Coupling violation
17 18
primaryCourses alternateCourses
0..2
<<entity>> 0..4
<<entity>> <<entity>> 0..*
PrimaryScheduleOfferingInfo <<entity>> instructor <<entity>> <<entity>>
FulltimeStudent ParttimeStudent
Professor CourseOffering Course
0..*
0..1 0..* 0..* 1
Prerequisites
0..*
1
CourseOfferingList
19 20
5
22
21 22
23 24
23 24
6
25 26
Program documentation
/** Guidelines: Designing Operation Signatures
* Parse XML data into a DOM representation, taking local resources and Schemas into account.
* @param inputData a string representation of the XML data to be parsed.
* @param validating whether to Schema-validate the XML data • When designing operation signatures, consider if
* @return the DOM document resulting from the parse
* @throws ParserConfigurationException if no parser could be created
parameters are:
* @throws SAXException if there was a parse error
• Passed by value or by reference
* @throws IOException if there was a problem reading from the string
*/ • Changed by the operation
public static Document parseDocument(String inputData, boolean validating) throws
ParserConfigurationException, SAXException, IOException { • Optional
//Change to UnicodeReader for utf-8
...
• Set to default values
} • In valid parameter ranges
• The fewer the parameters, the better
• Pass objects instead of “data bits”
25 26
27
27 28
7
29 30
29 30
31 32
31 32
8
33 34
33 34
35 36
Class Diagram
Role Name
Client 0..* 0..* Supplier
<<entity>>
PerformResponsibility() SubjectInfo
Association
Prerequisites
Relationship for every link!
35 36
9
37 38
37 38
Java
What Does Multiplicity Mean? implementation
• Multiplicity answers two questions:
//InsuranceCompany.java file
• Is the association mandatory or optional? public class InsuranceCompany
{
• What is the minimum and maximum number of // Many multiplicity can be implemented using Collection
private List<InsuranceContract> contracts;
instances that can be linked to one instance?
/* Methods */
<<entity>> <<entity>>
}
0..* 1 // InsuranceContract.java file
CourseInfo. Subject
public class InsuranceContract
0..* {
private InsuranceCompany refers_to;
Prerequisites 0..3
/* Methods */
}
39 40
10
41 42
41 42
43
• The part may be removed (by the whole) before the whole is
removed.
Whole Part
44
43 44
11
45 @Nguyễn Thị Thu Trang, trangntt@soict.hust.edu.vn 46
1 0..2,4
When in doubt, use association.
45 46
Car(Engine engine) {
this.engine = engine;
<<control>>
} CourseRegistrationController
47 48
12
49 50
?
• Total number of CourseInfo and
Schedules are not small
• Must be able to navigate in both
directions Schedule + primaryCourses CourseInfo.
Schedule + primaryCourses CourseInfo Schedule + primaryCourses CourseInfo
0..* 0..4
0..* 0..4 0..* 0..4
49 50
51 52
51 52
13
53 54
53 54
55 56
ClassB
ClassB
+ utilityOp ( )
55 56
14
57 58
57 58
59 60
Descendents
59 60
15
61 62
61 62
63 64
63 64
16
65 66
Events: addLecturer,
removeLecturer Unassigned
removeLecturer addLecturer
Assigned
65 66
67 68
/ numStudents = 0
• Executed when the state
removeStudent [numStudents >0]/ numStudents = numStudents - 1
Entry/anAction
Unassigned
is entered
closeRegistration [numStudents<3]
addLecturer Canceled
cancel
• Do do/ Send cancellation notices
Full
StateB StateC
Do/anActivity Exit/someAction
• Exit
• Executed when the state addStudent /
numStudents = numStudents + 1
closeRegistration [ has Professor assigned ]
67 68
17
69 70
Which Objects Have Significant State? How Do State Machines Map to the Rest of the Model?
[numStudents<10] [numStudents>=10]
69 70
71 72
71 72
18
73 74
73 74
75 76
75 76
19
77 78
77 78
79 80
79 80
20
81
• A model element that can contain other model • Functionally coupled attributes/behavior
elements. • Generalizations were made
• A package can be used: • All class requirements were addressed
• To organize the model under development • Demands are consistent with state machines
• As a unit of configuration management • Complete class instance life cycle is described
• The class has the required behavior
University
Artifacts
81 82
83 84
21
86
85 86
87 88
Program documentation
Class design /**
* Parse XML data into a DOM representation, taking local resources and Schemas into account.
* @param inputData a string representation of the XML data to be parsed.
• Attribute design * @param validating whether to Schema-validate the XML data
* @return the DOM document resulting from the parse
• Type, description * @throws ParserConfigurationException if no parser could be created
* @throws SAXException if there was a parse error
• Operation design * @throws IOException if there was a problem reading from the string
• Operation Signature */
public static Document parseDocument(String inputData, boolean validating) throws
• Purpose/description of operation ParserConfigurationException, SAXException, IOException {
//Change to UnicodeReader for utf-8
• Purpose /description of each parameter ...
}
• Description of return value
• Error/Exception (when)
• Method design
• Special algorithm
• How to use parameters
87 88
22