factory-pattern

Call constructor of template parameter

可紊 提交于 2019-12-10 22:33:58
问题 Given a templated factory method, I would like to call different constructors based on the constructors the template parameter provides: template<typename T> T* Factory::create() { if (hasOnlyDefaultConstructor<T>()) return new T(); else return new T(this); } Two problems: If T does not have a constructor T(Factory*), then there are compilation problems. How to write hasOnlyDefaultConstructor()? In general I would like the following: template<typename T> T* Factory::create() { if

implement factory pattern for products with conditional compiling

本秂侑毒 提交于 2019-12-10 15:26:31
问题 I'd like to implement factory (or some other pattern) in a way that will allow me to compile the code without introducing type dependency. enum CarType { BMW, PORSCHE, MERC }; class CarFactory { public: static Car* create(CarType type) { switch(type) { case BMW : return new BMWCar(); case PORSCHE : return new PorscheCar(); default : return new MercCar(); } } }; When I compile CarFactory, I need to include BMWCar, PorscheCar and MercCar as a part of my compilation/linking unit. The way my

How Dependency Injection Fosters Testability

不问归期 提交于 2019-12-10 13:40:36
问题 I've been reading up on the Factory pattern, and have come across articles that suggest using the Factory pattern in conjunction with dependency injection to maximize reusability and testability. Although I have not been able to find any concrete examples of this Factory-DI hybrid, I'm going to try and give some code examples of my interpretation . However, my question is really about how this approach improves testability. My interpretation: So we have a Widget class: public class Widget { /

Which design pattern is the opposite of the Factory pattern?

六月ゝ 毕业季﹏ 提交于 2019-12-10 12:44:10
问题 I was wondering if there is an opposite pattern of the factory pattern. For example, when a certain object needs to be deleted some extra work needs to be done, to undo the configuration which was performed in the factory object. Extending the factory object with a Delete method for instance seems wrong, since the factory pattern is a strict creational pattern. Update: The reason why I'm using a factory is because the configuration which needs to be done would introduce some dependencies to

C#: Abstract Strategy base class serving as Abstract Factory for Strategy objects

↘锁芯ラ 提交于 2019-12-09 19:04:59
问题 I am trying to create a web-based tool for my company that, in essence, uses geographic input to produce tabular results. Currently, three different business areas use my tool and receive three different kinds of output. Luckily, all of the outputs are based on the same idea of Master Table - Child Table, and they even share a common Master Table. Unfortunately, in each case the related rows of the Child Table contain vastly different data. Because this is the only point of contention I

Singleton in Conjunction with the Factory Pattern in PHP5

北战南征 提交于 2019-12-09 06:46:26
问题 What is the best method for using singleton design pattern in conjunction with the factory method pattern in PHP5? My simplest usage scenario for this is instantiation selective database connection only once for each database type. 回答1: singleton factory for DB connection: class Registry { private static $_objects; public static function set($key, $object) { if (!array_key_exists($key, self::$_objects)) self::$_objects[$key] = $object; } public static function get($key) { if (array_key_exists

How to use a factory with Dependecy Injection without resorting to using Service Locator pattern

不想你离开。 提交于 2019-12-09 05:09:06
问题 I have a GUI application. In it I allow a user to select from a container-provided list of algorithms. Each algorithm will be kicked off as a background task in another view. I need to support multiple instances of this view, and support multiple instances of the same algorithm. That view will also be provided by the container. The algorithm is also stateful. So I have a case where I need to create instances of my view and algorithm and bind them together at runtime. I don't have static

Factory pattern : iBeacon Delegates are not called from implemention file

限于喜欢 提交于 2019-12-08 14:46:53
问题 I introduced “ Factory Pattern ” in my beacon scanning module. I referred http://crosbymichael.com/objective-c-design-patterns-factory.html In my Factory class, 2 modes of beacons are switched between Interface classes “ PCGoogleBeacon.h ” and “ PCAppleBeacon.h ”. //Header file of Factory typedef enum beaconMode { iBeacon, Eddystone } BeaconMode; @interface PCBeaconFinder : NSObject +(id) searchForBeaconMode:(BeaconMode) beaconMode; @end //Implementation of Factory +(id) searchForBeaconMode:

Building a “factory” in PHP

白昼怎懂夜的黑 提交于 2019-12-08 11:18:46
问题 I have created a File class, which takes care of all operations on files, I/O, and which acts differently depending on the nature of the files. I'm not happy with its actual structure, which looks like this: class File { function __construct($id) { $bbnq = sprintf(" SELECT * FROM documents WHERE id = %u", $id); $req = bbnf_query($bbnq); $bbn = $req->fetch(); $this->file_type = $bbn['file_type']; $this->file_name = $bbn['file_name']; $this->title = $bbn['title']; } function display() { return

Is it still a factory if it shares instances across requests?

懵懂的女人 提交于 2019-12-08 04:41:00
问题 Ok, this is entirely semantics and naming...but is my implementation still a factory pattern if it's used, for example, to return a singleton instance? That is, is it still proper to call it a factory if it only constructs a new instance once: // am I still a factory? public static class SomeFactory { [ThreadStatic] private static object _currentSomething; public static object Something { get { return _currentSomething ?? _currentSomething = new object(); } } } 回答1: It looks factory and I