Class Note for CMPSCI 220 at UMass(2)
Class Note for CMPSCI 220 at UMass(2)
Popular in Course
Popular in Department
This 5 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at University of Massachusetts taught by a professor in Fall. Since its upload, it has received 16 views.
Reviews for Class Note for CMPSCI 220 at UMass(2)
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 02/06/15
Highly Skilled Programming CMPSCI 220 291A I Pro cient with Programming Methodology Tools Spring 2009 Methods I The two are often interrelated 8 Design Observers and Factories I Engquot deSIgn methOdS and Programmmg anguage Patterns and Pnnuples l Speci cally 00 language features and design UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science I UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Scienee Four Design Principles The Strategy Design Pattern I Identify aspects ofyour application that vary I De nes a family of algorithms I Separate them from what says the same I Encapsulates each one I That is encapsulate the parts that vary I Program to an interface not an implementation Makes them Interchangeable l Interfaces will specify behaviors I Lets the algorithm vary independently from I Classes will implement speci c versions the clients that use it I Favor composition over inheritance I HASeA can be better than lSeA I Classes should be open for extension but closed for modi cation UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science The Decorator Design Pattern Another Design Pattern I Attaches additional responsibilities to an I Motivating example Obied dynmnicauy I A awed design I Provides a exible alternative to subclassing I A design Principle for extending functionality I Another pattern emerges UWERSITY 0E MASSAcHusrirs AMHERST Department of Computer Science I 7 UMVERSITY 0E MASSAcHusrrTs AMHERST Department of Computer Science The Weather Monitoring Application I Given a WeatherData class that provides getter methods for measurement values I Temperature humidity barometric pressure I Our task Three displays using weather data I Current conditions statistics forecast I But must be extensible W new displays possible WeatherData s measurementsChangedo method called whenever new data available UNIVERSITY OF MASSACHUSETTS AMHERST Department orquot Computer Science The Weather Monitoring Application A Simple Solution method called whenever weather measurements have been updated public void measurementsChanged oat temp getTemperature 0 oat humidity getHumidity 0 oat pressure getPressure currentConditionsDispIayupdate temp humidity pressure statisticsDispIayupdate temp humidity pressure forecastDispIayupdate temp humidity pressure The Weather Monitoring Application WeatherData getTemperature getHumidity getPressure measurementsChanged method called whenever weather other methods measurements have been updated public void measurementsChanged what should this code look like What Do You Think I Problemsgt I Design Principles Violatedigt UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science What Do You Think I Problemsgt I Adding new display elements means recoding I No way to add display elements at run time I Design Principles Violated I Encapsulate what varies I Program to an interface not an implementation I Classes should be open for extension but closed for modification UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science The Observer Design Pattern I De nes a one to many dependency between objects I When one object changes state I All dependents are notified and updated automatically I Based on the publish subscribe idiom I Publisher 2 Subject I Subscriber Observer Umvsnsmfo MASSACHUSHTS AMHERST Department of Computer Science The Observer Pattern Another Design Principle interface Interface I Strive for loosely coupled designs between M observer5 Observer regererobserved r Mateo obects that interact remo ve Observe0 no ObSSrVSISO I Sub1ect only knows observer implements a 4 certain interface I Can add observers at any time ConcreteSub 39ect registerObserver I removeObserver 4 when ConcreteObserver update I Never need to modify subject to add new types of observers noanObserverSO other Observer speci c methods C b I d d 1 getstateo I an reuse su 1ects or 0 servers in epen enty sersmeo Implements quot7 I Changes to subjects don t affect observers and UsesInvokes gt Vlce ve 52 39 7 7 Umvsnsm or MASSACHUSFI39rsi AMHERST Department creompmerScaense UNIVERsmr or MASSACHLlSETES AMHERST Department of Computer Science Applying the Observer Pattern to Weather Monitoring Java s BuiltIn Observer Pattern inferface ltltInterfacegtgt ltltInterfacegtgt Subjects inherit from javautilObservable M 039W Observer DisplayElement regSterobserve 39 update display I To send notl catlon remo ve Observe0 p 71 A notifyCEaserveIsO 1 I F1rst Call setChangedO Weather bata Then call notifyObservers or registerObserver mmnmond onsoisnlax x notifyObserversObJect arg Ob Lillggbse ggg fume display current I To rece1ve notl cations observer calls measurements M updateObservable 0 Object arg getTemperatureO getHumiditYO StatisticsDisplay I The arg allows for data push as well as pull getPressureO update measuremenlsChangedO displayo quotdisplay statistics t t e l e e e e Dunmam orMASSAcnusnTs AMHERST Department or Computer science Applying Java s BuiltIn Observer The Weather Application Code Pattern to Weather Monitoring UsIng the Observer Pattern m ltltInterfacegtgt ltltInterfacegtgt I On the course web site addObseweK Observers Observer DisplayElement deleteObserverO V updareo dIspayo I httD wwweedlabcsumassedu c5220 WeatherCode notifyObserversO p Xx setChangedO quot I Also the Java built in observer version I httD wwweedlabcsumassedu c5220 WeatherObsCode m CurrentConditionsDis la K sub39ect Pdateo getTemperatureO J dlsplavO dSPaY Current getHumidityo measuremenls getPressureO StatisticsDisplax update display dispay statistics UNIVERSITY or MASSACHUSETTS AMHERST Department of Computer Science UNIVERSITY or MAssAcHusnTs AMHERsr Department of Computer Science Two More Design Patterns Simple Factory Pattern I Motivating example PIuaStore SImplePIuaFactory PIua gt ordeerzaO createPIzzaO prepareO I A Pseudo pattern bake I A true pattern is I A desi rinci 1e gm P P Not really a Pattern I Another pattern emerges oSimiIar to the static factory method approach UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science The Pizza Code Using the Factory Pattern The Factory Method Design Pattern I On the course web site I De nes an interface for creating an object 39 hm WWW edlabCSmmssEdu C5220 PiZZaCOde I But lets subclasses decide which Classes to instantiate UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science The Pizza Code The Factory Method Pattern Using the Factory Method Pattern ML Creator I On the course web site factonMethodO anopemtiono I httD Wwweedlabcsumassedu C5220 PizzaFaCtCode ConcreteProduct A ConcreteCreator lt factoryMethodO UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science Another Design Principle I Depend upon abstraction I Do not depend upon concrete classes I The Dependency Inversion Principle The Abstract Factory Method Design Pattern I Provides an interface for creating families of related or dependent objects I Without specifying their concrete classes UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science Abstract Factory Method Pattern lt ltinterfacegt gt AbstractFactoq crea reProducrAO crea re ProduchO xquot a ltltinterfacegtgt AbstractProductA create ProductAO create ProductAO createProductBO createProductBO ConcreteFacto l ConcreteFacto Z Va A ltltinterfacegtgt AbstractProductB ProductBZ UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science The Pizza Code Using the Abstract Factory Method Pattern I On the course web site I httD wwweedlabcsumassedu c5220 PizzaAbstFactCode UNIVERSITY OF MASSACHUSETTS AMHERST Department of Computer Science
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'