Design Patterns Introduction What is a Design Pattern

  • Slides: 50
Download presentation
Design Patterns Introduction

Design Patterns Introduction

What is a Design Pattern? o A (Problem, Solution) pair. o A technique to

What is a Design Pattern? o A (Problem, Solution) pair. o A technique to repeat designer success. o Borrowed from Civil and Electrical Engineering domains.

Designer How Patterns are used? n n n Design Problem. Solution. Implementation details. Programmer

Designer How Patterns are used? n n n Design Problem. Solution. Implementation details. Programmer Reduce gap Design Implementation Gamma, E. , Helm, R. , Johnson, R. , Vlissides, J. : Design patterns: elements of reusable object-oriented software. 1995. Buschmann, F. , Meunier, R. , Rohnert, H. , Sommerlad, P. , Stal, M. : Pattern-oriented software architecture: a system of patterns. 2002.

Design patterns you have already seen o o Encapsulation (Data Hiding) Subclassing (Inheritance) Iteration

Design patterns you have already seen o o Encapsulation (Data Hiding) Subclassing (Inheritance) Iteration Exceptions

Encapsulation pattern o Problem: Exposed fields are directly manipulated from outside, leading to undesirable

Encapsulation pattern o Problem: Exposed fields are directly manipulated from outside, leading to undesirable dependences that prevent changing the implementation. o Solution: Hide some components, permitting only stylized access to the object.

Exception pattern o Problem: Code is cluttered with errorhandling code. o Solution: Errors occurring

Exception pattern o Problem: Code is cluttered with errorhandling code. o Solution: Errors occurring in one part of the code should often be handled elsewhere. Use language structures for throwing and catching exceptions.

Derived Conclusion o Patterns are Programming language features. o Programming languages are moving towards

Derived Conclusion o Patterns are Programming language features. o Programming languages are moving towards Design. o Many patterns are being implemented in programming languages.

Pattern Categories o Creational Patterns concern the process of object creation. o Structural Patterns

Pattern Categories o Creational Patterns concern the process of object creation. o Structural Patterns concern with integration and composition of classes and objects. o Behavioral Patterns concern with class or object communication.

What is the addressing Quality Attribute? o Modifiability, Exchangeability, Reusability, Extensibility, Maintainability. What properties

What is the addressing Quality Attribute? o Modifiability, Exchangeability, Reusability, Extensibility, Maintainability. What properties these patterns provide? o More general code for better Reusability. o Redundant code elimination for better Maintainability.

Design pattern example

Design pattern example

Some examples of Design Patterns

Some examples of Design Patterns

Template for discussion o o o Non-software example. Pattern intent. Software counterpart example. Non-software

Template for discussion o o o Non-software example. Pattern intent. Software counterpart example. Non-software examples are derived from paper & PPT – “Non-software examples of software design patterns” (OOPSLA 97).

Facade (Non software example) Provide a unified interface to a set of interfaces in

Facade (Non software example) Provide a unified interface to a set of interfaces in a subsystem.

Facade (Software counterpart)

Facade (Software counterpart)

Flyweight (Non software example) Use sharing to support large numbers of finegrained objects efficiently

Flyweight (Non software example) Use sharing to support large numbers of finegrained objects efficiently

Flyweight (Software counterpart) Memory

Flyweight (Software counterpart) Memory

Chain of Responsibility (Non software example) Chain the receiving objects and pass the request

Chain of Responsibility (Non software example) Chain the receiving objects and pass the request along the chain until an object handles it.

Chain of Responsibility (Software counterpart) Internet Login page Password checking Balance statement

Chain of Responsibility (Software counterpart) Internet Login page Password checking Balance statement

Memento (Non software example) Externalize object’s state so that object can be restored to

Memento (Non software example) Externalize object’s state so that object can be restored to this state later.

Memento (Software counterpart)

Memento (Software counterpart)

Observer (Non software example) When an object changes its state, all its dependants are

Observer (Non software example) When an object changes its state, all its dependants are notified.

Observer (Software counterpart) Internet Cricinfo Server

Observer (Software counterpart) Internet Cricinfo Server

Adapter (Non software example) Convert the interface of a class into another Interface clients

