Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Outline Software Design Principles What are Design Patterns? Types of Design Patterns Details on the Design Patterns
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".
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.
Open Close Principle Software entities like classes, modules and functions should be open for extension but closed for modifications.
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.
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?
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.
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.
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.
Creational patterns The first type of design pattern is the creational pattern. Examples: Prototype. Singleton. Abstract Factory. Builder. Factory Method.
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.
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.
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.
Singleton Design Pattern (Cont.)
_lockThis 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.
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.
Shallow Copy
Deep copy
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.
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.
Prototype Design Pattern (Cont.)
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.
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.
Adapter Design Pattern (Cont.)
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.
Facade Design Pattern (Cont.)
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.
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.
Template Method Design Pattern (Cont.)
Observer Design Pattern
That’s all folks!