Christopher Alexander Design Patterns Jens Gustavsson TAPESTRY OF LIGHT AND DARK Create alternating areas of light and dark throughout the building, in such a way that people naturally walk toward the light, whenever they are going to important places: seats, entrances, stairs passages, places of special beauty, and make other areas darker, to increase the contrast. 1 Software Design Patterns What is good software design? A Design Pattern is a standard solution for a standard design problem in a certain context. Goal: resuse design information z z Functionality Runtime qualities z z z z z z Other qualities z z z Example: Facade Performance Safety Security Reliability Usability Portability Maintainability Testability Example: Facade Facade 2 How to describe design patterns? Facade Intent Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. Motivation Structuring a system into subsystems helps reduce complexity. A common design goal is to minimize the communication and dependencies between subsystems. … example … Facade Facade Applicability Consequences Use the Facade pattern when: z you want to provide a simple interface to a complex subsystem. This makes subsystems more reusable and easier to customize. z there are many dependencies between clients and the implementation classes of an abstraction. Introduce a facade to decouple the subsystem from other subsystems, thereby promoting subsystem independence and portability. z you want to layer your subsystems. Use a facade to define an entry point to each subsystem level. The Facade pattern offers the following benefits: 1. It shields clients from subsystem components, thereby reducing the number of objects that clients deal with and making subsystem easier to use. 2. It promotes weak coupling between subsystem and its clients. Weak coupling lets you vary the components of the subsystem without affecting its clients. 3. It doesn't prevent applications from using subsystem classes if they need to. 3 Facade z z z z z z z Structure Participants Collaborations Implementation Sample Code Known Uses Related Patterns Patterns do not contain new ideas! Composite, motivation Composite, motivation aPicture Text aRectangle aLine aText aCircle 4 Composite, motivation Composite, motivation aPicture Text aRectangle aLine aText aCircle Composite, applicability z z Represent hierarchies of objects Ignore difference between compositions of objects and individual objects aCircle aLine aLine aPicture aLine aLine Composite, structure Component Client * operation() add(Component) remove(Component) getChild(int) Leaf operation() Composite operation() add(Component) remove(Component) getChild(int) 1 5 Composite, our example Composite, consequences VisualComponent Client move() setColor() move() setColor() Rectangle move() setColor() z z Makes the client simple Easy to add new kinds of components Can make design too general 1 Line Circle z move() setColor() * add(VisualComponent) remove(VisualComponent) getChild(int) Picture move() setColor() add(VisualComponent) remove(VisualComponent) getChild(int) State, motivation DrawingController Client Text tool mouseClicked() { if (tool == zoom) { ... } else if (tool == select) { ... } } keyPressed() { if (tool == zoom) { ... } else ... 6 State, example DrawingController Client mouseClicked() keyPressed() CreationTool mouseClicked() keyPressed() SelectionTool mouseClicked() keyPressed() State, structure Tool mouseClicked() keyPressed() ZoomTool mouseClicked() keyPressed() Some thoughts z z z z Context Many patterns are simple Common vocabulary A language facilitates thought If design patterns are the building elements for systems, shouldn't they be the primitives in programming languages? request() ConcreteStateA handle() State handle() ConcreteStateB handle() ConcreteStateC handle() Anti Patterns z Poltergeist z z z Copy and Paste Programming z z Shortlived, stateless, forwards responsibility Typical names: manager, controller, startProcess Several copies of almost identical code Boat Anchor z Something without value for a project that is used anyway 7