Download presentation
1
Design Patterns Introduction
2
Design Patterns What is a Pattern?
"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 has four essential elements Pattern name Problem Solution "the pattern provides an abstract description of a design problem and a general arrangement of elements solves it" Consequences
3
Design Pattern Space Purpose Scope Creational Structural Behavioral
Class Factory Method Adapter Interpreter Template Method Object Abstract factory Builder Prototype Singleton Adapter Bridge Composite Facade Flyweight Proxy Chain of Responsibility Command Iterator Mediator Memento State Strategy Visitor
4
OMT-Based Notation Examples from Text Class Diagram Object Diagram
Inheritance
5
OMT-Based Notation Examples from Text Class Relations
Abstract and Implementation Creating
6
OMT-Based Notation Examples from Text Object Interaction
7
Object, Classes and Type (Again)
Signature An operations name, parameters, and return type Interface Set of all signatures defined by an object's operations Type A name used to denote a particular interface Objects may have many types Different objects in share a type Subtype A type is a subtype of another it its interface contains the interface of its supertype Class A class defines an object's implementation
8
Design Principle 1 Program to an interface, not an implementation
Use abstract classes (and/or interfaces in Java) to define common interfaces for a set of classes Declare variables to be instances of the abstract class not instances of particular classes Benefits of programming to an interface Client classes/objects remain unaware of the classes of objects they use, as long as the objects adhere to the interface the client expects Client classes/objects remain unaware of the classes that implement these objects. Clients only know about the abstract classes (or interfaces) that define the interface.
9
Design Principle 2 Favor object composition over class inheritance
Allows behavior changes at run time Helps keep classes encapsulated and focused on one task Reduce implementation dependencies
10
Designing for Change Some common design problems with design patterns that address the problem Creating an object by specifying a class explicitly Abstract factory, Factory Method, Prototype Dependence on specific operations Chain of Responsibility, Command Dependence on hardware and software platforms Abstract factory, Bridge
11
Designing for Change Dependence on object representations/implementations Abstract factory, Bridge, Memento, Proxy Algorithmic dependencies Builder, Iterator, Strategy, Template Method, Visitor Tight Coupling Abstract factory, Bridge, Chain of Responsibility, Command, Facade, Mediator, Observer
12
Designing for Change Extending functionality by subclassing
Bridge, Chain of Responsibility, Composite, Decorator, Observer, Strategy Inability to alter classes conveniently Adapter, Decorator, Visitor
13
Singleton
14
Singleton Intent Ensure a class only has one instance, and provide a global point of access to it Motivation There are times when a class can only have one instance Applicability Use the Singleton pattern when there must be only one instance of a class, and it must be accessible to clients from a well-known access point when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code
15
Situations for Using a Singleton
Java Security manager All parts of a program must access the same security manager Once set a security manager cannot be changed in a program Logging the activity of a server All parts of the server should use the same instance of the logging system The server should not be able to change the instance of the logging system was it has been set
16
Implementation Java // Only one object of this class can be created
class Singleton { private static Singleton _instance = null; private Singleton() { fill in the blank } public static Singleton getInstance() { if ( _instance == null ) _instance = new Singleton(); return _instance; } public void otherOperations() { blank; } class Program { public void aMethod() { X = Singleton.getInstance(); } }
17
Implementation C++ // Only one object of this class can be created class Singleton { private: static Singleton* _instance; void otherOperations(); protected: Singleton(); public: static Singleton* getInstance(); } Implementation Singleton* Singleton::_instance = 0; Singleton* Singleton::getInstance() { if ( _instance == 0 ) _instance = new Singleton; return _instance; }
18
Singleton and Inheritance
Singleton has subclasses Each subclass needs to be a singleton A program can have one copy of each class Solution Just make each subclass a singleton
19
Structure Participants Singleton
Defines an getInstance operation that lets clients access its unique instance May be responsible for creating its own unique instance
20
Structure Collaborations Clients access a Singleton instance solely through Singleton's getInstance operation Consequences Controlled access to sole instance Reduced name space Permits subclassing Permits a variable number of instances More flexible than class operations
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.