230 likes | 331 Views
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.
E N D
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 : • Iterators does not expose internal structure [only return elements for use] • 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 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 • Internal structure links Exposed to user • Only one way traversal at a time
The Iterator Pattern • Intent • Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation • 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. • Also Known As Cursor • Motivation • An aggregate object such as a list should allow a way to traverse its elements without exposing its internal structure • It should allow different traversal methods • It should allow multiple traversals to be in progress concurrently
Solution • Define an iterator that fetches one element at a time • Each iterator object keeps track of the position of the next element • If there are several aggregate/iterator variations, it is best if the • aggregate and iterator classes realize common interface types.
Model View Controller • Some programs have multiple editable views • Example: HTML Editor • (a) WYSIWYG view • (b) structure view • (c) source view • Editing one view updates the other • Updates seem instantaneous
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 • An object, called the subject, is source of events • One or more observer objects want to be notified • when such an event occurs.
Solution • Define an observer interface type. All concrete observers implement it. • The subject maintains a collection of observers. • The subject supplies methods for attaching and detaching observers. • Whenever an event occurs, the subject notifies all observers.
Observer Pattern in Java • Observer Interface • To be implemented by the listeners or Observers or views • A class can implement the observer interface when it wants to be informed of the change in the Observable (Subject/Model) Object void update(Observable o, Object arg) 3. Update method is called whenever state of subject is changed.
Observable class • Extends Object , Package java.util. • Represents subject/model which contains raw data. • If you want to create model / subject , make your class subclass of Observable • Can have one or more Observers. • Important methods : • void addObserver(Observer o) // Adds Observer • int countObservers(); • void deleteObserver(Observer o) • void deleteObservers() • boolean hasChanged() • boolean clearChanged() • void notifyObservers(), void notifyObservers(Object o)
import java.util.*; class idnumber extends Observable { private int year; private String descipline; private String no; private String idno; idnumber(int year, String descipline, String no) { this.year = year; this.descipline = descipline; this.no = no; idno = year + descipline + no; } public int getYear() { return year ;} public String getDescipline() { return descipline ;} public String getNo() { return no ;}
public void setYear(int year) { this.year = year; setChanged(); notifyObservers(new Integer(year)); } public void setNo(String no) { this.no = no; setChanged(); notifyObservers(no); } public void setDescipline(String descipline) { this.descipline = descipline; setChanged(); notifyObservers(descipline); } public void setIdNumber(int y, String s, String n) { idno = y+s+n; System.out.println("Idno Changed as :"+idno); } } // End of class
class yearobserver implements Observer { public void update(Observable o, Object arg) { if( arg instanceof Integer) { idnumber id = (idnumber) o; Integer year = ((Integer)arg).intValue(); id.setIdNumber(year, id.getDescipline(), id.getNo()); } } } class desciplineobserver implements Observer { public void update(Observable o, Object arg) { if(arg instanceof String ) { idnumber id = (idnumber) o; String descipline = (String)arg; id.setIdNumber(id.getYear(), descipline, id.getNo()); } } }
class Numobserver implements Observer { public void update(Observable o, Object arg) { if(arg instanceof String) { idnumber id = (idnumber) o; String num = (String)arg; id.setIdNumber(id.getYear(), id.getDescipline(), num); } } }
class observerdemo { public static void main(String args[]) { idnumber idno = new idnumber(2000,"A1PS","098"); Observer o1 = new desciplineobserver(); Observer o2 = new yearobserver(); Observer o3 = new Numobserver(); idno.addObserver(o1); idno.addObserver(o2); idno.addObserver(o3); idno.setYear(1999); idno.setDescipline("A7PS"); idno.setNo("001"); } }
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
Some Typical Applications where Strategy can be helpful • Saving Files in Different Formats file.setFormat(<Name of Format>); file.save(); • Compress Files using different algorithms • Various Encryption/Decryption Schemes