Professional C__ - Marc Gregoire [488]
shared_ptr const vector { if (inFactories.size() == 0) return nullptr; shared_ptr for (size_t i = 1; i < inFactories.size(); i++) { if (inFactories[i]->getNumCarsInProduction() < bestSoFar->getNumCarsInProduction()) { bestSoFar = inFactories[i]; } } return bestSoFar; } Code snippet from CarFactory\CarTest.cpp The following code makes use of this function to build 10 cars, whatever brand they might be, from the currently least busy factory. vector // Create 3 Ford factories and 1 Toyota factory. auto factory1 = make_shared auto factory2 = make_shared auto factory3 = make_shared auto factory4 = make_shared // To get more interesting results, preorder some cars. factory1->requestCar(); factory1->requestCar(); factory2->requestCar(); factory4->requestCar(); // Add the factories to a vector. factories.push_back(factory1); factories.push_back(factory2); factories.push_back(factory3); factories.push_back(factory4); // Build 10 cars from the least busy factory. for (size_t i = 0; i < 10; i++) { shared_ptr shared_ptr theCar->info(); } Code snippet from CarFactory\CarTest.cpp When executed, the program will print out the make of each car produced: Ford Ford Ford Toyota Ford Ford Ford Toyota Ford Ford The results are rather predictable because the loop effectively iterates through the factories in a round-robin fashion. However, one could imagine a scenario where multiple dealers are requesting cars, and the current status of each factory isn’t quite so predictable. Other Uses of Factories You can also use the factory pattern for more than just modeling real-world factories. For example, consider a word processor in which you want to support documents in different languages, where each document uses a single language. There are many aspects of the word processor in which the choice of document language requires different support: the character set used in the document (whether or not accented characters are needed), the spellchecker, the thesaurus, and the way the document is displayed to name a few. You could use factories to design a clean word processor by writing an abstract LanguageFactory superclass and concrete factories for each language of interest, such as EnglishLanguageFactory and FrenchLanguageFactory. When the user specifies a language for a document, the program instantiates the appropriate language factory and attaches it to the document. From then on, the program doesn’t need to know which language is supported in the document. When it needs a language-specific piece of functionality, it can just ask the LanguageFactory. For example, when it needs a spellchecker, it can call the createSpellchecker() method on the factory, which will return a spellchecker in the appropriate language. THE PROXY PATTERN Example: Hiding Network Connectivity Issues Consider
The proxy pattern is one of several patterns that divorce the abstraction of a class from its underlying representation. A proxy object serves as a stand-in for a real object. Such objects are generally used when using the real object would be time-consuming or impossible. For example, take a document editor. A document could contain several big objects, such as images. Instead of loading all those images when opening the document, the document editor could substitute all images with image proxies. These proxies don’t immediately load the images. Only when the user scrolls down in the document and reaches an image, the document editor will ask the image proxy to draw itself. Then, the proxy will delegate the work to the real image class, which will load the image.