Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns.

Similar presentations


Presentation on theme: "Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns."— Presentation transcript:

1 Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns

2 Programming With Java ICS201 University Of Hail2 Introduction to UML oUML (Unified Modeling Language) is a graphical language used for designing and documenting OOP software. oUML is a software design tools that can be used within the context of any OOP (Object-Oriented programming) language.

3 Programming With Java ICS201 University Of Hail3 UML oPseudocode is a way of representing a program in a linear and algebraic manner.  It simplifies design by eliminating the details of programming language syntax. oGraphical representation systems for program design have also been used:  Flowcharts and structure diagrams for example. oUnified Modeling Language (UML) is yet another graphical representation formalism.  UML is designed to reflect and be used with the OOP philosophy.

4 Programming With Java ICS201 Types of UML Diagrams Use Case Diagram Class Diagram Sequence Diagram Collaboration Diagram State Diagram ….

5 Programming With Java ICS201 5 Classes A class is simply represented as a box with the name of the class inside –The diagram may also show the attributes and operations –The complete signature of an operation is: operationName(parameterName: parameterType …): returnType Rectangle height width getArea resize Rectangle height width Rectangle getArea resize Rectangle height: int width: int getArea(): int resize(int,int)

6 Programming With Java ICS201 University Of Hail6 UML Class Diagram oA class diagram is divided up into three sections: 1.The top section contains the class name. 2.The middle section contains the data specification for the class (attributes). 3.The bottom section contains the actions or methods of the class (operations). ClassName attributes operations

7 Programming With Java ICS201 Class Names ClassName attributes operations The name of the class is the only required tag in the graphical representation of a class. It always appears in the top-most compartment.

8 Programming With Java ICS201 Software Design (UML) Class Attributes (Cont’d) Person + name : String # address : Address # birthdate : Date / age : Date - ssn : Id Attributes can be: + public # protected - private / derived Attributes are usually listed in the form: attributeName : Type A derived attribute is one that can be computed from other attributes. For example, a Person’s age can be computed from his birth date. / age : Date

9 Programming With Java ICS201 Software Design (UML) Class Operations Person name : String address : Address birthdate : Date ssn : Id eat sleep work play Operations describe the class behavior and appear in the third compartment. oEach method in a UML diagram is indicated by the name of the method, followed by its parameter list, a colon (:), and its returned type.

10 Programming With Java ICS201 University Of Hail10 UML Class Diagram oA class diagram need not give a complete description of the class. oIf a given analysis does not require that all the class members be represented, then those members are not listed in the class diagram. oMissing members are indicated with an ellipsis (three dots).

11 Programming With Java ICS201 University Of Hail11 Example (UML Class Diagram) Square - Side : double - xCoordinate : double - yCoordinate : double + resize (double newSide) : void + move (double newx, double newy ) : void # erase ( ) : void … attributes Class methods Class name

12 Programming With Java ICS201 Software Design (UML) Relationships In UML, object interconnections (logical or physical), are modeled as relationships. There are three kinds of relationships in UML: dependencies generalizations associations

13 Programming With Java ICS201 Software Design (UML) Dependency Relationships CourseSchedule add(c : Course) remove(c :Course) Course A dependency indicates a semantic relationship between two or more elements. The dependency from CourseSchedule to Course exists because Course is used in both the add and remove operations of CourseSchedule.

14 Programming With Java ICS201 Software Design (UML) Generalization Relationships Person A generalization connects a subclass to its superclass. It denotes an inheritance of attributes and behavior from the superclass to the subclass and indicates a specialization in the subclass of the more general superclass. Student

15 Programming With Java ICS201 Software Design (UML) Association Relationships If two classes in a model need to communicate with each other, there must be link between them. An association denotes that link. Instructor Student

16 Programming With Java ICS201 Software Design (UML) Association Relationships (Cont’d) We can indicate the multiplicity of an association by adding multiplicity adornments to the line denoting the association. The example indicates that a Student has one or more Instructors: Instructor Student 1..*

17 Programming With Java ICS201 Software Design (UML) Association Relationships (Cont’d) We can also indicate the behavior of an object in an association (i.e., the role of an object) using rolenames. Instructor Student 1..* learns fromteaches

