It Era Tor

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 15

Design Patterns

A Pattern presents a proven advice in standard format A design pattern gives advice about a problem in software design. Attributes of s design Pattern:
1. Name 2. Context 3. Problem 4. Solution

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.

Iterators cont.
Consider a LinkedList LinkedList list = new LinkedList() How you will traverse in Java
ListIterator Ltr = list.listIterator(); While(Ltr.hasNext() { Object current = Ltr.next(); . }

How you will traverse in C


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

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 or a 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

Solution
1. Define an iterator that fetches one element at a time 2. 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.

Name in Design Pattern Aggregate ConcreteAggregate Iterator ConcreteIterator

Actual Name (linked lists) List LinkedList ListIterator anonymous class implementing ListIterator listIterator() next() opposite of hasNext() return value of hasNext()

createIterator() next() isDone() currentItem()

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

1. 2. 3. 4. 5. 6.

Model: data structure, no visual representation Views: visual representations Controllers: user interaction Views/controllers update model Model tells views that data has changed Views redraw themselves

Observer Pattern
Model notifies views when something interesting happens Button notifies action listeners when something interesting happens 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.

Solution
1. Define an observer interface type. All concrete observers implement it. 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.

Name in Design Pattern Subject Observer ConcreteObserver

Actual Name (Swing buttons) JButton ActionListener the class that implements the ActionListener interface type addActionListener() actionPerformed()

attach() notify()

Strategy Pattern
Context: A class can benefit from different variants for an algorithm Clients sometimes want to replace standard algorithms with custom versions

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

Example : Layout Managers

Name in Design Pattern Context Strategy ConcreteStrategy doWork()

Actual Name (layout management) Container LayoutManager a layout manager such as BorderLayout a method such as layoutContainer

Example : Sorting

Name in Design Pattern Context Strategy ConcreteStrategy

Actual Name (sorting) Collections Comparator a class that implements Comparator compare

doWork()

You might also like