Download presentation
Presentation is loading. Please wait.
Published byLouisa Rodgers Modified over 9 years ago
1
Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns
2
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
3
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.
4
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. 4
5
Programming With Java ICS201 5 Why a Design Pattern Reusability Helping new designers to have a more flexible and reusable design Improving the documentation and maintenance of existing system
6
Programming With Java ICS201 Different models of cars, produced by assembling different parts and following different designs.
7
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
8
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.
9
Programming With Java ICS201 University Of Hail9 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.
10
Programming With Java ICS201 University Of Hail10 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.
11
Programming With Java ICS201 11 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
12
Programming With Java ICS201 12 Adapter
13
Programming With Java ICS201 Adapter Example:
14
Programming With Java ICS201 University Of Hail14 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.
15
Programming With Java ICS201 University Of Hail15 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.
16
Programming With Java ICS201 University Of Hail16 The Model-View-Controller Pattern
17
Programming With Java ICS201 University Of Hail17 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.
18
Programming With Java ICS201 University Of Hail18 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.
19
Programming With Java ICS201 University Of Hail19 Divide-and-Conquer Sorting Pattern
20
Programming With Java ICS201 University Of Hail20 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.
21
Programming With Java ICS201 University Of Hail21 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.
22
Programming With Java ICS201 University Of Hail22 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.
23
Programming With Java ICS201 University Of Hail23 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.