Download presentation
1
Design Patterns CS is not simply about programming
it is also about good design some designs are better than others more efficient in space or time required (traditional criteria) more robust more extensible more reusable more understandable these are central concerns of Software Engineering there are trade-offs everywhere architect balances aesthetics, functionality, cost mechanical engineer balances manufacturability, strength, maintainability, cost software engineer balances all of there and more which trade-off is best? no perfect solution, no exact rules
2
Design it takes experience
seeing examples (good and bad) using doing it's like writing: you always find a “better” way rarely find final design on first try no one ever stops learning you have to keep practicing: like in music, sports why not a catalog of good designs? we could read and learn from two “bibles” of Design Patterns: The Timeless Way of Building by Christopher Alexander (1979) design patterns in architecture initiated the study of design patterns in software Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994)
3
Reuse, Do Not Build Again
libraries are reusable classes components, like architect’s windows no indication on how to use them in program patterns are more general than libraries specify relationships between classes one pattern may represent several interacting classes generic, must be applied to specific problem Patterns have have name and abstract, identify key aspects of design’s structure e.g. name: Colonial Revival abstract: any structure key aspects: typically white or gray wood, facade shows symmetrically balanced windows centered on door; windows have double-hung sashes and panes
4
Patterns We Used constructors accessor/Mutator methods
pattern name: initialization abstract: ensure that objects have proper internal state before they are used key aspects: method that first calls super() and then sets up instance’s own instance variables accessor/Mutator methods pattern name: encapsulation abstract: keeps other objects from changing object’s internal state directly key aspects: make instance variables private or protected and provide public accessor and mutator methods instance variables pattern name: containment abstract: models objects that are composed of other objects key aspect: store components as instance variables, initialize them in the constructor and provide access protection through encapsulation patterns often interact containment pattern uses initialization and encapsulation patterns
5
Pattern Catalogs patterns form catalog of collected knowledge
well over 100 have been catalogued by experts many more on the way formatting and content is key need to find pattern among many must document uses, trade-offs patterns classification creational patterns structural patterns behavioral patterns patterns have standard names bridge adapter proxy common vocabulary helps professionals to communicate e.g. Memento pattern is used in undo e.g. Adapter pattern is used to "adapt" object's class (wrapping it)
6
Creational Patterns abstract factory factory method builder
centralizes decision of what factory to instantiate factory method centralizes creation of an object of a specific type choosing one of several implementations builder separates the construction of a complex object from its representation so that the same construction process can create different representations lazy initialization delays the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed prototype used when the inherent cost of creating a new object in the standard way (e.g., using the 'new' keyword) is prohibitively expensive for a given application singleton restricts instantiation of a class to one object
7
Structural Patterns adapter aggregate bridge composite container
'adapts' one interface for a class into one that a client expects aggregate a version of the composite pattern with methods for aggregation of children bridge decouples an abstraction from its implementation so that the two can vary independently composite a tree structure of objects where every object has the same interface container create objects for the sole purpose of holding other objects and managing them decorator adds additional fuctionality to a class at runtime where subclassing would result in an exponential rise of new classes extensibility aka. framework hides complex code behind a simple interface façade creates simplified interface of an existing interface to ease common tasks flyweight lots of objects share one common properties object to save space proxy a class functions as an interface to another thing pipes and filters: a chain of processes where the output of each process is the input of the next
8
Behavioral Patterns chain of responsibility command event listener
command objects are handled or passed on by logic-containing processing objects command command objects encapsulate an action and its parameters event listener distributes data to objects that are registered to receive it interpreter implements a specialized computer language to rapidly solve a specific set of problems iterator accesses the elements of an aggregate object sequentially without exposing its underlying representation mediator provides a unified interface to a set of interfaces in a subsystem memento provides the ability to restore an object to its previous state (rollback) observer aka publish/subscribe. objects register to observe an event which may be raised by another object state a clean way for an object to partially change its type at runtime strategy algorithms can be selected on the fly template method describes the program skeleton of a program visitor separates an algorithm from an object
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.