it era tor

Upload: kishoreramana

Post on 07-Apr-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 It Era Tor

    1/15

    Design Patterns

    A Pattern presents a proven advice instandard format

    A design pattern gives advice about aproblem in software design.

    Attributes of s design Pattern:

    1. Name

    2. Context3. Problem

    4. Solution

  • 8/6/2019 It Era Tor

    2/15

    What are iterators

    Iterators helps to iterate or traverse the

    collections.[ From Start to End of

    collection] Advantages :

    1. Iterators does not expose internal

    structure [only return elements for use]

    2. More than one iterators can be attached

    to a single collection.

  • 8/6/2019 It Era Tor

    3/15

    Iterators cont.

    Consider a LinkedList

    LinkedList list = new LinkedList()

    How you will traverse in Java How you will traverse in C

    ListIterator Ltr = list.listIterator();

    While(Ltr.hasNext()

    {

    Object current = Ltr.next();

    .

    }

    Link Ltr = list.head;

    While(Ltr != null)

    {

    Object current = Ltr.data;

    Ltr = Ltr.next;

    }Disadvantages

    1. Internal structure links Exposed to user

    2. Only one way traversal at a time

  • 8/6/2019 It Era Tor

    4/15

    The Iterator Pattern

    Intent

    1. Provide a way to access the elements of an aggregate object sequentially

    without exposing its underlying representation

    2. An aggregate object is an object that contains other objects for the

    purpose of grouping those objects as a unit. It is also called a container ora collection Examples are a linked list and a hash table.

    3. Also Known As Cursor

    Motivation

    1. An aggregate object such as a list should allow a way to traverse its

    elements without exposing its internal structure

    2. It should allow different traversal methods

    3. It should allow multiple traversals to be in progress concurrently

  • 8/6/2019 It Era Tor

    5/15

    Solution

    1. Define an iterator that fetches one element at a time2. Each iterator object keeps track of the position of the next element

    3. If there are several aggregate/iterator variations, it is best if the

    aggregate and iterator classes realize common interface types.

  • 8/6/2019 It Era Tor

    6/15

    Name in Design Pattern Actual Name (linked lists)

    Aggregate List

    ConcreteAggregate LinkedList

    Iterator ListIterator

    ConcreteIterator anonymous class implementing

    ListIterator

    createIterator() listIterator()

    next() next()

    isDone() opposite of hasNext()

    currentItem() return value of hasNext()

  • 8/6/2019 It Era Tor

    7/15

    Model View Controller

    1. Some programs have multiple editable views

    2. Example: HTML Editor

    (a) WYSIWYG view(b) structure view

    (c) source view

    3. Editing one view updates the other

    4. Updates seem instantaneous

  • 8/6/2019 It Era Tor

    8/15

    1. Model: data structure, no visual representation

    2. Views: visual representations

    3. Controllers: user interaction

    4. Views/controllers update model

    5. Model tells views that data has changed

    6. Views redraw themselves

  • 8/6/2019 It Era Tor

    9/15

    Observer Pattern

    Model notifies views when something interesting happens

    Button notifies action listeners when something interestinghappens

    Views attach themselves to model in order to be notified

    Action listeners attach themselves to button in order to be

    notified Generalize: Observers attach themselves to subject

    Context

    1. An object, called the subject, is source of events

    2. One or more observer objects want to be notified

    when such an event occurs.

  • 8/6/2019 It Era Tor

    10/15

    Solution

    1. Define an observer interface type. All concrete observers implementit.

    2. The subject maintains a collection of observers.

    3. The subject supplies methods for attaching and detaching

    observers.

    4. Whenever an event occurs, the subject notifies all observers.

  • 8/6/2019 It Era Tor

    11/15

    Name in Design Pattern Actual

    Name (Swing buttons)

    Subject JButton

    Observer ActionListener

    ConcreteObserver the class that implements the ActionListener

    interface type

    attach() addActionListener()

    notify() actionPerformed()

  • 8/6/2019 It Era Tor

    12/15

    Strategy Pattern

    Context:

    A class can benefit from different

    variants for an algorithm Clients sometimes want to replace

    standard algorithms with custom

    versions

  • 8/6/2019 It Era Tor

    13/15

    Solution Define an interface type that is an abstraction for the algorithm

    Actual strategy classes realize this interface type.

    Clients can supply strategy objects

    Whenever the algorithm needs to be executed, the context class

    calls the appropriate methods of the strategy object

  • 8/6/2019 It Era Tor

    14/15

    Name in Design Pattern Actual Name (layout management)

    Context Container

    Strategy LayoutManager

    ConcreteStrategy a layout manager such as BorderLayout

    doWork() a method such as layoutContainer

    Example : Layout Managers

  • 8/6/2019 It Era Tor

    15/15

    Name in Design Pattern Actual Name (sorting)

    Context Collections

    Strategy Comparator

    ConcreteStrategy a class that implements

    Comparator

    doWork() compare

    Example : Sorting