callbacks singelton and wrappers

Upload: abhipankaj

Post on 24-Feb-2018

247 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 Callbacks Singelton and Wrappers

    1/33

    Copyright W. Howde 1

    Lecture 14: Callbacks,

    Singletons and Wrappers

  • 7/25/2019 Callbacks Singelton and Wrappers

    2/33

    Copyright W. Howde 2

    Callback attern

    ! Conte"t: controlling entity # calls$uses controlled

    entity %

    ! roble&:' % can ha(e no control o(er #

    ' How can % initiate so&ething that is under the control

    o) # without taking control o) #*

    ! Solution: # gi(es % a pointer to one o) its

    )unctions to call, or an ob+ect to e"ecute-, when

    the situation occurs

  • 7/25/2019 Callbacks Singelton and Wrappers

    3/33

    Copyright W. Howde

    Callback /b+ects

    ! Controller # will call a &ethod %.b0a incontrollee % with an ob+ect a

    ! a &ust ha(e an e"pected &ethoda.callback0 that is known to % and whichwill be called by % when the callback

    situation arises! # &ust know the na&e o) the &ethod %.b0in % to call

  • 7/25/2019 Callbacks Singelton and Wrappers

    4/33

    Copyright W. Howde 4

    Callbacks and 3 Separation

    ! odel 3iew separation architecture0discussed in

    architecture lecture

    ' separation )acilitates change o) look and )eel- whereodel is retained but 3iew changed

    ' controller is (iew, controllee is &odel

    ' (iew posts callback ob+ect with &odel by calling

    appropriate &ethod in &odel' callback ob+ect &ust ha(e e"pected callback &ethod

  • 7/25/2019 Callbacks Singelton and Wrappers

    5/33

    Copyright W. Howde

    5S Callback 6"a&ple

    ! 7re8uent 5atee 7eature

    ' nor&ally 9; will call get#5ate0 in 5L when user

    indicates this user option' get#5ate0 will return date in)or&ation which 9; will

    display

    ' i) the )ound date has been dated &any ti&es, we want

    5L to display a 9; &essage 0i.e. e"ecute so&ething inthe 9;

    ' 9; registers a 7re8uent5atee call back ob+ect w

  • 7/25/2019 Callbacks Singelton and Wrappers

    6/33

    Copyright W. Howde <

  • 7/25/2019 Callbacks Singelton and Wrappers

    7/33

    Copyright W. Howde =

    6(ent 5elegation and Callbacks

    ! Co&ponents and e(ents, e.g. button b

    ! /ptions

    i i) b is pushed, b>s special response &ethod iscalled

    ii other code tells b what to do when button ispushed. ?egister- a callback0actionListener ob+ect " with b by e"ecuting

    b.add#ctionListener0"@ Callback pattern* sort o)

  • 7/25/2019 Callbacks Singelton and Wrappers

    8/33

    Copyright W. Howde A

    ?elated atterns

    ! Can be used )or Call%ack

    'ublisher$Subscriber

    ! 9eneral pattern

    '/bser(er$/bser(able

    ! Supported by classes in Ba(a library

  • 7/25/2019 Callbacks Singelton and Wrappers

    9/33

    Copyright W. Howde

    ublisher@Subscriber

    ! Special 6(ent anager ob+ect

    ! /b+ects interested in an e(ent, register with

    e(ent &anager! /b+ects who recogniDe e(ent, publish

    occurrence o) e(ent with e(ent &anager

    ! 6(ent &anager in)or&s subscribers'6"pected to ha(e a speci)ic &ethod that will be

    called

  • 7/25/2019 Callbacks Singelton and Wrappers

    10/33

    Copyright W. Howde 1E

    /bser(er$/bser(able

    ! /bser(ers register with /bser(able

    ! /bser(able recogniDes e(ent and calls

    speci)ied &ethod in obser(er

    ! Ba(a /bser(er

    ';nter)ace class, i&ple&ented by obser(ers

    ! Ba(a /bser(able

    'Class, e"tended by callback caller classes

  • 7/25/2019 Callbacks Singelton and Wrappers

    11/33

    Copyright W. Howde 11

    Ba(a /bser(er Class

    ! ;&ple&enters &ust ha(e an update0 &ethod

    which the /bser(able ob+ect " will call

    'update0/bser(able ", /b+ect y

    ! When /bser(able ob+ect " calls the update0

    &ethod )or a registered obser(er, it passes

    itsel) as an identi)ier and also a data ob+ect y

  • 7/25/2019 Callbacks Singelton and Wrappers

    12/33

    Copyright W. Howde 12

    Ba(a /bser(able Class

    ! sers o) this class e"tend it

    ! ;nherited &ethods )ro& /bser(able

    'add/bser(er0/bser(er D

    ! #dds the ob+ect D to the obser(able>s list o)

    obser(ers

    'noti)y/bser(er0/b+ect y! Calls the update0 &ethod )or each o) the obser(ers

    in its list o) obser(ers, passing y as the data ob+ect

  • 7/25/2019 Callbacks Singelton and Wrappers

    13/33

    Copyright W. Howde 1

    Singleton attern

    ! Conte"t: class )or which we only want a single

    instance, and to which we &ay want global access

    ! Solution:' re)erence the ob+ect globally through its class de)inition

    ' class (ariable called instance- whose (alue is an

    instance o) the ob+ect

    ' static &ethod called instance0 that

    ! Creates an instance i) not created yet

    ! ?eturns the instance o) the class

  • 7/25/2019 Callbacks Singelton and Wrappers

    14/33

    Copyright W. Howde 14

    5S Singleton 6"a&ple

    ! Want to ha(e a globally accessible ob+ect

    that records session history statistics

    ! 9ets updated )ro& di))erent places, e.g.

    when di))erent kinds o) &essages are

    generated

    ! se the singleton pattern with a Historyclass

  • 7/25/2019 Callbacks Singelton and Wrappers

    15/33

    Copyright W. Howde 1

    5S Singleton History Class

  • 7/25/2019 Callbacks Singelton and Wrappers

    16/33

    Copyright W. Howde 1s constructor takes an adaptee ob+ect as anargu&ent

    ' #daptor &ethods re)erence the adaptee ob+ect (ia itsadaptee class &ethods

  • 7/25/2019 Callbacks Singelton and Wrappers

    18/33

    Copyright W. Howde 1A

    #dapter @ ;nheritance

    Client

    +request'()

    interface

    Target

    +request'()

    Adaptor

    +request()

    Adaptee

  • 7/25/2019 Callbacks Singelton and Wrappers

    19/33

    Copyright W. Howde 1

    #daptor @ Co&position

    +Adaptor(in adaptee)+request'()

    -Adaptee: adaptee

    Adaptor

    +request()

    Adaptee: adaptee

    Client

    +request'()

    interface

    Target

  • 7/25/2019 Callbacks Singelton and Wrappers

    20/33

    Copyright W. Howde 2E

    #daptor 6"a&ple '

    ri&iti(e 5ata ?ei)ication! /b+ecti)ication o) pri&iti(e data types in

    Ba(a, i.e. wrap an instance in a class

    ! Why*! When pri&iti(e is used as a para&eter, it will be

    passed as call by (alue.

    ' Cannot change its (alue inside the &ethod

    ! ay want to add to )unctionality,

    ' e.g. ;nt is the ob+ecti)ication )or int

    ' ;nt.parse;nt0String s will return the int )or s.

  • 7/25/2019 Callbacks Singelton and Wrappers

    21/33

    Copyright W. Howde 21

    ser 5e)ined

    ?ei)ication 6"a&ple! ;nter)ace

    %ool?e);nter)ace G set0boolean b boolean get0 I

    ! #daptee @ boolean (alue! #daptor

    class %oolean?e) i&ple&ents %ool?e);nter)ace

    G public boolean (al

    public %oolean?e)0boolean "Gthis.(al J "Ipublic set0boolean b Gthis.(al J bI

    public get0 Greturn this.(alI

    I

  • 7/25/2019 Callbacks Singelton and Wrappers

    22/33

    Copyright W. Howde 22

    #daptor 6"a&ple '

    ;nter)ace #daptors! ;n order to carry out certain operations, a

    class &ay be re8uired to i&ple&ent a

    special inter)ace! ;nter)ace &ay contain &any )unctions only

    a )ew o) which are nor&ally rele(ant

    ! Fedious to continually supply nullde)initions )or the unneeded )unctions

  • 7/25/2019 Callbacks Singelton and Wrappers

    23/33

    Copyright W. Howde 2

    ;nter)ace #daptor Strategy

    ! Construct a utility class that i&ple&ents the

    inter)ace and supplies null de)initions )or all

    the &ethods! ser e"tends the utility adaptor class

    instead o) i&ple&enting the inter)ace

    ! ser supplies de)initions )or the &ethods o)interest

  • 7/25/2019 Callbacks Singelton and Wrappers

    24/33

    Copyright W. Howde 24

    Ba(a ;nter)ace #daptor 6"a&ple

    ! WindowListener inter)ace' i&ple&ented by any ob+ect that wants to register itsel)

    as a listener )or e(ents generated by a Window

    ' = &ethods in inter)ace

    ' window closing e(ent that occurs when user clicks " inupper right corner re8uires de)inition )orwindowClosing0 &ethod only

    ! Window#daptor i&ple&ents inter)ace with = null)unctions' e"tend Window#dapter and o(erride windowClosing0

  • 7/25/2019 Callbacks Singelton and Wrappers

    25/33

    Copyright W. Howde 2

    5ecorator Strategy

    ! 5ecorator wrapper@ e&phasis:'9oal is to alter the run ti&e beha(ior o) an

    ob+ect'Wrapper should preser(e the inter)ace

    ! ossible adaptor class approach:'adaptor sublcasses adaptee so has sa&e inter)ace

    'adaptor constructor takes wrappee as anargu&ent, so it is altering it at runti&e

  • 7/25/2019 Callbacks Singelton and Wrappers

    26/33

    Copyright W. Howde 2