Transcript Patterns

Programming With Java
ICS201
Chapter 12
UML and Patterns
University Of Hail
1
Programming With Java
ICS201
Introduction to Patterns
o 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
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.
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
Programming With Java
ICS201
Why a Design Pattern
• Reusability
• Helping new designers to have a more flexible and
reusable design
• Improving the documentation and maintenance of
existing system
5
Programming With Java
ICS201
Different models of cars, produced by assembling
different parts and following different designs.
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
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.
Programming With Java
ICS201
Container-Iterator Pattern
o 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.
o 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]
o The Container-Iterator pattern describes how an iterator
is used on a container.
University Of Hail
9
Programming With Java
ICS201
Adaptor Pattern
o The Adaptor pattern transforms one class into a different
class without changing the underlying class, but by
simply adding a new interface.
o For example, one way to create a stack data structure is
to start with an array, then add the stack interface.
o The adopter pattern says start with a container, like an
array, and add an interface, like the stack interface.
University Of Hail
10
Programming With Java
ICS201
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
11
Programming With Java
ICS201
Adapter
12
Programming With Java
ICS201
Adapter
• Example:
Programming With Java
ICS201
The Model-View-Controller Pattern
o 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
14
Programming With Java
ICS201
Example
(The Model-View-Controller Pattern)
o The Model might be a container class, such as an array.
o The View might display one element of the array.
o The Controller would give commands to display the
element at a specified index.
o 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
15
Programming With Java
ICS201
The Model-View-Controller Pattern
University Of Hail
16
Programming With Java
ICS201
A Sorting Pattern
o The most efficient sorting algorithms all seem to follow a
divide-and-conquer strategy.
o 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
17
Programming With Java
ICS201
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.
University Of Hail
18
Programming With Java
ICS201
Divide-and-Conquer Sorting Pattern
University Of Hail
19
Programming With Java
ICS201
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.
University Of Hail
20
Quick Sort
Programming With Java
ICS201
o In 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.
o The join method actually does nothing.
University Of Hail
21
Restrictions on the Sorting Pattern
Programming With Java
ICS201
o 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.
o 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
22
Programming With Java
ICS201
Efficiency of the Sorting Pattern
o 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
23