60 likes | 202 Views
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.
E N D
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. 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.
The Factory Method Pattern Chapter 3 – Page 32 The Product is an abstract interface for the objects being created. 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 ConcreteCreatoroverrides the Creator’s factory method to return an instance of a ConcreteProduct.
Non-Software Example Chapter 3 – Page 33 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. Report documents will instantiate Title, Text, and Bibliography pages, while Resume documents will instantiate Skills, Education, and Experience pages.
Computer Factory Method Example Chapter 3 – Page 34 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. 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 Code Chapter 3 – Page 35 #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; } }; classComputerFactory { 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(); }
Factory Method Design Advantages Chapter 3 – Page 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.