0% found this document useful (0 votes)
115 views30 pages

Overview of Design Patterns in Programming

This document provides an introduction to design patterns. It defines a design pattern as a (problem, solution) pair that can be reused to address common programming problems. Design patterns are techniques for solving general design problems that have been successfully used in civil and electrical engineering domains. The document discusses how patterns are used by designers to bridge the gap between problem identification and implementation. It also provides examples of common design patterns like encapsulation, exceptions, and others. Overall, the document serves as a high-level overview of design patterns, their purpose, and some examples.

Uploaded by

Zubair Rahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views30 pages

Overview of Design Patterns in Programming

This document provides an introduction to design patterns. It defines a design pattern as a (problem, solution) pair that can be reused to address common programming problems. Design patterns are techniques for solving general design problems that have been successfully used in civil and electrical engineering domains. The document discusses how patterns are used by designers to bridge the gap between problem identification and implementation. It also provides examples of common design patterns like encapsulation, exceptions, and others. Overall, the document serves as a high-level overview of design patterns, their purpose, and some examples.

Uploaded by

Zubair Rahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Design Patterns Introduction

Engr. Shaukat Ali


shauky84@[Link]
What is a Design Pattern?
 A (Problem, Solution) pair.

 A technique to repeat designer success.

 Borrowed from Civil and Electrical


Engineering domains.
Designer How Patterns are used?
 Design Problem.
Programmer
 Solution.
 Implementation details.

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
 Encapsulation (Data Hiding)
 Subclassing (Inheritance)
 Iteration
 Exceptions
Encapsulation pattern
 Problem: Exposed fields are directly
manipulated from outside, leading to
undesirable dependences that prevent
changing the implementation.

 Solution: Hide some components, permitting


only stylized access to the object.
Exception pattern
 Problem: Code is cluttered with error-
handling code.

 Solution: Errors occurring in one part of the


code should often be handled elsewhere. Use
language structures for throwing and catching
exceptions.
Derived Conclusion
 Patterns are Programming language features.

 Programming languages are moving towards


Design.

 Many patterns are being implemented in


programming languages.
Pattern Categories
 Creational Patterns concern the process of
object creation.

 Structural Patterns concern with integration


and composition of classes and objects.

 Behavioral Patterns concern with class or


object communication.
What is the addressing Quality
Attribute?
 Modifiability, Exchangeability, Reusability,
Extensibility, Maintainability.

What properties these patterns


provide?
 More general code for better Reusability.

 Redundant code elimination for better


Maintainability.
Design pattern example
Some examples of Design
Patterns
Template for discussion
 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 a
subsystem.
Facade (Software counterpart)
Flyweight (Non software example)

Use sharing to
support large
numbers of fine-
grained objects
efficiently
Flyweight (Software counterpart)

Memory
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 Password Balance


page checking statement
Memento (Non software example)

Externalize
object’s state so
that object can be
restored to this
state later.
Memento (Software counterpart)
The Memento design pattern defines three distinct roles:

1. Originator - the object that knows how to save itself.


2. Caretaker - the object that knows why and when the
Originator needs to save and restore itself.
3. Memento - the lock box that is written and read by the
Originator, and shepherded by the Caretaker.
Observer (Non software example)

When an object
changes its state,
all its dependants
are notified.
Observer (Software counterpart)

Internet

Cricinfo
Server
Adapter (Non software example)

Convert the
interface of a
class into another
Interface clients
expect.
Adapter (Software counterpart)

Java Native
Java module C++ module
Interface (JNI)
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 Syntax Semantic Intermediate Interpret


analysis analysis analysis code ation

Java Compiler

Java Java Semantic Java byte


JVM
Lexer Parser analysis code

Python Compiler

Python Python Semantic Python byte


PVM
Lexer Parser analysis code
Iterator (Non software example)

Provide a way to
access the
elements of a set
sequentially.
Iterator (Software counterpart)
3/26/2019 32

Common questions

Powered by AI

One of the key attributes that design patterns address is extensibility, which pertains to the ability to extend or enhance existing software functionalities. By employing design patterns, software systems can accommodate future growth and changes without the need for extensive refactoring. For example, the use of patterns like the Adapter can help integrate new functionalities or modules into existing systems seamlessly, thus promoting ease of scalability and the efficient incorporation of future requirements .

The Encapsulation pattern addresses the problem where exposed fields are directly manipulated from outside, leading to undesirable dependencies and making it difficult to change the implementation without affecting external components. The solution is to hide some components within the object, only permitting access through stylized interfaces, which improves the integrity of the data within objects and encapsulates the complexity of operations from external manipulation .

Behavioral design patterns are crucial in defining how objects interact and communicate within software systems. They facilitate clear and efficient communication, which improves overall system cooperation and contributes to robust, maintainable designs. For instance, the Observer pattern allows an object (the subject) to notify all its dependent objects (observers) of state changes, promoting loose coupling and thereby enhancing system extensibility and maintainability .

Structural design patterns help in the composition and integration of classes and objects to form larger structures. They focus on simplifying relationships between entities to streamline the architecture of software. An example is the Facade pattern, which provides a unified interface to a set of interfaces in a subsystem, simplifying usage of complex systems .

The Chain of Responsibility pattern enhances error handling by allowing a request to be passed along a chain of potential handlers until it is handled by an appropriate handler. This pattern decouples the sender and receiver, providing flexibility and extensibility in the error-handling process. For instance, in a customer support system, a complaint could be initially handled by a chatbot, and if not resolved, escalated sequentially to a junior representative, senior representative, and finally a manager, ensuring issues are resolved at the appropriate level of expertise .

Implementing the Memento pattern in a distributed system may require modifications such as secure state management across multiple nodes and efficient synchronization mechanisms. Since the Memento involves saving and restoring object states, ensuring consistency and data integrity across nodes is crucial. Aspects like version control, efficient data serialization/deserialization, and maintaining minimal but sufficient state information to reduce bandwidth and improve performance may also be integral, alongside robust security measures to protect sensitive data during state transitions .

Integrating non-software examples significantly aids in understanding and teaching complex design patterns by providing tangible analogs that facilitate comprehension of abstract concepts. For instance, using a restaurant kitchen as a metaphor for the Flyweight pattern (sharing commonly used resources efficiently) helps learners visualize how these patterns apply in real-world scenarios beyond software, fostering intuitive understanding and retention, thus bridging the gap between theoretical knowledge and practical application .

Design patterns serve as a mechanism to encapsulate proven solutions to common problems, essentially acting as a template for successful design strategies. They facilitate the bridging of the gap between design and implementation by offering a structured approach to problem solving in software development. By providing solutions in the form of reusable templates, they reduce the time and effort required to translate design concepts into executable code, thus streamlining the process from design to implementation .

In a real-world application such as a stock market monitoring system, the Observer pattern can be implemented to manage real-time stock price updates. Here, the stock market server acts as the subject, while various client applications (e.g., broker dashboards, mobile apps) act as observers. When the price of a stock changes, the server notifies all registered client applications, ensuring they display the current prices. This pattern offers an efficient way to manage dependencies, ensuring that updates are broadcasted accurately and timely to all subscribers, thus maintaining consistency across different platforms .

Creational design patterns aid in the instantiation of objects in such a way that they promote reuse and modifiability. By standardizing the process of object creation, they allow programmers to replace or extend components with minimal changes. For example, the Builder pattern separates the construction of a complex object from its representation, enabling the creation of various object representations from the same construction process .

You might also like