meeting -*- Outline -*- ** Factory (23.3) copy figure 23.4 This is motivated by using the adapter pattern: who creates the adapters? how to determine which subclass of the adapter to create? don't want the domain objects to have the responsibility for creating these *** separation of concerns separation concerns means to modularise distinct concerns into different areas of the software, so each has a cohesive purpose. for example, use a different group of objects for application logic vs. communicating with external systems *** factories see also the design patterns book, pages 87 and following and the factory method pattern on pages 107 and following ------------------------------------------ FACTORY (23.3) Problem: who should create objects when: - the exact class will vary - creation logic is complex Solution: assign responsibility to a factory class Example: ------------------------------------------ show figure 23.4 ------------------------------------------ // $RCSfile: ServicesFactory.h,v $ #ifndef ServicesFactory_h #define ServicesFactory_h #include "AccountingAdapter.h" class ServicesFactory { private: AccountingAdapter * accountingAdapter; // ... public: virtual AccountingAdapter * getAccountingAdapter(); // ... }; #endif ------------------------------------------ ------------------------------------------ // $RCSfile: ServicesFactory.cpp,v $ #include "ServicesFactory.h" #include #include "SAPAccountingAdapter.h" #include "TopAccountingAdapter.h" AccountingAdapter * ServicesFactory::getAccountingAdapter() { const char *sysname = getenv("ACCOUNTINGSYSTEM"); if (strcmp(sysname,"SAP") == 0) { return new SAPAccountingAdapter(); } else if (strcmp(sysname,"Top") == 0) { return new TopAccountingAdapter(); } else { // ... return 0; } } ------------------------------------------ *** discussion The Java version uses reflection, so is more dynamic, and has basically no direct coupling to the classes it creates. (data driven design, protected variation) *** related patterns factories are often accessed by the singleton pattern *** also known as Kit