18 Programming With Java ICS201  Consider the part-whole association:  Modelling the “part-whole” association as an Aggregation makes the “part-whole” relationship explicit and conveys greater meaning: More Advanced Features: Aggregation **** VehiclePart Vehicle isPartOf hasParts

19 Programming With Java ICS201 19 Composition –A composition is a strong kind of aggregation if the aggregate is destroyed, then the parts are destroyed as well

20 Programming With Java ICS201 University Of Hail20 Inheritance Diagrams ( Generalization) oAn inheritance diagram shows the relationship between a base class and its derived class(es).  Normally, only as much of the class diagram is shown as is needed.  Note that each derived class may serve as the base class of its derived class(es). oEach base class is drawn above its derived class(es)  An upward pointing arrow is drawn between them to indicate the inheritance relationship.

21 Programming With Java ICS201 University Of Hail21 Inheritance Diagrams oThe arrows also help in locating method definitions. oTo look for a method definition for a class:  Examine the class definition first.  If the method is not found, the path of connecting arrows will show the order and direction in which to search.  Examine the parent class indicated by the connecting arrow.  If the method is still not found, then examine this parent's parent class indicated by the connecting arrow.  Continue until the method is found, or until the top base class is reached.

22 Programming With Java ICS201 University Of Hail22 A Class Hierarchy in UML Notation StudentEmployee StaffUndergraduate Person GraduateFaculty Arrows go from a derived class to its base class.

23 Programming With Java ICS201 University Of Hail23 Some Details of a Class Hierarchy + setName(String newName): void + getName( ): String + toString( ): String + sameName(Person otherPerson): boolean - name: String Person + set(String newName, int newStudentNumber): void + getStudentNumber( ): int + setStudentNumber(int newStudentNumber ): void + toString( ): String + equals(Object otherObject): boolean - studentNumber: int Student

24 Programming With Java ICS201 oA pattern in programming is a kind of template or outline of a software task. A pattern is the outline of a reusable solution to a general problem Introduction to Patterns

25 Programming With Java ICS201 Design Patterns Patterns capture solutions to particular design problems. They provide for the reuse of successful designs. A design pattern is like a template that can be applied in many different situations. A design pattern provides an abstract description of a design problem and how a general arrangement of elements (classes and objects) solves it.

26 Programming With Java ICS201 Design Pattern A design pattern is a general reusable solution to a commonly occurring problem in software design. is not a finished design that can be transformed directly into code. is a description or template for how to solve a problem that can be used in many different situations. 26

27 Programming With Java ICS201 27 Why a Design Pattern Reusability Helping new designers to have a more flexible and reusable design Improving the documentation and maintenance of existing system

28 Programming With Java ICS201 Different models of cars, produced by assembling different parts and following different designs.

29 Programming With Java ICS201 Different design plans for producing different models of products (cars). Allow to produce different products in a quick amount of time. You don’t have to develop system from scratch

30 Programming With Java ICS201 Elements of a Design Pattern Pattern Name Describe a design problems and its solutions in a word or two Description of the problem Describes the problem and its context. A short sentence or two raising the main difficulty. The Solution Describes the elements that make up the design, their relationships, and collaborations. The Consequences The results and tradeoffs of applying the pattern. Benefits of applying a pattern.

31 Programming With Java ICS201 University Of Hail31 Container-Iterator Pattern oA container is a class whose objects hold multiple pieces of data.  An array is a container.  Vectors and linked lists are containers.  A String contains the characters. oAny construct that can be used to cycle through all the items in a container is an iterator.  An array index i is an iterator for an array for (int i; i < a.length; i++) Do something with a[i] oThe Container-Iterator pattern describes how an iterator is used on a container.

32 Programming With Java ICS201 University Of Hail32 Adaptor Pattern oThe Adaptor pattern transforms one class into a different class without changing the underlying class, but by simply adding a new interface. oFor example, one way to create a stack data structure is to start with an array, then add the stack interface. oThe adopter pattern says start with a container, like an array, and add an interface, like the stack interface.

