Unformatted text preview:

Dependence ManagementDependenceSlide 3Cycles of dependenceEliminating dependenceObserver PatternSlide 423Abstract ServerAdapterSlide 10Slide 11MediatorNot MediatorSlide 14Slide 15Application ModelFaçadeThe CompilerCreational PatternsFactory MethodSlide 21Slide 22Factory ObjectApplicabilitySlide 441PrototypeMaking PrototypeAbstract FactoryMaking Abstract FactoryBuilderImplementing BuilderMaking BuilderSummary of Factory PatternsSingletonSlide 35Managing dependencesSlide 37Slide 38Slide 39Dependence managementDesign patterns and refactoringDependence ManagementBob Martin says “Object-oriented programming is dependence management.”See his “Design Principles and Design Patterns” athttp://www.objectmentor.com/resources/articles/Principles_and_Patterns.PDFDependenceHow can one package depend on another?Reuse a class•By inheritance•By instantiation (direct reference)Reuse a variableReuse a method?DependenceIf package A depends on package B then you can’t run the tests for A unless you also have B.“A depends on B” means you can’t use A unless you have B.“Package A depends on package B” means that something in A depends on something in B.Cycles of dependenceIf package A depends on package B then package B should NOT depend on package A.If classes C and D both depend on each other, put them in the same package.Eliminating dependenceThe Observer pattern eliminates dependence.Suppose that “FBIAgent” is an observer of Mobster. Mobster is a subclass of Model, so it can have observers. Class FBIAgent probably depends on class Mobster, but Mobster does not depend on FBIAgent.Observer PatternObserverObserverupdate:update:SubjectSubjectaddDependent: addDependent: removeDependent: removeDependent: changed:changed:MobsterMobsterrobBankrobBankdriveCardriveCarFBIAgentFBIAgentupdate:update:observer/observer/dependentdependentmodelmodel*Smalltalk base libraryGUI library ApplicationApplication GUI SUnit testsAbstract ServerBob Martin’s name for “depend on an interface, not on a concrete class”.InterfaceClientServerCreation scriptConsumer<<interface>>ResourceManagerResrcMgr1AdapterIntent: Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.AdapteeAdapterTargetAdapterTargetRequest()ClientAdapterRequest()AdapteeAdapterAllows client and adaptee to be unchangedAdapter is usually uglyClient AdapteeAdapterMediatorDefine an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.Example: Insurance policies must be approved before they are issued. There is a procedure (which can change over time, and which is different for different kinds of policies) for approving a policy. This procedure must interact with work queues of managers and with the history that is kept on the customer. Instead of putting this procedure in the insurance policy, put it in a separate object that is easy to change. (This is a “business process”)Not MediatorCustomerHistoryInsurancePolicyWorkerMediatorProcedureCustomerHistoryInsurancePolicyWorkerMediatorColleaguesIf interaction is main thing that changes, then make the interaction be an object.Colleague classes become more reusable.Mediator is the non-reusable part.MediatorProcedureCustomerHistory InsurancePolicy WorkerBusiness RuleDomain ObjectsMediatorColleaguesApplication ModelMediator between widgets and domain model.PayrollSystemInterfaceListViewTextView EmployeePayrollSystemFaçadeProvide a unified interface to a set of interfaces in a subsystem. Define a higher-level interface that makes the subsystem easier to use.FacadeThe CompilerScannerReturn ExpressionSmalltalkExpressionCompilercompileevaluateCompiledMethodAssignmentExpressionMessage Expression...ParserCreational PatternsFactory MethodFactory ObjectAbstract FactoryBuilderPrototypeSingletonFactory MethodDon't (call constructor / send message to class) directly.Make a separate function / method to create object.How to do it:Find every class name in “producer”Extract it (or perhaps “CN new”) into a methodFactory MethodResults:Localizes dependenciesModules become less coupledFactory MethodProducerdoSomethingConcreteProducercreateXXcreateXX ^XX newdoSomething …self createXX...XXFactory ObjectProblem with factory method -- have to create subclass to parameterize.Often end up with parallel class hierarchies.Example: subclass of Tool for each figure you want to createAlternative: parameterize CreationTool with object that creates figureSmalltalk automatically creates a factory for every class, the Class!(Note: Factory Object is generalization of Abstract Factory,Builder, and Prototype. It is not in the book.)ApplicabilityUse factory objects:when system creates them automaticallywhen more than one class needs to have product specifiedwhen most subclasses only specialize to override factory methodFigureFactory newLineFigureFactoryElipseFigureFactoryRectangleFigureFactoryFigureLineFigureElipseFigureRectangleFigurePrototypeMaking a class hierarchy of factories seems wasteful.The parameters of an object can be as important as its class.Solution:Use any object as a factory by copying it to make a new instance.AdvantagesDon't need new factory hierarchy.Can make new "class" by parameterizing an objectDisadvantagesRequires robust copyingMaking PrototypeYou have a design in which objects are parameterized by passing in classes. You are making new classes just for their constructors, or you want to make “composite classes”.Make sure “copy” works. Define “new” as an instance method that returns a copy. Change client to pass in instances instead of classes.Abstract 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


View Full Document

UIUC CS 598 - Dependence Management

Download Dependence Management
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 Dependence Management 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 Dependence Management 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?