A Smalltalk Patterns Safari Brian Foote The Refactory
A Smalltalk Patterns Safari Brian Foote The Refactory, Inc. 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 1
Why Patterns? What’s New Here is that Nothing is New Here • Patterns are about what works • Patterns give us a way to talk about what works • Patterns distill experience • Patterns give us a pithy design vocabulary 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 2
Alexander on Patterns in solutions come from patterns in problems. "A pattern is a solution to a problem in a context. " "Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice. " Christopher Alexander -- A Pattern Language 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 3
Why Patterns in the Wild? • • • Learn where to look Learn how to spot ‘em and recognize ‘em See how they evolve Appreciate their history, and our history Understand their ecosystem and interdependence • Helps us discern and discover new ones 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 4
The Gang of Four: The Essential Field Guide Design Patterns: Elements of Reusable Object-Oriented Software Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Addison Wesley, 1995 A landmark book that changed the way programmers think about building object-oriented programs 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 5
Dr. Johnson, I presume? Great explorers, like Livingstone and Johnson neither necessarily create nor invent, nor, do they even always “discover” Often, their greatest skill is communication. . . 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 6
Your Guide • Over Twenty Years in the Bush. . . • Stalking Smalltalk since ‘ 81… or ‘ 85… Patterns? • An long-time associate of Dr. Johnson. . . 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 7
Field Guides (cont. ) The Design Patterns Smalltalk Companion Alpert, Brown, and Woolf Addison Wesley, 1998 Pattern-Oriented Software Architecture Buschmann, Meunier, Rohnert, Sommerlad, and Stahl Wiley, 1996 The Pattern Languages of Program Design Series Volumes 1 -4 Addison Wesley, 1995 -2000 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 8
Visual. Works* The Image: A Virtual Veldt The Cradle of Object-Oriented Architecture An Olduvai Gorge Teeming with Patterns 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 9
Creational patterns Go. F Design Patterns • Abstract Factory • Builder • Factory Method • Prototype • Singleton Structural patterns • Adapter • Bridge • Composite • Decorator • Facade • Flyweight • Proxy 11/27/2020 (C) Brian Foote 1999 Behavioral Patterns • Chain of Responsibility • Command • Interpreter • Iterator • Mediator • Memento • Observer • State • Strategy • Template Method • Visitor Smalltalk Patterns Safari 10
The Big Five • • • Lion Leopard Elephant Buffalo Rhino 11/27/2020 (C) Brian Foote 1999 • • • Composite Proxy Strategy Observer Visitor Smalltalk Patterns Safari 11
Objects within Objects 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 12
Composite Context: • Developing OO software Problem: • Complex part-whole hierarchy has lots of similar classes. – Example: document, chapter, section, paragraph. Forces • • simplicity -- treat composition of parts like a part • • power -- create new kind of part by composing existing ones • • safety -- no special cases, treat everything the same 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 13
Composite Idea: make abstract "component" class. Alternative 1: every component has a (possibly empty) set of components. Component Children Chapter . . . Paragraph Problem: many components have no components 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 14
Composite Component Container Children Leaf Composite and Component have the exact same interface. • interface for enumerating children • Component implements Children() by returning empty set • interface for adding/removing children? 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 15
Composite Pattern Component Container Children Leaf Composite and Component have the exact same interface. • interface for enumerating children • Component implements Children() by returning empty set • interface for adding/removing children? 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 16
Two Design Alternatives Component does not know what it is a part of. Component can be in many composite. Component can be accessed only through composite. Component knows what it is a part of. Component can be in only one composite. Component can be accessed directly. 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 17
Component Knows its Composite Rules when component knows its single composite. • A is a part of B if and only if B is the composite of A. Duplicating information is dangerous! Problem: how to ensure that pointers from components to composite and composite to components are consistent. 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 18
Ensuring Consistency The public operations on components and composites are: • • Composite can enumerate components. • • Component knows its container. • • Add/remove a component to/from the composite. The operation to add a component to a composite updates the container of the component. There should be no other way to change the container of a component. Smalltalk does not enforce private methods. 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 19
Example: Equipment weight cost Floppy. Disk . . . Composite. Eq. weight | total : = 0. self children. Do: [: each | total : = total + each weight]. ^total 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 20
Visual. Component>>add: 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 21
Composite Pattern in Visual. Works Visual. Component Children Composed. Text List. View Visual. Part Container Composite. Part Most operations in Composite. Part simply iterate over the children and repeat the operation on them. 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 22
Composite. Part add. Component: a. Visual. Component self is. Open if. True: [. . . ] if. False: [components add. Last: a. Visual. Component. – a. Visual. Component container: self] display. On: a. Graphics. Context "Display each of the receiver's components. " | clip. Box : = a. Graphics. Context clipping. Bounds. components do: [: component | (component intersects: clip. Box) copy]] if. True: [component display. On: a. Graphics. Context 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 23
Summary of Composite is a kind of Component Permits arbitrary hierarchies Add/remove Component from Composite Operations on Composite iterate over Components 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 24
Template Method As plentiful as sawgrass. . . Often associated with scavengers e. g. print. On: a. String 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 25
Factory Method Plentiful in the image. . . • View default. Controller. Class • UILook. Policy • Xxx. Class methods 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 26
Abstract Factory 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 27
Builder Class. Builder UI Builders 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 28
Prototype This isn’t Self, after all. . . • Visual. Works Text. Attributes • Look for copy • Thinglab, Morphic. . . 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 29
Singleton • • All the Metaclasses. . . Process. Scheduler Source. File. Manager true, false, nil 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 30
Strategy • Sundry Policies. . . 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 31
Observer Pattern Intent: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Subject observer/dependent Observer Update Add. Dependent Remove. Dependent Changed Rectangle. Figure 11/27/2020 (C) Brian Foote 1999 end. Point Line. Figure Update Smalltalk Patterns Safari 32
Iterator Provide a way to access the elements of an aggregate object sequentially without exposing its underlying implementation. Iterator Aggregate Create. Iterator() Concrete. Aggregate Create. Iterator() 11/27/2020 (C) Brian Foote 1999 First() Next() Is. Done() Current. Item() Concrete. Iterator Smalltalk Patterns Safari 33
Iterator in Smalltalk 1) Internal iterator - iterate inside the Iterator • easy to use, not as powerful as external iterator • works well because of blocks employees do: [: each | each name print. On: a. Stream] 2) Combine Next() and Current. Item() (Smalltalk Stream) employee. Stream : = General. Motors employee. Stream. [employee. Stream at. End] while. False: [employee. Stream next name print. On: a. Stream] 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 34
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 it 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 35
Proxy Subject Client request. . . Real. Subject request real. Subject Proxy request real. Subject request 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 36
Proxy Remote proxy - first package arguments, then make remote procedure call. Virtual proxy - compute objects, then forward request. Protection proxy - check access rights, then forward request. 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 37
Creating an Orphan nil subclass: #Future instance. Variable. Names: ‘semaphore ' class. Variable. Names: ‘ ' pool. Dictionaries: ' ' category: ‘Reflection-Examples’ In Visual. Works, Class. Builder does the rest. Default implementations of does. Not. Understand: and class are provided. 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 38
Locating Orphans all. Behaviors. Do: a. Block "Evaluate the argument, a. Block, for each kind of Behavior in the system (that is, Object and its subclasses, plus other classes that have no superclass). " Class roots. Of. The. World do: [: cls | a. Block value: cls all. Subclasses. Do: a. Block] 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 39
Terminology and Taxonomy Is this a Wildebeest? A Gnu? Class: Mammalia Order: Artiodactyla Family: Bovidae Genus species: Connochaetes (flowing beard) taurinus (like a bull) albojubatus (white mane) 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 40
Decorator Object () Visual. Component () Visual. Part () Wrapper () 11/27/2020 (C) Brian Foote 1999 Geometric. Wrapper () Filling. Wrapper () Stroking. Wrapper () Graphics. Attributes. Wrapper () Passivity. Wrapper ('control. Active') Reversing. Wrapper () Strike. Out. Wrapper () Scaling. Wrapper () Translating. Wrapper () Layout. Wrapper () Bounded. Wrapper () Bordered. Wrapper () Menu. Bar. Wrapper () Bounding. Wrapper () Scroll. Wrapper () Data. Set. Scroll. Wrapper () Slave. Scroll. Wrapper () Widget. State. Wrapper () Widget. Wrapper () Spec. Wrapper () Smalltalk Patterns Safari 41
Adaptor • Aspect Adaptor • Pluggable Adaptor 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 42
Flyweight • Character 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 43
Chain of Responsibility • Visual. Part 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 44
Command • Menu. Item Blocks are often used instead in Smalltalk, so this pattern is not as common in its pure form in this environment. . . 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 45
Interpreter • The Window. Spec Walker Though it’s not clear how to classify this… The simulator? 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 46
Observer 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 47
Parse Tree 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 48
Visitor • Program. Node. Enumerator This class provides a framework for recursively processing a program node tree. Program nodes of type <Node. Type> should implement node. Do: an. Enumerator ^an. Enumerator do<Node. Type>: self. . . arguments. . . by analogy with the Program. Node. Builder messages new<Node. Type>[arguments] The old node is the first argument, so that enumerators can preserve the comment and source information if they wish. To rebuild each non-leaf node, the enumerator sends the message self do. Node: <argument> which is implemented by default as do. Node: a. Node ^a. Node node. Do: self 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 49
Visitor • Program. Node. Enumerator Subclasses must implement the following messages: enumerating-non-leaves do. Assignment: variable: value: do. Block: arguments: body: do. Cascade: receiver: messages: do. Message: receiver: selector: arguments: do. Method: selector: primitive: block: do. Parameter: variable: type: do. Return: value: do. Sequence: temporaries: statements: enumerating-leaves do. Literal: value: do. Variable: name: 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 50
Program. Node. Enumerator is a visitor for the Smalltalk-80 compiler. Smalltalk compiler parse tree hierachy rarely changes. There are lots of potential programs that need to traverse trees: • • consistency checkers • • program transformation • • metrics It makes sense to keep these applications out of the parse tree. Easy to make minor variations on tree walking applications. 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 51
Facade Rarely seen in the image. . . Perhaps the Smalltalk Compiler itself qualifies… 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 52
Bridge Elusive Quarry Yelland’s paper from OOPSLA ‘ 96 discusses how Bridges might improve the image. . . 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 53
State Have you seen one? 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 54
Memento Nocturnal? Seen in applications, like the Refactoring Browser Difficult to find in the image. . . 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 55
Mediator • Case can be made for models. . . 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 56
Creational patterns Go. F Design Patterns • Abstract Factory • Builder • Factory Method • Prototype • Singleton Structural patterns • Adapter • Bridge • Composite • Decorator • Facade • Flyweight • Proxy 11/27/2020 (C) Brian Foote 1999 Behavioral Patterns • Chain of Responsibility • Command • Interpreter • Iterator • Mediator • Memento • Observer • State • Strategy • Template Method • Visitor Smalltalk Patterns Safari 57
Bird on Patterns Learn the patterns and then forget ‘em -- Charlie Parker http: //www. hillside. net 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 58
Conserving Infodiversity The Image: A Cyberworld Heritage Site 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 59
A Victory over Entropy • A stunning collective achievement • A temple to design • Incubator for architecture • A pattern hunter’s paradise 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 60
Contact Information Brian Foote The Refactory, Inc. 209 W. Iowa Urbana, IL 61801 (217) 328 -3523 http: //www. laputan. org (H) http: //www. refactory. com (O) foote@refactory. com 11/27/2020 (C) Brian Foote 1999 Smalltalk Patterns Safari 61
- Slides: 61