Class Represented by a rectangle with possibly 3 compartments Customer Class name Customer Class name Customer Class name getName() checkCreditRating() operations Customer Name Address Fall 2009 Class name attributes ACS-3913 Name Address attributes getName() checkCreditRating() operations Ron McFadyen 1 Class Some classes are stereotyped: «Singleton» dbFacade Later in the course we study the Singleton design pattern. Some methodologies have 3 stereotypes for “analysis” classes: boundary, control, entity Fall 2009 ACS-3913 Ron McFadyen 2 Attributes an object contains data which are defined as part of the Class definition examples: • Students have names, addresses, etc; • Courses have titles, descriptions, prerequisite information. Student name address Rectangle corner: Point Level of detail present will depend on whether you are in analysis or design, and your purposes at the time Fall 2009 ACS-3913 Ron McFadyen 3 Attributes To what degree is an attribute visible to other classes? Private – Public + Protected # Package ~ Student -name -address Fall 2009 ACS-3913 Ron McFadyen 4 Attributes Default values = Derived values / Multiplicity [ ] Ordering {ordered} Uniqueness {unique} Invoice -date:Date = today -/total: Currency -payments[0..*]: Currency Fall 2009 ACS-3913 Student -name -address[1..3] {unique} Ron McFadyen 5 Operations. What are the responsibilities of a class? What can it do? Visibility Parameters Signature the name, parameters, and return type of the operation Student +getName() +getGPA(term :Term, gpaType: String) Fall 2009 ACS-3913 Ron McFadyen 6 Associations • correspond to verbs expressing a relationship between classes • example a Library Member borrows a Copy of a Book •Navigability •A directed association show that messages will be sent in that direction •Multiplicities • we indicate via multiplicities the range of allowable cardinalities for participation in an association • examples: 1, 1..*, 0..*, 1..3 Fall 2009 ACS-3913 Ron McFadyen 7 Associations • Names and roles • you can name the relationship and indicate how to read it • you can give role names for participating objects The name of the relationship and the direction for reading the name Person 1..* employee employer The role of a Person in this relationship Fall 2009 1 Works for ACS-3913 Company The role of a Company in this relationship Ron McFadyen 8 Associations •example: A duck uses a flying behavior Duck Fall 2009 1 uses ACS-3913 FlyBehavior Ron McFadyen 9 Associations • example: An employee is supervised by an employee reports to supervised * Employee 0,1 supervisor A reflexive association: an instance of this association involves two Employee objects (two objects from the same class). Fall 2009 ACS-3913 Ron McFadyen 10 Inner Class It is common to use + SwingObserverExample + to indicate an inner class Angel listens to AngelListener + Devil listens to DevilListener Fall 2009 ACS-3913 Ron McFadyen 11 Navigability The association below is directed. The arrow indicates the direction in which messages are sent. Note this means that a duck will “know” its flying behaviour. In the implementation of a duck there is need for an attribute that is a reference to a flying behaviour object. 1 uses Duck Fall 2009 FlyBehavior ACS-3913 Ron McFadyen 12 A b: B B Navigability: An association line with a navigability adornment indicates that an object of one class (A) is connected uni-directionally to an object of another class (B) An object of class A can navigate to an object of class B. •I.e. an object of class A needs to know the B object, but not the other way around. •An instance of A can send a message to an instance of B, the instance can respond, but doesn’t initiate. An association line that has no specific navigation indicated, is considered to be bi-directional Fall 2009 ACS-3913 Ron McFadyen 13 Association Class * * Company Business rule: a person contracts their services to a company Person Contract startDate Association Class: a modeling element that is both an association and a class. It has attributes, operations, multiplicities, etc. It can participate in other relationships. Note the dashed line. Fall 2009 ACS-3913 Ron McFadyen 14 * * Company Person Contract startDate Many to many associations •likely candidate for an Association Class Limitation: each company object can be associated to a person object only once. If we wanted to provide for a person to be contracted to the same company more than once, we would need to promote Contract to be a regular class. How do we model this? Fall 2009 ACS-3913 Ron McFadyen 15 Reflexive Association An association involving the same class more than once. Person 1 1 marries Person ? * * marries Fall 2009 ACS-3913 Ron McFadyen 16 Reflexive Association An association involving the same class more than once. Part * Fall 2009 * comprises ACS-3913 Ron McFadyen 17 Interfaces An interface is special type of class that cannot be instantiated. An application can never instantiate an interface. An interface defines a set of public attributes and operations that some class must use (depends on) There is no behaviour defined, no method coded Some other class inherits the interface and provides the implementation (generalization, inheritance) Fall 2009 ACS-3913 Ron McFadyen 18 Interfaces The interface named FlyBehavior comprises the fly operation <<interface>> FlyBehavior fly() FlyWithWings fly() Fall 2009 FlyNoWay fly() ACS-3913 Ron McFadyen These classes implement the fly operation 19 Abstract Classes An abstract class is a special type of class in a class hierarchy that cannot be instantiated. An application can never instantiate an abstract class and so must instantiate objects at a more specialized level in the hierarchy. An abstract class defines a set of attributes and operations that some class must use (depends on) If an operation is not abstract, then its behaviour is defined in the abstract class, i.e. there is an implementation, but this may be overriden in a more specialized subclass. Fall 2009 ACS-3913 Ron McFadyen 20 Abstract Classes Duck The abstract class has its name in italics. performfly() swim() display() MallardDuck display() Fall 2009 The performFly() and swim() operations are defined, but display() is not defined (it is an abstract operation) RubberDuck swim() display() ACS-3913 The swim operation is overridden in RubberDuck. Both subclasses have implementations of display(). Ron McFadyen 21