450 likes | 610 Views
Chapter 12 UML and Patterns. Introduction to UML. UML ( Unified Modeling Language) is a graphical language used for designing and documenting OOP software. UML is a software design tools that can be used within the context of any OOP (Object-Oriented programming) language. UML.
E N D
Chapter 12 UML and Patterns University Of Hail
Introduction to UML • UML(Unified Modeling Language) is a graphical language used for designing and documenting OOP software. • UML is a software design tools that can be used within the context of any OOP (Object-Oriented programming) language. University Of Hail
UML • Pseudocode is a way of representing a program in a linear and algebraic manner. • It simplifies design by eliminating the details of programming language syntax. • Graphical representation systems for program design have also been used: • Flowcharts and structure diagrams for example. • Unified Modeling Language (UML) is yet another graphical representation formalism. • UML is designed to reflect and be used with the OOP philosophy. University Of Hail
Types of UML Diagrams • Use Case Diagram • Class Diagram • Sequence Diagram • Collaboration Diagram • State Diagram • ….
Rectangle Rectangle Rectangle getArea height height resize width width getArea resize 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 Rectangle height: int width: int getArea(): int resize(int,int)
ClassName attributes operations UML Class Diagram • A class diagram is divided up into three sections: • The top section contains the class name. • The middle section contains the data specification for the class (attributes). • The bottom section contains the actions or methods of the class (operations). University Of Hail
ClassName attributes operations Class Names 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.
Class Attributes (Cont’d) Attributes are usually listed in the form: attributeName : Type Person + name : String # address : Address # birthdate : Date / age : Date - ssn : Id Attributes can be: + public # protected - private / derived 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 Software Design (UML)
Person name : String address : Address birthdate : Date ssn : Id eat sleep work play Class Operations Operations describe the class behavior and appear in the third compartment. • Each method in a UML diagram is indicated by the name of the method, followed by its parameter list, a colon (:), and its returned type. Software Design (UML)
UML Class Diagram • A class diagram need not give a complete description of the class. • If a given analysis does not require that all the class members be represented, then those members are not listed in the class diagram. • Missing members are indicated with an ellipsis (three dots). University Of Hail
Class name attributes Class methods Example (UML Class Diagram) University Of Hail
Relationships • In UML, object interconnections (logical or physical), are modeled as relationships. • There are three kinds of relationships in UML: • dependencies • generalizations • associations Software Design (UML)
Dependency Relationships A dependency indicates a semantic relationship between two or more elements. The dependency from CourseScheduleto Course exists because Course is used in both the add and remove operations of CourseSchedule. CourseSchedule Course add(c : Course) remove(c :Course) 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 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. Student Instructor 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: Student Instructor 1..* 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. teaches learns from Student Instructor 1..* 1..* Software Design (UML)
isPartOf * * * * Vehicle VehiclePart hasParts More Advanced Features: Aggregation • Consider the part-whole association: • Modelling the “part-whole” association as an Aggregation makes the “part-whole” relationship explicit and conveys greater meaning:
Composition • A composition is a strong kind of aggregation • if the aggregate is destroyed, then the parts are destroyed as well
Inheritance Diagrams (Generalization) • An 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). • Each base class is drawn above its derived class(es) • An upward pointing arrow is drawn between them to indicate the inheritance relationship. University Of Hail
Inheritance Diagrams • The arrows also help in locating method definitions. • To 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. University Of Hail
Person Student Employee Staff Undergraduate Graduate Faculty A Class Hierarchy in UML Notation Arrows go from a derived class to its base class. University Of Hail
Person - name: String + setName(String newName): void + getName( ): String + toString( ): String + sameName(Person otherPerson): boolean Student - studentNumber: int + set(String newName , int newStudentNumber): void + getStudentNumber( ): int + setStudentNumber(int newStudentNumber ): void + toString( ): String + equals(Object otherObject): boolean Some Details of a Class Hierarchy University Of Hail
Introduction to Patterns • A 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
Design Patterns Patterns capture solutions to particular design problems. They provide for the reuseof 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.
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.
Why a Design Pattern • Reusability • Helping new designers to have a more flexible and reusable design • Improving the documentation and maintenance of existing system
Different models of cars, produced by assembling different parts and following different designs.
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
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.
Container-Iterator Pattern • A 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. • Any 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] • The Container-Iterator pattern describes how an iterator is used on a container. University Of Hail
Adaptor Pattern • The Adaptor pattern transforms one class into a different class without changing the underlying class, but by simply adding a new interface. • For example, one way to create a stack data structure is to start with an array, then add the stack interface. • The adopter pattern says start with a container, like an array, and add an interface, like the stack interface. University Of Hail
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
Adapter • Example:
The Model-View-Controller Pattern • The 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. University Of Hail
Example (The Model-View-Controller Pattern) • The Model might be a container class, such as an array. • The View might display one element of the array. • The Controller would give commands to display the element at a specified index. • The Model would notify the View to display a new element whenever the array contents changed or a different index location was given. University Of Hail
The Model-View-Controller Pattern University Of Hail
A Sorting Pattern • The most efficient sorting algorithms all seem to follow a divide-and-conquer strategy. • Given 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. University Of Hail
A Sorting Pattern • The method split rearranges the elements in the interval a[begin] through a[end] and divides the rearranged interval at splitPoint. • The two smaller intervals are then sorted by a recursive call to the method sort. • 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. University Of Hail
Divide-and-Conquer Sorting Pattern University Of Hail
Merge Sort • The simplest realization of this sorting pattern is the merge sort. • The definition of split is very simple : it divides the array into two intervals without rearranging the elements. • The merging starts by comparing the smallest elements in each smaller sorted interval. • The smaller of these two elements is the smallest of all the elements in either subinterval. • The method join makes use of a temporary array, and it is to this array that the smaller element is moved. • The process is repeated with the remaining elements in the two smaller sorted intervals to find the next smallest element, and so forth. University Of Hail
Quick Sort • In the quick sort realization of the sorting pattern, the definition of split is quite sophisticated, while join is utterly simple: • An arbitrary value called the splitting value is chosen. • The elements in the array are rearranged: • All elements less than or equal to the splitting value are placed at the front of the array. • All elements greater than the splitting value are placed at the back of the array. • The splitting value is placed in between the two. • The smaller elements are then sorted by a recursive call, as are the larger elements. • These two sorted segments are combined. • The join method actually does nothing. University Of Hail
Restrictions on the Sorting Pattern • Like 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. • The 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. University Of Hail
Efficiency of the Sorting Pattern • The 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. University Of Hail