6
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while allowing the individual applications to define how such objects are created. Chapter 3 – Page 1 This makes the design of the objects more customizable without resorting to new classes. Instead, this pattern defines a separate method for creating objects, letting subclasses override the derived type of product that is created.

Creational Pattern: Factory Method

  • Upload
    conan

  • View
    41

  • Download
    0

Embed Size (px)

DESCRIPTION

Creational Pattern: Factory Method. Chapter 3 – Page 31. At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while allowing the individual applications to define how such objects are created. - PowerPoint PPT Presentation

Citation preview

Page 1: Creational Pattern: Factory Method

Creational Pattern: Factory MethodAt times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while allowing the individual applications to define how such objects are created.

Chapter 3 – Page 1

This makes the design of the objects more customizable without resorting to new classes. Instead, this pattern defines

a separate method for creating objects, letting subclasses override the derived type of product that is created.

Page 2: Creational Pattern: Factory Method

The Factory Method PatternThe Product is an abstract interface for the objects being created.

Chapter 3 – Page 2

The ConcreteProduct is the actual implementation of the objects being created.The Creator defines the factory method, returning an object of type Product; it can also define a default implementation of the factory method, returning a default ConcreteProduct object.The ConcreteCreator overrides the Creator’s factory method to return an instance of a ConcreteProduct.

Product

ConcreteCreator

FactoryMethod()

Creator

FactoryMethod()AnOperation() product = FactoryMethod()

ConcreteProduct

return new ConcreteProduct

Page 3: Creational Pattern: Factory Method

Non-Software ExampleThe Document is the abstract Page creator, instantiating those Page objects in a manner consistent with the particular Document subclass (Resume or Report) that has been established.

Chapter 3 – Page 3

Report documents will instantiate Title, Text, and Bibliography pages, while Resume documents will instantiate Skills, Education, and Experience pages.

Resume

CreatePage()

SkillsPage

EducationPage

ExperiencePage

Report

CreatePage()

TitlePage

TextPage

BibliographyPage

Document

CreatePage()OpenPage()

Page

Page 4: Creational Pattern: Factory Method

Chapter 3 – Page 4

Note the potential compilation benefit here. If we move the abstract Computer into a separate header file with the ComputerFactory, we can then move the implementation of the NewComputer() function (the factory method) into a separate implementation file. That implementation file would be the only one needing knowledge of the derived classes. Thus, if a change is made to any derived class of Computer, or a new Computer type is added, only the implementation file for NewComputer() would need to be recompiled.

Computer Factory Method ExampleInstead of instantiating the computers directly, the client uses the ComputerFactory to generate new computers, specifying whether they should be laptops or desktops during the process.

Computer

ComputerFactory

NewComputer()

Laptop

Desktop

Page 5: Creational Pattern: Factory Method

Computer Factory Method CodeChapter 3 – Page 5#include <iostream>#include <string>using namespace std;

class Computer{ public: virtual void Run() = 0; virtual void Stop() = 0;};

class Laptop: public Computer{ public: virtual void Run() { cout << "Laptop Active" << endl; } virtual void Stop() { cout << "Laptop Hibernating" << endl; }};

class Desktop: public Computer{ public: virtual void Run() { cout << "Desktop Active" << endl; } virtual void Stop() { cout << "Desktop Off" << endl; }};

class ComputerFactory{ public: // This is the factory method, used // instead of a Computer constructor // in order to enable instances of // one of the Computer subclasses to // be returned. static Computer *NewComputer (const string &description) { if (description == "laptop") return new Laptop; if (description == "desktop") return new Desktop; return NULL; }};

void main(){ Computer *C1 = ComputerFactory::NewComputer("laptop"); Computer *C2 = ComputerFactory::NewComputer("desktop"); C1->Run(); C2->Run(); C1->Stop(); C2->Stop(); C1->Run(); C2->Stop();}

Page 6: Creational Pattern: Factory Method

Factory Method Design AdvantagesChapter 3 – Page 6

• The Factory Method pattern reduces duplication of code that would result from having separate methods in multiple classes just to perform custom object creation.

• While the implementation of a Factory Method certainly isn’t simpler than just calling a new operator, it does effectively communicate where instantiation occurs and how it may be overridden.

• By completely decoupling the client from the implementation details of the derived classes, polymorphic creation is made possible.