Adapter (Non software example) Convert the interface of a class into another Interface clients expect.

Adapter (Software counterpart) Java module Java Native Interface (JNI) C++ module

Adapter (Software counterpart) Java module Java Native Interface (JNI) C++ module

Builder (Non software example) Separate the construction process of a complex object from its

Builder (Non software example) Separate the construction process of a complex object from its representation so that the same construction Process can create different representations.

Builder (Software counterpart) Compiler process Lexical analysis Syntax analysis Semantic analysis Intermediate code Interpret

Builder (Software counterpart) Compiler process Lexical analysis Syntax analysis Semantic analysis Intermediate code Interpret ation Java Parser Semantic analysis Java byte code JVM Semantic analysis Python byte code PVM Java Compiler Java Lexer Python Compiler Python Lexer Python Parser

Iterator (Non software example) Provide a way to access the elements of a set

Iterator (Non software example) Provide a way to access the elements of a set sequentially.

Iterator (Software counterpart)

Iterator (Software counterpart)

Interpreter (Non software example) Interpreter interprets the sentences in a language based on its

Interpreter (Non software example) Interpreter interprets the sentences in a language based on its grammar.

Interpreter (Software counterpart) In Gtalk/Yahoo messengers : -) is interpreted as : -( is

Interpreter (Software counterpart) In Gtalk/Yahoo messengers : -) is interpreted as : -( is interpreted as

Broker (Non software example) Broker component is responsible for coordinating communication between clients and

Broker (Non software example) Broker component is responsible for coordinating communication between clients and remote servers.

Broker (Software counterpart)

Broker (Software counterpart)

Publisher-Subscriber (Non software example) Publishers register themselves to a broker and subscribers discover publisher

Publisher-Subscriber (Non software example) Publishers register themselves to a broker and subscribers discover publisher from broker.

Publisher-Subscriber (Software counterpart) Forums (e. g. movie forums)

Publisher-Subscriber (Software counterpart) Forums (e. g. movie forums)

Proxy (Non software example) Provide a surrogate or placeholder for another object to control

Proxy (Non software example) Provide a surrogate or placeholder for another object to control access to it.

Proxy (Software counterpart)

Proxy (Software counterpart)

Strategy (Non software example) A Strategy defines a set of algorithms that can be

Strategy (Non software example) A Strategy defines a set of algorithms that can be used interchangeably.

Strategy (Software counterpart) Multiple interchangeable weapons available to attack an enemy.

Strategy (Software counterpart) Multiple interchangeable weapons available to attack an enemy.

Mediator (Non software example) Loose coupling between colleague objects is achieved by having colleagues

Mediator (Non software example) Loose coupling between colleague objects is achieved by having colleagues communicate with the Mediator, rather than one another.

Mediator (Software counterpart) Gtalk Server

Mediator (Software counterpart) Gtalk Server

Master-Slave (Non software example) Master component distributes work to identical slave components and computes

Master-Slave (Non software example) Master component distributes work to identical slave components and computes a final result from the results when the slaves return.

Master-Slave (Software counterpart) Movie players Graphics partitioning High-resolution Game players

Master-Slave (Software counterpart) Movie players Graphics partitioning High-resolution Game players

Layers (Software example) Layers structure applications whose dominant characteristic is a mix of low-

Layers (Software example) Layers structure applications whose dominant characteristic is a mix of low- and high- level issues.

MVC (Software example) MVC structures interactive applications.

MVC (Software example) MVC structures interactive applications.

Microkernel (Software example) Microkernel separates the variant and non-variant parts of a product-line application.

Microkernel (Software example) Microkernel separates the variant and non-variant parts of a product-line application.

Reflection (Software example) Reflection pattern provides a mechanism for changing structure and behavior of

Reflection (Software example) Reflection pattern provides a mechanism for changing structure and behavior of a system dynamically.

State (Software example) An object alters its behavior when its internal state changes.

State (Software example) An object alters its behavior when its internal state changes.

Template Method (Software example) Event handling in Java GUI components is unimplemented. Define the

Template Method (Software example) Event handling in Java GUI components is unimplemented. Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.

Observer example

Observer example

11/5/2020 52

11/5/2020 52