33 Programming With Java ICS201 33 The Adapter Pattern Problem: –How to obtain the power of polymorphism when reusing a class whose methods have the same function but not the same signature Solution: –Create an adapter class and associate existing class to it, called ‘Adaptee’ –The polymorphic methods of Adapter can ‘delegate’ to methods of the Adaptee. –Client calls operations on an Adapter instance, which then calls Adaptee’s operations to carry out the request

34 Programming With Java ICS201 34 Adapter

35 Programming With Java ICS201 Adapter Example:

36 Programming With Java ICS201 University Of Hail36 The Model-View-Controller Pattern oThe Model-View-Controller pattern is a way of separating the I/O task of an application from the rest of the application.  The Model part of the pattern performs the heart of the application.  The View part is the output part; it displays a picture of the Model's state.  The Controller is the input part; it relays commands from the user to the Model.

37 Programming With Java ICS201 University Of Hail37 Example (The Model-View-Controller Pattern) oThe Model might be a container class, such as an array. oThe View might display one element of the array. oThe Controller would give commands to display the element at a specified index. oThe Model would notify the View to display a new element whenever the array contents changed or a different index location was given.

38 Programming With Java ICS201 University Of Hail38 The Model-View-Controller Pattern

39 Programming With Java ICS201 University Of Hail39 A Sorting Pattern oThe most efficient sorting algorithms all seem to follow a divide-and-conquer strategy. oGiven an array a, and using the < operator, these sorting algorithms:  Divide the list of elements to be sorted into two smaller lists (split).  Recursively sort the two smaller lists (sort).  Then recombine the two sorted lists (join) to obtain the final sorted list.

40 Programming With Java ICS201 University Of Hail40 A Sorting Pattern 1.The method split rearranges the elements in the interval a[begin] through a[end] and divides the rearranged interval at splitPoint. 2.The two smaller intervals are then sorted by a recursive call to the method sort. 3.After the two smaller intervals are sorted, the method join combines them to obtain the final sorted version of the entire larger interval.  Note that the pattern does not say exactly how the methods split and join are defined. Different definitions of split and join will yield different sorting algorithms.

41 Programming With Java ICS201 University Of Hail41 Divide-and-Conquer Sorting Pattern

42 Programming With Java ICS201 University Of Hail42 Merge Sort o The simplest realization of this sorting pattern is the merge sort. 1.The definition of split is very simple : it divides the array into two intervals without rearranging the elements. 2.The merging starts by comparing the smallest elements in each smaller sorted interval. 3.The smaller of these two elements is the smallest of all the elements in either subinterval. 4.The method join makes use of a temporary array, and it is to this array that the smaller element is moved. 5.The process is repeated with the remaining elements in the two smaller sorted intervals to find the next smallest element, and so forth.

43 Programming With Java ICS201 University Of Hail43 Quick Sort oIn the quick sort realization of the sorting pattern, the definition of split is quite sophisticated, while join is utterly simple: 1.An arbitrary value called the splitting value is chosen. 2.The elements in the array are rearranged: i.All elements less than or equal to the splitting value are placed at the front of the array. ii.All elements greater than the splitting value are placed at the back of the array. iii.The splitting value is placed in between the two. 3.The smaller elements are then sorted by a recursive call, as are the larger elements. 4.These two sorted segments are combined. oThe join method actually does nothing.

44 Programming With Java ICS201 University Of Hail44 Restrictions on the Sorting Pattern oLike all patterns, the sorting pattern has some restrictions on where it applies:  It applies only to types for which the < operator is defined.  It applies only to sorting into increasing order. oThe pattern can be made more general, however:  The < operator can be replaced with a boolean valued method called compare.  The compare method would take two arguments of the base type of the array, and return true or false based on the comparison criteria.

45 Programming With Java ICS201 University Of Hail45 Efficiency of the Sorting Pattern oThe most efficient implementations of the sorting pattern are those for which the split method divides the array into two large parts.  The merge sort split divides the array into two roughly equal parts, and is very efficient.  The quick sort split may or may not divide the array into two roughly equal parts.


Download ppt "Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns."

Similar presentations


Ads by Google