--------------------------------------------------- Com S 362 Lecture Outline --------------------------------------------------- CLASS DIAGRAMS - describes the types of objects in the system - also describes the static relationships between these objects - Three part: class name, properties, and operations > draw the class diagram for class Point ---------------------- | Point | ---------------------- | - x: int | | - y: int | ---------------------- | + getX(): int | | + getY(): int | | + setX(int) | | + setX(x:int):void | ---------------------- ------------------------------------------------- CLASS ATTRIBUTES > Explain attributes of class Point -x: int -y: int (Accessibility Modifiers) Q: What does -x means, what are the alternatives? + stands for public # stands for protected / stands for derived ~ stands for package Q: What about more than one of type int? - x: int[0..1] (Zero or one) - x: int[1] (Exactly one) - x: int[*] (Zero or more, no limit) - x: int[m..n] (from m to n) Example: * - Zero or more 1..* - One or more 1..40 - One to 40 5 - Exactly five 3,5,7 - Exactly 3, 5, or 8 ---------------------------------------------- Ex: How will you represent attributes programmatically? In Java? In C#? Write code on the board: ----------------------------------------------- GENERAL FORM OF ATTRIBUTES visibility name: type multiplicity = default-value {property-string} - visibility marker is public(+)/private(-)/protected(#). - the name is a string. - the type is either a pre-defined or a user-defined. type of the attribute. - multiplicity is of the form m..n. - default value is the value for a newly created object. - property-string allows additional information to be specified (such as readonly). ----------------------------------------------- OPERATIONS > Explain operations of class Point > accessibility modifiers similar to attributes -------------------------------------------------- GENERAL FORM OF OPERATIONS visibility name(parameter-list): return-type { property-string } - visibility marker is public(+)/private(-)/protected(/). - the name is a string. - the parameter-list is a list, possibly empty, that represents the type of each parameter, (optionally) the name of the parameter, and the direction (i.e. whether it is in parameter, out parameter, or in/out parameter. Default value represents in parameter. - the return-type is either a pre-defined or a user-defined that represents the return type of the method. - property-string allows additional information to be specified (such as readonly). --------------------------------------------------- RELATIONSHIPS BETWEEN TYPES ASSOCIATIONS ------------ - a type of relationship between two classes - shown as a solid arrow from source to target [Source Class] ==============> [Target Class] Example: The following association arrow denotes that a Line contains a field of type Point. --------- ------------ | Point | 2 start,end 1 | Line | |-------|<=================|----------| Alternative: ---------------------- | Line | ---------------------- | - start: Point | | - end: Point | ---------------------- | + getStart(): Point| | + getEnd(): Point | | + setStart(Point) | | + setEnd(Point) | ---------------------- Q: When to use association arrows vs. attributes? > Rule of thumb - for builtin (primitive) types such as int, float, boolean, double, string, etc use attributes, otherwise use associations. Generally, for more complex types. GENERALIZATION -------------- - relationship between two classes such that one class is a subclass of other - represented as a solid arrow with a triangle head from the subclass to superclass or interface, more generally from subtype to supertype. -------------------------------- | <> FigureElement | -------------------------------- | + draw() | -------------------------------- /\ ---- | | | ---------------------- | Line | ---------------------- | - start: Point | | - end: Point | ---------------------- | + getStart(): Point| | + getEnd(): Point | | + setStart(Point) | | + setEnd(Point) | ---------------------- Note the use of stereotype <> in the diagram for FigureElement. Ex: Look up stereotypes in UML. DEPENDENCY ---------- - denotes that a source class may have to change if the target class has to change. - dependencies may exist for various reasons, sending a message, using other class to store data, mentioning as parameters. - denoted as a dotted arrow between the source and the target. ------------------------------------------------ Some interesting tidbits about UML class diagrams Q: How to denote a static attribute or operation? (Underline it) Q: How to denote a derived attribute or operation? (mark the name with /) Q: What is the difference between composition and aggregation? Q: How are composition and aggregation denoted? Both are denoted with a diamond tail and an arrow head. In case of composition the tail diamond is filled, whereas aggregation is denoted by an empty diamond. Q: How do you mark a class abstract? Add the keyword abstract to the class name. Q: How do you mark a template class? Mark the class diagram on the top-right with a dotted rectangle containing the type parameter. Q: How do you mark an enumeration? <>, similar to <> Q: What is an active class? How do you mark it? Each instance of an active class executes and controls its own thread of control. It is shown as following. ----------------------------- || || || Command || || Processor || || || ----------------------------- Q: What are description classes? Description classes: (Meta-class) - Contains information that describes something else. Example: Book Description 1--Describes---* Books