Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

Similar presentations


Presentation on theme: "Software Design Patterns Curtsy: Fahad Hassan (TxLabs)"— Presentation transcript:

1 Software Design Patterns Curtsy: Fahad Hassan (TxLabs)

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

3 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".

4 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.

5 Open Close Principle Software entities like classes, modules and functions should be open for extension but closed for modifications.

6 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.

7 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?

8 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.

9 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.

10 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.

11 Creational patterns The first type of design pattern is the creational pattern. Examples: Prototype. Singleton. Abstract Factory. Builder. Factory Method.

12 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.

13 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.

14 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.

15 Singleton Design Pattern (Cont.)

16 _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.

17 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.

18 Shallow Copy

19 Deep copy

20 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.

21 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.

22 Prototype Design Pattern (Cont.)

23 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.

24 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.

25 Adapter Design Pattern (Cont.)

26

27

28 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.

29 Facade Design Pattern (Cont.)

30

31

32 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.

33 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.

34 Template Method Design Pattern (Cont.)

35

36

37

38 Observer Design Pattern

39 That’s all folks!


Download ppt "Software Design Patterns Curtsy: Fahad Hassan (TxLabs)"

Similar presentations


Ads by Google