day16

33 %
67 %
Information about day16
Entertainment

Published on November 19, 2007

Author: miloung

Source: authorstream.com

Design Patterns:  Design Patterns Observer Pattern:  Observer Pattern Dependence mechanism / publish-subscribe / event handler / constraints / broadcast Let objects propagate information without depending on each other much. Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Observer Pattern:  Observer Pattern Observer update: Subject addDependent: removeDependent: changed: ValueHolder value: value TextView update: observer/ dependent model Observer Pattern: :  Observer Pattern: Registration subject addDependent: observer Notification self changed. self changed: #value Update define update: aSymbol Notification:  Notification Object notifies dependents when information changes by sending itself a #changed message. changed: anArg self dependents do: [:each | each update: anArg] Problem:  Problem A man and dog are in the room. When the dog wants to go out, he barks. When the man hears the dog barking, he opens the door. When the dog wants to go out and the door is open, he leaves. Basic classes:  Basic classes Dog bark / move Person Door open / close / isOpen Object addDependent: changed: update: Collaborations:  Collaborations Person Door openclose Dog bark listen watch Dynamic Model:  Dynamic Model Record order of events, interaction between objects. Dog Person Door Sequence diagram bark open notify go thru door notify close register register unregister notify A Script:  A Script | person door dog | door := Door new close. dog := Dog new. dog door: door. person := Person new. person door: door; dog: dog. dog bark. Door:  Door opened <Boolean> open opened := true. self changed: #open Door:  Door close opened := false. self changed: #close isOpen ^opened Dog:  Dog currentState :: #sleeping, #waiting, #outside bark currentState := #waiting. self changed: #bark door: aDoor door := aDoor. door addDependent: self Dog:  Dog goOut currentState := #outside. door removeDependent: self. self changed: #move Dog:  Dog update: aSymbol (currentState == #waiting) & (aSymbol == #open) ifTrue: [self goOut ] Person:  Person dog: aDog dog := aDog. dog addDependent: self Person:  Person update: aSymbol aSymbol == #bark ifTrue: [door open]. aSymbol == #move ifTrue: [door close] Watcher:  Watcher instance variable: name <String> update: aSymbol Transcript show: subjectName; show: ' '; show: aSymbol; cr name: aString name := aString Slide19:  | person door dog | door := Door new close. door addDependent: (Watcher new name: 'door'). dog := Dog new. door addDependent: dog. dog addDependent: (Watcher new name: 'Fido'). person := Person new. person door: door; dog: dog. dog bark. Improvements:  Improvements Creating method (have class method return initialized object) Compose method (Watcher on: door name: 'door') (door watcherNamed: 'door') Model and Memory Management:  Model and Memory Management Dog allInstances reports a lot of dogs! Garbage collection doesn't help. Object uses a global dictionary to store dependents. Subject must "release" its observers/dependents. Make Dog a subclass of Model:  Make Dog a subclass of Model Model uses an instance variable to store dependents. It does not have to release its observers. Subclasses of Model cause less problems with garbage collection. Class that has dependents should be subclass of Model. Slide23:  If you are not using Model then after the script says dog bark. add the messages dog release. door release. person release Advantage of Observer Pattern:  Advantage of Observer Pattern Easy to add new observers. Coupling between observer and subject is abstract. Disadvantage of Observer Pattern:  Disadvantage of Observer Pattern Often hard to understand relationships between objects in system. Sometimes inefficient. Adding New Observer:  Adding New Observer Suppose room also has a bird, which is usually in a cage. If bird is not in cage and door opens, bird flies out. Bird:  Bird update: aSymbol (aSymbol == #open) & (self isCaged not) ifTrue: [self flyOut] Script must now create a bird and make it depend on the door. Summary:  Summary Observer is an important pattern Used in a UI to make reusable components Reduces coupling by increasing abstraction Abstraction makes programs easier to change, harder to understand Often requires a “script” to set up dependencies Creational Patterns:  Creational Patterns Factory Method Factory Object Abstract Factory Builder Prototype Singleton Abstract Factory:  Abstract Factory Sometimes a group of products are related -- if you change one, you might need to change them all. Solution: Make a single object that can make any of the products. ScrollBar MotifScrollBar PMScrollBar WidgitFactory CreateScrollBar CreateWindow MotifWidgetFactory CreateScrollBar CreateWindow PMWidgetFactory CreateScrollBar CreateWindow Making Abstract Factory:  Making Abstract Factory Give Producer a component called “Factory” Create class Factory Add instance variable “factory” to Producer Change constructor to have line factory:=Factory new Move factory methods to Factory Copy factory method to Factory Change sends of createFoo to “factory createFoo” Refactoring creation:  Refactoring creation Encapsulate in a method – makeProduct (Factory method) Groups of related products => move factory methods to a new object (Abstract Factory) Method too big => method object / Builder User customizable => Prototype Slide33:  Prototype not used much in Smalltalk – use classes as objects instead. Builder often used only to encapsulate building, not provide variation. (No subclassing) Factory method very common in Smalltalk – less common in Java. Instead, use Dependency Injection. Dependency Injection:  Dependency Injection Eliminate dependencies between classes by creating settors for objects, instead of creating them in a constructor describing the dependencies between classes with a script and having a standard module read the script, create the objects, and inject the dependencies. The script:  The script Smalltalkers often use code Smalltalk literal XML Non-standard format Slide36:  windowSpec "UIPainter new openOnClass: self andSelector: #windowSpec" <resource: #canvas> ^#(#FullSpec #window: #(#WindowSpec #label: 'Payroll' #bounds: #(#Rectangle 1245 452 1544 761 ) ) ... WikiWorks configuration file:  WikiWorks configuration file [Server] port=80 directory=.name=Wiki's Home at UIUC accessLog=access.log statusLog=status.log [Wiki] name=CampSmalltalk directory=CampSmalltalk [Wiki] name=VisualWorks directory=VisualWorks Dependency injection:  Dependency injection Makes components more composable Harder to debug a component Can’t see relationship between components in the browser Requires reflection in the programming language Design patterns and refactoring:  Design patterns and refactoring Patterns are most useful for complex systems. Add them later by refactoring Keep system simple – no unnecessary patterns Keep system flexible – all needed patterns Next time:  Next time Read State, Command, Visitor

