1 / 23

Design Patterns

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.

Download Presentation

Design Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. 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

  4. 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

  5. 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.

  6. 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

  7. 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

  8. 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.

  9. 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.

  10. 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.

  11. 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)

  12. 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 ;}

  13. 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

  14. 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()); } } }

  15. 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); } } }

  16. 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"); } }

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

  18. 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

  19. Example : Layout Managers

  20. Example : Sorting

  21. 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

More Related