DOC PREVIEW
UIUC CS 598 - Design Patterns

This preview shows page 1-2-3-19-20-38-39-40 out of 40 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 40 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Design PatternsObserver PatternSlide 3Observer Pattern:NotificationProblemBasic classesCollaborationsDynamic ModelA ScriptDoorSlide 12DogSlide 14Slide 15PersonSlide 17WatcherPowerPoint PresentationImprovementsModel and Memory ManagementMake Dog a subclass of ModelSlide 23Advantage of Observer PatternDisadvantage of Observer PatternAdding New ObserverBirdSummaryCreational PatternsAbstract FactoryMaking Abstract FactoryRefactoring creationSlide 33Dependency InjectionThe scriptSlide 36WikiWorks configuration fileDependency injectionDesign patterns and refactoringNext timeDesign PatternsObserver PatternDependence 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 PatternObserverObserverupdate:update:SubjectSubjectaddDependent: addDependent: removeDependent: removeDependent: changed:changed:ValueHolderValueHoldervalue:value:valuevalueTextViewTextViewupdate:update:observer/observer/dependentdependentmodelmodelObserver Pattern: Registrationsubject addDependent: observerNotificationself changed. self changed: #valueUpdatedefine update: aSymbolNotificationObject notifies dependents when information changes by sending itself a #changed message.changed: anArgself dependents do: [:each | each update: anArg]ProblemA 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 classesDogbark / movePersonDooropen / close / isOpenObjectaddDependent:changed:update:CollaborationsPersonDooropencloseDogbarklistenwatchDynamic ModelRecord order of events, interaction between objects.Record order of events, interaction between objects.DogDogPersonPersonDoorDoorSequence diagramSequence diagrambarkbarkopenopennotifynotifygo thru doorgo thru doornotifynotifyclosecloseregisterregisterregisterregisterunregisterunregisternotifynotifyA Script| person door dog |door := Door new close.dog := Dog new.dog door: door.person := Person new.person door: door; dog: dog.dog bark.Dooropened <Boolean>openopened := true.self changed: #openDoorcloseopened := false.self changed: #closeisOpen^openedDogcurrentState :: #sleeping, #waiting, #outsidebarkcurrentState := #waiting.self changed: #barkdoor: aDoordoor := aDoor.door addDependent: selfDoggoOutcurrentState := #outside.door removeDependent: self. self changed: #moveDogupdate: aSymbol(currentState == #waiting) & (aSymbol == #open)ifTrue: [self goOut ]Persondog: aDogdog := aDog.dog addDependent: selfPersonupdate: aSymbolaSymbol == #bark ifTrue: [door open].aSymbol == #move ifTrue: [door close]Watcherinstance variable: name <String>update: aSymbolTranscript show: subjectName; show: ' '; show: aSymbol; crname: aStringname := aString| 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.ImprovementsCreating method (have class method return initialized object)Compose method(Watcher on: door name: 'door')(door watcherNamed: 'door')Model and Memory ManagementDog allInstancesreports 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 ModelModel 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.If you are not using Model then after the script saysdog bark.add the messagesdog release. door release. person releaseAdvantage of Observer PatternEasy to add new observers.Coupling between observer and subject is abstract.Disadvantage of Observer PatternOften hard to understand relationships between objects in system.Sometimes inefficient.Adding New ObserverSuppose room also has a bird, which is usually in a cage.If bird is not in cage and door opens, bird flies out.Birdupdate: aSymbol(aSymbol == #open) & (self isCaged not) ifTrue: [self flyOut]Script must now create a bird and make it depend on the door.SummaryObserver is an important patternUsed in a UI to make reusable componentsReduces coupling by increasing abstractionAbstraction makes programs easier to change, harder to understandOften requires a “script” to set up dependenciesCreational PatternsFactory MethodFactory ObjectAbstract FactoryBuilderPrototypeSingletonAbstract FactorySometimes 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.ScrollBarMotifScrollBar PMScrollBarWidgitFactoryCreateScrollBarCreateWindowMotifWidgetFactoryCreateScrollBarCreateWindowPMWidgetFactoryCreateScrollBarCreateWindowMaking Abstract FactoryGive Producer a component called “Factory”Create class FactoryAdd instance variable “factory” to ProducerChange constructor to have line factory:=Factory newMove factory methods to FactoryCopy factory method to FactoryChange sends of createFoo to “factory createFoo”Refactoring creation1) Encapsulate in a method – makeProduct(Factory method)2) Groups of related products => move factory methods to a new object(Abstract Factory)3) Method too big => method object / Builder4) User customizable => PrototypePrototype 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 InjectionEliminate 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 scriptSmalltalkers often use codeSmalltalk literalXMLNon-standard formatwindowSpec"UIPainter new openOnClass: self andSelector: #windowSpec"<resource: #canvas>^#(#FullSpec #window: #(#WindowSpec #label: 'Payroll' #bounds: #(#Rectangle 1245 452 1544 761 ) ) ...WikiWorks configuration


View Full Document

UIUC CS 598 - Design Patterns

Download Design Patterns
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Design Patterns and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Design Patterns 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?