Tải bản đầy đủ (.pdf) (25 trang)

Software design: Lecture 39 - Sheraz Pervaiz

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (400.46 KB, 25 trang )

1

Software Design
Lecture : 39


2

Category 3: Behavioral Patterns
This  category  of  patterns  are  concerned  with 
algorithms  and  assignments  of  responsibilities 
between objects.

They  describe  not  just  patterns  of  objects  or 
classes  but  also  the  pattern  of  communication 
between them.


3

Behavioral Design Patterns
This  category  will  shift  the  focus  from  flow  of 
control to concentrate just on the way objects are 
interconnected.
These    patterns  are  concerned  with  the 
assignment  of  responsibilities  between  objects, 
or,  encapsulating  behavior  in  an  object  and 
delegating requests to it. 


4



Iterator  Design Pattern
OR 
Well Managed Collection


5

Motivation for Iterator Design Pattern
There  are  different  data  structure  to  store  data  or 
objects in a collection i­e Array, Array list , Vector,   
stack, link list etc.

In  other  words  there  are  different  container  which 
contain different object and to access the content of 
each container different mechanism is applied.


6

Array Vs Vector
Arrays are Static data 
structure

Vectors are dynamic data 
structure

Arrays are data type specific

Vector contains a dynamic 

list of references to other 
objects

Since size is fixed memory is 
allocated for array

Due to dynamic behavior 
Vectors are memory efficient 
way of storing data


7

Internal Iterator ­ Cursor
 The collection itself offers methods to allow a client to visit 
different objects within the collection. 
 For example, the java.util.ResultSet class contains the data 
and also offers methods such as next() to navigate through 
the item list.

 There can be only one iterator on a collection at any given 
time.
  The collection has to maintain or save the state of iteration.


8

External Iterators
 The  iteration  functionality  is  separated  from  the 
collection and kept inside a different object referred to as 

an iterator

 Typical  example  include  Vector  in  which  elements  are 
stored inside vector but are traversed using Enumeration 
for instance as shown in the example in next slide.


9

import java.util.*; 
public class Program { 
public static void main(String[] args) 
{
Vector v = new Vector(); 
v.add(0, "Apple");
 v.add(1, "Orange");
 v.add(2, "Banana");
//Assigning Vector to Iterator
Enumeration vEnum = v.elements(); 
while (vEnum.hasMoreElements()) 

System.out.println(vEnum.nextElement()); 
}}} 


10

Output of the Program
Apple
Orange

Banana


11

Iterator Pattern Defined
“The  Iterator  Design  pattern  provides  a  way  to  access 
the  element  of  aggregate  object  sequentially  without 
knowing it’s underlying representation”


12

Intend of Iterator Design Pattern
 Allows  a  client  object  to  access  the  contents  of  a 
container  in  a  sequential  manner,  without  having  any 
knowledge  about  the  internal  representation  of  its 
contents.

 Client should not be involved in the internal traversal of  
the contents of the container.


13

Discussion on Iterator Design 
Pattern
Uniform  method  of  accessing  elements  of   
collection  without  knowing  the  underlying 
representation.


We  can  write  polymorphic  code  to  access  the 
elements of underlying aggregate objects.


14

Key idea behind Iterator
The  idea  is  to  take  out  the  responsibility  for 
access and traversal out of the collection and put 
them in the Iterator Object.

Iterator  class  will  define  an  interface  for 
accessing the element of the collection.


15

Iterator pattern
The  abstraction  provided  by  the  Iterator  pattern 
allows  you  to  modify  the  collection  implementation 
without making any changes outside of collection. It 
enables  you  to  create  a  general  purpose  GUI 
component  that  will  be  able  to  iterate  through  any 
collection of the application. 


16

Classes in Iterator Pattern

 The  Iterator  pattern  suggests  that  a  Container  object 
should  be  designed  to  provide  a  public  interface  in  the 
form of an Iterator object   for different client objects to 
access its contents.

   An  Iterator  object  contains    public  methods  to  allow  a 
client object to navigate through the list of objects within 
the container


17

Class Diagram of Iterator Design 
Pattern


18


19


20

Consequences
 Benefits
i.   Simplifies  the  interface of  the  Aggregate by not 
polluting it with traversal methods
ii.Supports multiple, concurrent traversals
iii.Supports variant traversal techniques

 Liabilities
    None!


21

Example 
 This example is using a collection of books and 
it  uses  an  iterator  to  iterate  through  the 
collection.


22

Class Diagram


23

interface IIterator
{
public boolean hasNext();
public Object next();
}
interface IContainer
{
public IIterator createIterator();
}



24

class BooksCollection implements IContainer
{
private String m_titles[] = {"Design Patterns","1","2","3","4"};

public IIterator createIterator()
{
BookIterator result = new BookIterator();
return result;
}


25

private class BookIterator implements IIterator
{
private int m_position;
public boolean hasNext()
{
if (m_position < m_titles.length)
return true;
else
return false;
}
public Object next()
{
if (this.hasNext())
return m_titles[m_position++];
else

return null;
}}}


×