Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 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. C h a p t e r 3 – P a g e 31 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.

3 The Factory Method Pattern The Product is an abstract interface for the objects being created. C h a p t e r 3 – P a g e 32 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.

4 Non-Software Example The 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. C h a p t e r 3 – P a g e 33 Report documents will instantiate Title, Text, and Bibliography pages, while Resume documents will instantiate Skills, Education, and Experience pages.

5 C h a p t e r 3 – P a g e 34 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 Example Instead 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.

6 Computer Factory Method Code C h a p t e r 3 – P a g e 35 #include 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(); }

7 Factory Method Design Advantages C h a p t e r 3 – P a g e 36 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.


Download ppt "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."

Similar presentations


Ads by Google