Professional Documents
Culture Documents
It Era Tor
It Era Tor
It Era Tor
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
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(); . }
Disadvantages
1. Internal structure links Exposed to user 2. Only one way traversal at a time
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.
Actual Name (linked lists) List LinkedList ListIterator anonymous class implementing ListIterator listIterator() next() opposite of hasNext() return value of hasNext()
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.
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
Actual Name (layout management) Container LayoutManager a layout manager such as BorderLayout a method such as layoutContainer
Example : Sorting
Actual Name (sorting) Collections Comparator a class that implements Comparator compare
doWork()