6'170 Recitation 7 - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

6'170 Recitation 7

Description:

{ public int compare(Object o1, Object o2) { if ((o1 instanceof ... GUI helper classes (drawing cars etc) Utility classes (e.g. TimeProfiler) PS6 Checkpoint! ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 21
Provided by: asfandya
Category:
Tags: recitation

less

Transcript and Presenter's Notes

Title: 6'170 Recitation 7


1
6.170 Recitation 7
  • Asfandyar Qureshi

2
Anonymous Classes Huh?
  • Comparator myComparator new Comparator()
  • public int compare(Object o1, Object o2)
  • if ((o1 instanceof String) (o2
    instanceof String))
  • String s1 (String)o1
  • String s2 (String)o2
  • return -s1.compareTo(s2)
  • else
  • return (o1.hashCode() -
    o2.hashCode())
  • // java.util.Comparator is an interface
  • TreeSet set new TreeSet(myComparator)
  • set.add("a") set.add("b") set.add("c")
  • System.out.println(set)

3
Design Patterns Overview
  • Design patterns are
  • High level programming idioms
  • Standard solutions to common problems.
  • Useful design buzzword
  • Examples
  • Encapsulation and Data Hiding
  • Subclassing and Interfaces (Inheritance)
  • Exceptions
  • Iterators

4
Design Patterns Restraint
  • When to use them?
  • Often there are trade-offs (flexibility/performanc
    e)
  • Dont apply them indiscriminately!
  • Delay, get something basic first, then improve it
    once you understand it better.
  • If youre design has a problem, consider patterns
    that address that problem.
  • Design patterns and Understandability
  • Decrease (add indirection, increase code size)
  • Increase (modularity, ease description)

5
Design Patterns
  • Creational
  • Factory
  • Singleton
  • Prototypes
  • Structural
  • Composite
  • Adaptor
  • Wrapper
  • Proxy, Decorator, etc
  • Behavioural
  • Model / View (Observer)
  • Blackboard
  • Visitor

6
Pattern Singleton
  • Gaurantees that only one object of a particular
    class ever exists.
  • class HeartSuit extends CardSuit
  • private static final
  • HeartSuit INSTANCE
  • private HeartSuit()
  • public static Hearts getInstance()
  • if (INSTANCE null) INSTANCE new
    HeartSuit()
  • return INSTANCE
  • Why not make every method static?
  • Most useful for large expensive objects
  • Variant have x instances instead of 1 (think
    CardSuit)

7
Pattern Adaptors
  • Purpose convert the interface of a class into
    another interface that clients expect.
  • Think of the driver classes from PS4 and PS5
  • They used delegation can also use subclassing
  • Can be necessary if you want to reuse code, but
    that code does not satisfy a required interface
  • Simpler to write an adapter
  • Harder to refactor code to conform to new
    interface
  • Testing adaptors
  • You still need to test the adaptor!
  • Test with the adaptors specs

8
Pattern Factory Methods
  • Problem with Java constructors
  • Returned object is always of the given type,
    never of a subtype.
  • Factory methods try to solve this
  • Construct and return an object that is either of
    the specified type OR of a subtype
  • The intent of this pattern is to define an
    interface for creating an object
  • Parameters (e.g. file contents) and subclassing
    can determine the exact type of the returned
    object

9
Factory Methods Example
  • class JPEGImage extends Image
  • class GIFImage extends Image
  • class SVGImage extends Image
  • Image readImageFromFile(File filename)
  • throws IOException

10
Factory Objects Example
  • Class JPEGFactory extends ImageFactory
  • Image readImageFromFile(File filename)
  • throws IOException
  • Image localizeImage (Image image)
  • class someGUIclass
  • ImageFactory factory
  • public someGUIclass()
  • factory new JPEGFactory()
  • // use factory to read images

11
Pattern Observer
  • The Observer pattern allows one or more objects
    (the observers) to watch another (the subject)
  • Defines a publish / subscribe relationship
  • Whenever something interesting happens at the
    subject, all the observers are notified.
  • Event at the subject gets propagated to observers
  • Key Decouples observer from subject
  • Subject doesnt need to know anything special
    about observers.
  • Observers take the initiative and subscribe to
    events from the subject.

12
Pattern Observer
13
Observers Java Support
  • class Observable
  • public void addObserver(Observer o)
  • public void deleteObserver(Observer o)
  • public void setChanged()
  • public void notifyObservers()
    notifyObservers(null)
  • public void notifyObservers(Object arg)
  • Interface Observer
  • public void update(Observable o, Object arg)

14
Observers Java Support
  • class Logger extends Observable
  • public void log(String entry)
  • entry getCurrentTimeString() entry
  • logList.add(entry)
  • setChanged()
  • notifyObservers(entry)
  • ...
  • class EchoObserver implements Observable
  • public void update(Observable o, Object arg)
  • System.out.println(arg)
  • Logger logger new Logger()
  • logger.log(before we added the observer)
  • logger.addObserver(new EchoObserver())
  • logger.log(afterwards)

15
Pattern Blackboard
  • Generalizes observer pattern
  • Multiple models multiple views
  • Models publish data on the blackboard
  • Views react if the data is relevant
  • insert filters
  • Example MIT mailing lists
  • Anyone on csail-discuss_at_mit.edu?

16
Blackboard
  • class Blackboard
  • public insertFilter (Filter msgFilter, Observer
    observer)
  • filters.put(msgFilter, observer)
  • public publishMessage(Message msg, Observable
    publisher)
  • Iterator iter filters.values.iterator()
  • while (iter.hasNext())
  • Filter filter (Filter)iter.next()
  • if (filter.interesting(msg))
  • ((Observer)filters.get(filter)).update(pu
    blisher, msg)

17
Paradigm Events
  • Event driven programming
  • Think of system in terms of events
  • When an event occurs, do something, handle it
  • Network data arrives
  • Spreadsheet data changes (update charts)
  • Someone clicks on a button
  • Java (and many other GUI APIs) are structured
    around events and event handlers
  • Youll see lots of observers/listeners in Swing
  • More efficient to polling.
  • Event driven programming is also powerful in many
    other contexts (e.g. efficiency see 6.824)

18
Quick Overview PS6
  • Two weeks to complete longer than others
  • Overview
  • Graph Building (read maps from TigerDB)
  • Traffic Sources (inject cars into simulation)
  • Traffic Monitors (monitor flow of cars)
  • Programmatic Interface
  • Graphical User Interface
  • Youll have to learn Java Swing programming
  • Documentation
  • Miniature version of final project documentation

19
PS6 Overview
  • Premature Optimization
  • Get it working first!
  • Use profiling (locate hot-spots) and then
    optimize what you need to.
  • Use the TimeProfiler class we provide you, or
    your favourite Java profiler
  • Staff Provided code
  • Database Reader
  • GUI helper classes (drawing cars etc)
  • Utility classes (e.g. TimeProfiler)

20
PS6 Checkpoint!
  • Binary checkpoint due by Friday
  • Come find any TA or LA to check you off
  • What you have to demonstrate
  • Graph building code
  • Ideally, it should be working
  • But if you havent finished debugging it, you can
    still get checked off
  • Design Overview, Schedule, Test Strategy
  • Convince us that youve started to think about
    these issues doesnt have to be the final MDD
Write a Comment
User Comments (0)
About PowerShow.com