Using UML Patterns and Java ObjectOriented Software Engineering
Using UML, Patterns, and Java Object-Oriented Software Engineering Art for Chapter 9, Specifying Interfaces
Figure 9 -1, the class implementor, the class extender, and the class user role. Class User Developer Class Implementor Class Extender Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java Call Class Realize Class Refine Class 2
Figure 9 -2, ARENA Game abstract class with user classes and extender classes. League Game 1 * Tournament Bernd Bruegge & Allen H. Dutoit Tic. Tac. Toe Object-Oriented Software Engineering: Using UML, Patterns, and Java Chess 3
Figure 9 -3, Declaration for the Tournament class. Tournament -max. Num. Players: int public class Tournament { private int max. Num. Players; /* Other fields omitted */ +get. Max. Num. Players(): int +get. Players(): List +accept. Player(p: Player) +remove. Player(p: Player) +is. Player. Accepted(p: Player): bool public Tournament(League l, int max. Num. Players) public int get. Max. Num. Players() {…}; public List get. Players() {…}; public void accept. Player(Player p) {…}; public void remove. Player(Player p) {…}; public boolean is. Player. Accepted(Player p) {…}; /* Other methods omitted */ } Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 4
Figure 9 -4, Examples of invariants, preconditions, and postconditions in OCL attached as notes to the UML model. «invariant» get. Max. Num. Players() > 0 «precondition» get. Num. Players() < get. Max. Num. Players() Tournament «precondition» !is. Player. Accepted(p) «precondition» is. Player. Accepted(p) Bernd Bruegge & Allen H. Dutoit «postcondition» is. Player. Accepted(p) -max. Num. Players: int +get. Num. Players(): int +get. Max. Num. Players(): int «postcondition» +accept. Player(p: Player) !is. Player. Accepted(p) +remove. Player(p: Player) +is. Player. Accepted(p: Player): boolean Object-Oriented Software Engineering: Using UML, Patterns, and Java 5
Tournament invariant context Tournament inv: self. get. Max. Num. Players() > 0 Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 6
Tournament. accept. Player contract context Tournament: : accept. Player(p) pre: not is. Player. Accepted(p) context Tournament: : accept. Player(p) pre: get. Num. Players() < get. Max. Num. Players() context Tournament: : accept. Player(p) post: is. Player. Accepted(p) context Tournament: : accept. Player(p) post: get. Num. Players() = @pre. get. Num. Players() + 1 Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 7
Tournament. remove. Player contract context Tournament: : remove. Player(p) pre: is. Player. Accepted(p) context Tournament: : remove. Player(p) post: not is. Player. Accepted(p) context Tournament: : remove. Player(p) post: get. Num. Players() = @pre. get. Num. Players() - 1 Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 8
Figure 9 -5, Method declarations for the Tournament class annotated with preconditions, postconditions, and invariants public class Tournament { /** The maximum number of players * is positive at all times. * @invariant max. Num. Players > 0 */ private int max. Num. Players; /** The players List contains * references to Players who are * are registered with the * Tournament. */ private List players; /** Returns the current number of * players in the tournament. */ public int get. Num. Players() {…} Bernd Bruegge & Allen H. Dutoit /** The accept. Player() operation * assumes that the specified * player has not been accepted * in the Tournament yet. * @pre !is. Player. Accepted(p) * @pre get. Num. Players()<max. Num. Players * @post is. Player. Accepted(p) * @post get. Num. Players() = * @pre. get. Num. Players() + 1 */ public void accept. Player (Player p) {…} /** The remove. Player() operation * assumes that the specified player * is currently in the Tournament. * @pre is. Player. Accepted(p) * @post !is. Player. Accepted(p) * @post get. Num. Players() = @pre. get. Num. Players() - 1 */ public void remove. Player(Player p) {…} Object-Oriented Software Engineering: Using UML, Patterns, and Java 9
Figure 9 -6, Associations among League, Tournament, and Player classes in ARENA. League * +start: Date +end: Date +get. Active. Players() {ordered} * tournaments Tournament +start: Date +end: Date +accept. Player(p: Player) * tournaments players * Bernd Bruegge & Allen H. Dutoit * players Player +name: String +email: String Object-Oriented Software Engineering: Using UML, Patterns, and Java 10
Figure 9 -7, Example situation with two Leagues, two Tournaments, and five Players. ttt. Expert: League chess. Novice: League winter: Tournament xmas: Tournament start=Dec 21 end=Dec 22 start=Dec 23 end=Dec 25 alice: Player bob: Player marc: Player joe: Player zoe: Player Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 11
Figure 9 -8, There are only three basic types of navigation. Any OCL constraint can be built using a combination of these three types. 1. Local attribute Tournament start: Date end: Date 2. Directly related class League 3. Indirectly related class League * * Player * Tournament * * Player Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 12
Examples of constraints using each type of navigation 1. Local attribute context Tournament inv: end - start <= Calendar. WEEK 2. Directly related class context Tournament: : accept. Player(p) pre: league. players->includes(p) 3. Indirectly related class context League: : get. Active. Players post: result = tournaments. players->as. Set Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 13
OCL forall quantifier /* All Matches in a Tournament occur within the Tournament’s time frame */ context Tournament inv: matches->for. All(m: Match | m. start. after(t. start) and m. end. before(t. end)) Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 14
OCL exists quantifier /* Each Tournament conducts at least one Match on the first day of the Tournament */ context Tournament inv: matches->exists(m: Match | m. start. equals(start)) Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 15
Figure 9 -9, Analysis objects of ARENA identified during the analysis of Announce Tournament use case. Tournament. Form 1 1 apply. For. Tournament() Tournament. Control * select. Sponsors() * advertize. Tournament() accept. Player() announce. Tournament() 1 1 Tournament * * players Player * matches * Match start status name * start end accept. Player() remove. Player() schedule() * sponsors * * Advertiser matches * play. Move() get. Score() Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 16
Figure 9 -10, a sequence diagram for the apply. For. Tournament() operation. : Player : Tournament. Form: Tournament. Control t: Tournament p: Player apply. For. Tournament(p) is. Player. Overbooked(p) get. Start. Date() get. End. Date() get. Tournaments() tournaments Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 17
Figure 9 -11, Adding type information to the object model of ARENA. Tournament. Form 1 1 +apply. For. Tournament() * Tournament. Control * +select. Sponsors(advertisers): List +advertize. Tournament() +accept. Player(p) +announce. Tournament() +is. Player. Overbooked(): boolean 1 1 Tournament * * players Player * matches * Match +name: String * +start: Date +end: Date +accept. Player(p) +remove. Player(p) +schedule() * sponsors * * Advertiser matches * +start: Date +status: Match. Status +play. Move(p, m) +get. Score(): Map Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 18
Pre- and post-conditions for ordering operations on Tournament. Control +select. Sponsors(advertisers) +advertize. Tournament() +accept. Player(p) +announce. Tournament() +is. Player. Overbooked(): boolean context Tournament. Control: : select. Sponsors(advertiser s) pre: interested. Sponsors->not. Empty and tournament. sponsors->is. Empty context Tournament. Control: : select. Sponsors(advertiser s) post: tournament. sponsors. equals(advertisers) context Tournament. Control: : advertise. Tournament() pre: tournament. sponsors->is. Empty and not tournament. advertised context Tournament. Control: : advertise. Tournament() post: tournament. advertised context Tournament. Control: : accept. Player(p) pre: tournament. advertised and interested. Players->includes(p) and Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java not is. Player. Overbooked(p) 19
Specifying invariants on Tournament and Tournament Control ¨ All Matches of in a Tournament must occur within the time frame of the Tournament context Tournament inv: matches->for. All(m| m. start. after(start) and m. start. before(end)) ¨ No Player can take part in two or more Tournaments that overlap context Tournament. Control inv: tournament. players->for. All(p| p. tournaments->for. All(t| t <> tournament implies not t. overlap(tournament))) Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 20
Specifying invariants on Match Player players* Match ¨ players * * tournaments Tournament matches * A match can only involve players who are accepted in the tournament context Match inv: players->for. All(p| p. tournaments->exists(t| t. matches->includes(self))) context Match inv: players. tournaments. matches. includes(se lf) Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 21
Figure 9 -13, A simple example of contract inheritance: An invariant specified in a superclass must hold for all of its subclasses. User email: String «invariant» email <> nil notify() League. Owner Bernd Bruegge & Allen H. Dutoit Player Advertiser Object-Oriented Software Engineering: Using UML, Patterns, and Java Spectator 22
Figure 9 -14, Embedded ODD approach. Class stubs are generated from the object design model. Analysis model Document analysis RAD System design Subsystem decomposition Design goals Object design Initial object design model Generate class stubs Initial class stubs Implementation Commented code Bernd Bruegge & Allen H. Dutoit Document object design ODD Object-Oriented Software Engineering: Using UML, Patterns, and Java 23
Figure 9 -16, new Round class and changes in the Tournament. Style, Tournament, and Round APIs. League 1 * plan. Rounds(Tournament) : List legal. Num. Players(n: int) : boolean get. Tournament. Style() : Tournament. Style * players* Player Tournament. Style Tournament tournaments name start end name: String email: String notify(Player, Message) get. Matches(Tournament) accept. Player() remove. Player() plan() {ordered} players* Match start status score matches 1 * play. Move(Player, Move) get. Moves(): List Bernd Bruegge & Allen H. Dutoit * Round plan() get. Previous. Round(): Round is. Planned(): boolean is. Completed(): boolean Object-Oriented Software Engineering: Using UML, Patterns, and Java 24
Constraints on Tournament. Style ¨ Only tournaments without rounds and with the right number of players can be planned. context Tournament. Style: : plan. Rounds(t: Tournament) pre: t <> nil and t. rounds = nil and legal. Num. Players(t)>contains(t. players->size) ¨ All players are assigned to at least one match context Tournament. Style: : plan. Rounds(t: Tournament) post: t. get. Players()->for. All(p| p. get. Matches(tournament)->not. Empty) context Tournament. Style: : plan. Rounds(t: Tournament) Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java post: 25
Invariant on Round ¨ A player cannot be assigned to more than one match per round context Round inv: matches->for. All(m 1: Match| m 1. players->for. All(p: Player| p. matches->for. All(m 2: Match| m 1 <> m 2 implies m 1. round <> m 2. round))) Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 26
Constraints on Round. plan() ¨ Invoking plan() on a Round whose previous Round is completed results in a planned Round context Round. plan() post: @pre. get. Previous. Round(). is. Completed() implies is. Planned() ¨ A round is planned if all matches have players assigned to them context Round. is. Planned() post: result implies matches->for. All(m| m. players->size = tournament. league. game. num. Players. Per. Match) ¨ A round is completed if all of its matches are completed. context Round. is. Completed() post: result implies matches->for. All(m| m. winner <> nil) Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 27
Constraints on Knock. Out. Style ¨ The number of players should be a power of two. context Knock. Out. Style: : legal. Num. Players(t: Tournament) post: result Sequence[2. . t. max. Num. Players]>select(elem| floor(log(elem)/log(2)) = (log(elem)/log(2))) ¨ The number of matches in a round is 1 for the last round. Otherwise, the number of matches in a round is exactly twice the number of matches in the subsequent round. context Knock. Out. Style: : plan. Rounds(t: Tournament) post: result->for. All(index: Integer| if (index = result->size) then result->at(index). matches->size = 1 else result->at(index). matches->size = (2*result->at(index+1). matches->size)) Bernd Bruegge &endif) Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 28
Constraints on Knock. Out. Round ¨ A player can play in a round only if it is the first round or if it is the winner of a previous round. context Knock. Out. Round inv: previous. Round = nil or matches. players->for. All(p| round. previous. Round. matches>exists(m| m. winner = p)) ¨ If the previous round is not completed, this round cannot be planned. context Kock. Out. Round: : plan() post: not self@pre. get. Previous. Round(). is. Completed() implies not is. Planned() Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 29
- Slides: 29