Unformatted text preview:

Design Patterns in SmalltalkWe talked about ...IteratorComparing two collectionsSlide 5ProxySlide 7Implementing Proxy with doesNotUnderstand:BridgeFlyweightFlyweights for TextSlide 12Flyweight for CADSlide 14Flyweight as a compound patternChain of ResponsibilitySlide 17Slide 18MementoSlide 20Slide 21Slide 22Patterns Protect from ChangePatterns lead to standard interfacesSmalltalk features impact patternsPatterns and refactoringCommandRefactoring to CommandCompositeRefactoring to a CompositeFaçadeDesign patterns and refactoringDesign Pattern summaryDesign Patterns in SmalltalkInfluenced by programming languageLarge-scale refactoringsWe talked about ...Template MethodState, Strategy, Observer, Mediator, FacadeCommand, Interpreter, VisitorAdapter, Composite, DecoratorFactory Method, Abstract Factory, Builder, Prototype, SingletonIteratorInternal iterator vs external iteratorInternal iterators depend on blocksInternal iterators simplerExternal iterators more powerfulComparing two collections= anObject| first second |self size = anObject size ifFalse: [^false].first := ReadStream on: self.second := ReadStream on: anObject.[first atEnd | second atEnd] whileFalse:[first next = second next ifFalse: [^false]].^first atEnd & second atEndIteratorProvide a way to access the elements of an aggregate object sequentially without exposing its underlying implementation.ConcreteIteratorIteratorFirst()Next()IsDone()CurrentItem()ConcreteAggregateCreateIterator()AggregateCreateIterator()ProxyProvide a surrogate or placeholder for another object to control access to itrepresent an object in a remote address spacecreate expensive objects on demandcheck access rights Proxy has same interface as “real subject”, and forwards operations to itProxySubjectRequestClientProxyRequestrealSubjectRealSubjectRequestImplementing Proxy with doesNotUnderstand:When an object does not understand a message, the message gets converted into doesNotUnderstand: message with the original message as an argumentDefault version of doesNotUnderstand: pops up a debuggerProxy can override doesNotUnderstand:BridgeDecouple an abstraction from its implementation so that the two can vary independently.ImplementorConcreteImp2AbstractionConcreteImp1FlyweightUse sharing to support large numbers of objects efficiently.Separate intrinsic state (state stored in flyweight) from extrinsic state (state passed in as part of context). Minimize extrinsic state. Share flyweights that have the same intrinsic state.Usually requires a factory that detects whether a flyweight exists with a particular intrinsic state and returns it.Flyweights for TextcolumnrowrowrowT h i s i s a s e n t e n c eT h i s a s e n t cFlyweight poolClient of flyweight,which holds context.Context is location.FlyweightFlyweightFactoryGetFlyWeight(key)Flyweight intrinsicOperation(extrinsic)flyweightpoolFlyweight class is usually abstract, with concrete subclasses that define the intrinsic state.Flyweight for CADA VLSI design system must model millions of transistors.This is only possible by sharing structure. Most transistors are part of larger structures (registers, NAND gates, RAM) that designers prefer to think about. Each kind of structure is called a cell.Each cell is interconnected with other cells.Context is location and interconnections.Flyweight for CADTransistorsizeDisplay(loc,connects)CellDisplay(loc,connects)flyweightpoolCompositeCellports, locationsDisplay(loc,connects)CellFactoryGetCell(key)Register(key,Cell)Flyweight as a compound pattern1) Recycling Factory (generalization of Singleton)2) Value Object - immutable objects3) Normalized Object - Separating intrinsic from extrinsicChain of ResponsibilityAvoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.Examples:“inheriting” color from carevent handlers in GUIChain of ResponsibilityHandlerHandleRequest()ConcreteHandler2HandleRequest()ConcreteHandler1HandleRequest()ClientChain of ResponsibilityUsually mixed with other patterns• Composite often has Chain of Responsibility up the tree.• Sometimes request is encoded as a Command• Sometimes request sent to StrategyMementoWithout violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.MementoGetStateSetStatestateOriginatorSet(Memento)CreateMementostateOriginatorreturn new Memento(state)state = m->GetState()MementoUndo is not enough in the presence of a constraint system.Must go back to same state, not just reverse operation.MementoA C A B C C B A A CX Z X Y Y X X X Z Translator isa state machineinput documentChange part of inputand recompute output.output documentA BProblem: Large structured documents are stored in one form and translated to another form to be printed. How can a small change in the input be converted into a change in the output without retranslating the entire input?MementoFor each input symbol, keep a memento of the translator and the output for that input symbol. Recompute the output until the translator is back in the same state.I S T O P Q R S S S TX Z X Y Y X X X Z next stateA C A B C C B A A CA Boutputinitial stateinputPatterns Protect from ChangeRule: if something is going to change, make it an object.Strategy: make algorithm an object so it can changeState: make state-dependent behavior an object so it can changeIterator: make the way you iterate over an aggregate an object so it can changeFacade: make a subsystem an object so it can changeMediator: make the way objects interact an object so it can changeFactory: make the classes of your products an object so it can changePatterns lead to standard interfacesDecorator, Composite, ProxyIntroduce new objects with same interfaces as old but that add a feature / represent a group / represent a remote objectAdapterFacadeSmalltalk features impact patternsIterator – blocksProxy – doesNotUnderstand:Command – blocks, perform:Prototype – classes as objectsBridge – dynamic typing?Patterns and refactoringCan think of any design pattern as a “big refactoring”Introduce design patterns when you know you need them, not when you think you might need themRefactoring to Patterns by Joshua


View Full Document

UIUC CS 598 - Influenced by programming language

Download Influenced by programming language
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 Influenced by programming language 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 Influenced by programming language 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?