Top Banner
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
39
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Software Design PatternsCurtsy: Fahad Hassan (TxLabs)

Page 2: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Outline

• Software Design Principles • What are Design Patterns?• Types of Design Patterns• Details on the Design Patterns

Page 3: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

What are Software Design Principles?

• Software design principles represent a set of guidelines that helps us to avoid having a bad design.

• The design principles are associated to Robert Martin who gathered them in "Agile Software Development: Principles, Patterns, and Practices".

Page 4: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Characteristics of a Bad Design • According to Robert Martin there are 3 important

characteristics of a bad design that should be avoided:

• Rigidity - It is hard to change because every change affects too many other parts of the system.

• Fragility - When you make a change, unexpected parts of the system break.

• Immobility - It is hard to reuse in another application because it cannot be separated out from the current application.

Page 5: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Open Close Principle

• Software entities like classes, modules and functions should be open for extension but closed for modifications.

Page 6: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Dependency Inversion Principle

• High-level modules should not depend on low-level modules. Both should depend on abstractions.

• Abstractions should not depend on details. Details should depend on abstractions.

Page 7: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Interface Segregation Principle• This principle teaches us to take care how we write our

interfaces.

• Clients should not be forced to depend upon interfaces that they don't use.

• For example if we create an interface called Worker and add a method lunch break, all the workers will have to implement it. What if the worker is a robot?

Page 8: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Single Responsibility Principle

• A class should have only one reason to change.

• When we need to make a change in a class having more responsibilities the change might affect the other functionality of the classes.

Page 9: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

What are Design Patterns?

• Design patterns provide solutions to common software design problems.

• In the case of object-oriented programming, design patterns are generally aimed at solving the problems of object generation and interaction, rather than the larger scale problems of overall software architecture.

• They give generalized solutions in the form of templates that may be applied to real-world problems.

Page 10: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Three Types of Patterns• Creational patterns:• Creational patterns provide ways to instantiate single

objects or groups of related objects.

• Structural patterns:• Structural patterns provide a manner to define relationships

between classes or objects.

• Behavioral patterns:• Behavioral patterns define manners of communication

between classes and objects.

Page 11: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Creational patterns

• The first type of design pattern is the creational pattern.

• Examples:• Prototype.• Singleton.• Abstract Factory. • Builder. • Factory Method.

Page 12: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Singleton Design Pattern • The singleton pattern ensures that only one object of a

particular class is ever created.

• Ensures that a class can only have one concurrent instance.

• Whenever additional objects of a singleton class are required, the previously created, single instance is provided. All further references to objects of the singleton class refer to the same underlying instance.

• This is a creational pattern as it is used to control class instantiation.

Page 13: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Singleton Design Pattern (Cont.)

• An example of the use of a singleton class is a connector to a legacy data file that only supports a single reader at any time.

• In this case, creating multiple instances of the legacy data connector would simply cause all but the first to fail when reading from the file.

• By forcing a single, global instance to be used, only one underlying connection would ever be active.

Page 14: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Singleton Design Pattern (Cont.)

• The UML class diagram above describes an implementation of the singleton pattern.

• The constructor for the class is marked as private. This prevents any external classes from creating new instances.

• The class is also sealed to prevent inheritance, which could lead to subclassing that breaks the singleton rules.

Page 15: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Singleton Design Pattern (Cont.)

Page 16: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Singleton Design Pattern (Cont.)

• Why have we used _lockThis?

• By locking the dummy "_lockThis" variable whilst checking, and possibly creating, the instance variable, all other threads will be blocked for very brief period.

• This means that no two threads will ever be able to simultaneously create their own copies of the object.

Page 17: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Prototype Design Pattern • The prototype design pattern is a design pattern that is used

to instantiate a class by copying, or cloning, the properties of an existing object.

• The new object is an exact copy of the prototype but permits modification without altering the original.

• This is a creational pattern as it is used to control class instantiation and object generation.

• We can do shallow copy as well as deep copy.

Page 18: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Shallow Copy

Page 19: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Deep copy

Page 20: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Prototype Design Pattern (Cont.)

• The pattern is used to instantiate a new object by copying all of the properties of an existing object, creating an independent clone.

Page 21: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Prototype Design Pattern (Cont.)

• The UML class diagram above describes an implementation of the prototype pattern:

• Prototype: This abstract class is the base class for the types of object that can be generated and cloned.

• ConcretePrototype (A/B): These classes inherit from the Prototype class and include any additional required functionality.

Page 22: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Prototype Design Pattern (Cont.)

Page 23: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Structural Patterns• The second type of design pattern is the structural pattern.

• Structural patterns provide a manner to define relationships between classes or objects.

• Examples:• Adapter.• Composite. • Decorator. • Facade. • Flyweight. • Proxy.

Page 24: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Adapter Design Pattern

• The adapter pattern is a design pattern that is used to allow two incompatible types to communicate.

• Where one class relies upon a specific interface that is not implemented by another class, the adapter acts as a translator between the two types.

• This is a structural pattern as it defines a manner for creating relationships between classes.

Page 25: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Adapter Design Pattern (Cont.)

Page 26: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Adapter Design Pattern (Cont.)

Page 27: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Adapter Design Pattern (Cont.)

Page 28: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Facade Design Pattern• The facade pattern is a design pattern that is used to simplify

access to functionality in complex or poorly designed subsystems.

• The facade class provides a simple, single-class interface that hides the implementation details of the underlying code.

• The facade pattern is ideal when working with a large number of interdependent classes, or with classes that require the use of multiple methods, particularly when they are complicated to use or difficult to understand.

Page 29: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Facade Design Pattern (Cont.)

Page 30: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Facade Design Pattern (Cont.)

Page 31: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Page 32: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Behavioral Patterns

• The final type of design pattern is the behavioral pattern. Behavioral patterns define manners of communication between classes and objects.

• Examples:• Chain of Responsibility. • Command. • Interpreter. • Iterator. • Mediator. • Memento. • State. • Strategy. • Template Method. • Visitor.

Page 33: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Template Method Design Pattern (Cont.)

• The template method pattern is a design pattern that allows a group of interchangeable, similarly structured, multi-step algorithms to be defined.

• Each algorithm follows the same series of actions but provides a different implementation of the steps.

• The overall structure of the basic algorithm is defined in an abstract base class.

Page 34: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Template Method Design Pattern (Cont.)

Page 35: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Template Method Design Pattern (Cont.)

Page 36: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Template Method Design Pattern (Cont.)

Page 37: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Template Method Design Pattern (Cont.)

Page 38: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Observer Design Pattern

Page 39: Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

That’s all folks!