#value presentations

Add a comment

Related presentations

Related pages

Day16 | hessenauer.org

Zum Teilen auf Google+ anklicken (Wird in neuem Fenster geöffnet) Klick, um über Twitter zu teilen (Wird in neuem Fenster geöffnet) Klick, um auf ...
Read more

Day 16 - Easy Breezy Beautiful Yoga - 30 Days of Yoga ...

Join Adriene on Day 16 of The 30 Days of Yoga journey! Easy Breezy Beautiful Yoga. Experiment going at your practice with and easy breezy ...
Read more

E-Day der WKÖ - WKO.at - Startseite

E-Day 2016 - Österreichs größter E-Business-Event am 3. März 2016 in der Wirtschaftskammer Österreich und per Livestream im Netz
Read more

Day16 - jcerny - sites.google.com

Computer Programming, Web Technologies, SAT Prep, and AP Computer Science
Read more

Learning My Purpose - Day 16

What on earth am I here for? What is the purpose of my life? Discover your purpose with free online teaching from Pastor Rick Warren.
Read more

Day16 : Mostafa Hamouda : Free Download & Streaming ...

Search the history of over 491 billion pages on the Internet. search Search the Wayback Machine
Read more

corpuls®: day16 - compressions-for-lives.com

Download Wallpaper Download. Back
Read more

day16-fryingpanbaytogohomebay - gunkholing - Google Sites

Seven pm, anchored in Go Home Bay. Phil is busy doing the dishes; the sky is cloudy - again; the weather report is not fit for human ears; and here we sit ...
Read more

day16 - YouTube

PBB Lucky 7 Day 16 July 30 2016 - 1st J ni Ellise Jerome Papasok sa bahay ni kuya - Duration: 6:19. Andrea Brillantes Official 251,396 views
Read more

Earn money online with the world's largest affiliate ...

Since 1998, we’ve shown over 15 MILLION men and women, in more than 190 countries worldwide, how to create a 24/7 income stream.
Read more