Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software Design and Architecture

Similar presentations


Presentation on theme: "Software Design and Architecture"— Presentation transcript:

1 Software Design and Architecture
Factory Method Design Patterns Muhammad Nasir

2 Factory Method Intent Also Known As
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. Also Known As Virtual Constructor The factory method pattern is an object-oriented design pattern to implement the concept of factories. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The factory method design pattern handles this problem by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.

3 Factory Method Motivation Consider the example of an ImageReader and subclasses which implement the ImageReader interface and instantiate objects of ImageReader for reading images based on the type of image e.g., JpegReader, GifReader etc.

4 Factory Method Applicability Use Factory method design pattern when
A class can’t anticipate the class of objects it must create A class wants its subclasses to specify the objects it creates. Classes delegate responsibility to one of several helper subclasses, and we want to localize the Knowledge of creating object.

5 Factory Method Structure

6 Factory Method Example

7 Factory Method Participants Product Concrete product Creator
defines the interface of objects the factory method creates Concrete product implements the object interface Creator declares the factory method Concrete creator overrides the factory method to create an instance with the desired attributes

8 Factory Method Collaborations
The creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate subclass.

9 Factory Method Consequences Advantage Potential disadvantage
Factory methods eliminate the need to bind application-specific classes into code. Potential disadvantage Clients might have to subclass the creator class just to create a particular concrete Product object.

10 Factory Method Sample Code

11 Factory Method

12 Factory Method Related Patterns
Abstract Factory pattern often implemented with Factory Methods Template Methods contain calls to Factory Methods

13 The End Thanks for listening Questions would be appreciated.


Download ppt "Software Design and Architecture"

Similar presentations


Ads by Google