Introduction To Design Patterns Part 2 PDF
Document Details
Uploaded by SweepingArcticTundra565
Tags
Summary
This document provides an introduction to design patterns, focusing on different types of patterns for creational and structural design patterns. It covers design issues, and how these patterns apply.
Full Transcript
Introduction to Design Patterns Part 2 RECAP What’s a design pattern? Why should I learn patterns? What are the Classification of patterns? L3 Describing Design Patterns Graphical notations ,whi...
Introduction to Design Patterns Part 2 RECAP What’s a design pattern? Why should I learn patterns? What are the Classification of patterns? L3 Describing Design Patterns Graphical notations ,while important and useful, aren‘t sufficient. capture relationships between classes and objects. consistent format we describe the design pattern. Each pattern is divided into sections according to the following template. UNIT-I 6 L3 Describing Design Patterns UNIT-I 7 The Catalog of Design Pattern Creational Factory Method Abstract Factory Builder Prototype Singleton The Catalog of Design Pattern Factory Method L4 The Catalog of Design Pattern Factory Method Factory Method: Defines an interface for creating an object ,but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. UNIT-I 10 L4 The Catalog of Design Pattern Factory Method UNIT-I 11 L4 The Catalog of Design Pattern Factory Method UNIT-I 12 L4 The Catalog of Design Pattern Factory Method UNIT-I 13 L4 The Catalog of Design Pattern Factory Method UNIT-I 14 L4 The Catalog of Design Pattern Factory Method UNIT-I 15 L4 The Catalog of Design Pattern Factory Method So, when can you apply the Abstract Factory Pattern? 1. Use the Factory Method when you don’t know beforehand the exact types and dependencies of the objects your code should work with. 2. Use the Factory Method when you want to provide users of your library or framework with a way to extend its internal components. 3. Use the Factory Method when you want to save system resources by reusing existing objects instead of rebuilding them each time. UNIT-I 16 The Catalog of Design Pattern Abstract Factory L4 The Catalog of Design Pattern Abstract Factory Abstract Factory: Provide an interface for creating families of related or dependent objects without specifying their concrete classes. The Catalog of Design Pattern Abstract Factory The Catalog of Design Pattern Abstract Factory The Catalog of Design Pattern Abstract Factory The Catalog of Design Pattern Abstract Factory The Catalog of Design Pattern Abstract Factory So, when can you apply the Abstract Factory Pattern? 1. when your code needs to work with various families of related products, but you don’t want it to depend on the concrete classes of those products 2. when you have a class with a set of Factory Methods that blur its primary responsibility. 3. In a well-designed program each class is responsible only for one thing The Catalog of Design Pattern Builder The Catalog of Design Pattern Builder Builder: Separates the construction of the complex object from its representation so that the same constriction process can create different representations. The Catalog of Design Pattern Builder The Catalog of Design Pattern Builder The Catalog of Design Pattern Builder The Catalog of Design Pattern Builder The Catalog of Design Pattern Builder So, when can you apply the Builder? 1. Use the Builder pattern to get rid of a “telescopic constructor” 1. Use the Builder pattern when you want your code to be able to create different representations of some product (for example, stone and wooden houses). 2. Use the Builder to construct Composite trees or other complex objects. The Catalog of Design Pattern Prototype L4 The Catalog of Design Pattern Prototype Prototype: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. UNIT-I 32 The Catalog of Design Pattern Prototype The Catalog of Design Pattern Prototype The Catalog of Design Pattern Prototype The Catalog of Design Pattern Prototype So, when can you apply Prototype? 1. Use the Prototype pattern when your code shouldn’t depend on the concrete classes of objects that you need to copy. 2. Use the pattern when you want to reduce the number of subclasses that only differ in the way they initialize their respective objects. Somebody could have created these subclasses to be able to create objects with a specific configuration. The Catalog of Design Pattern Singleton L4 The Catalog of Design Pattern Singleton Singleton: Ensure a class has only one instance, and provide a point of access to it. UNIT-I 38 The Catalog of Design Pattern Singleton The Catalog of Design Pattern Singleton Make the default constructor private Create a static creation method that acts as a constructor. The Catalog of Design Pattern Singleton So, when can you apply Singleton? 1. Use the Singleton pattern when a class in your program should have just a single instance available to all clients; for example, a single database object shared by different parts of the program. 2. Use the Singleton pattern when you need stricter control over global variables.