factory-pattern

Call to implicitly deleted copy constructor

感情迁移 提交于 2021-02-11 12:13:04
问题 I have the following setup: class MyClass { public: static MyClass Clone(const MyClass& other) { return MyClass(other, 10); } static MyClass CreateNormal(int x, int y) { return MyClass(int x, int y); } // There are a few more factory functions. private: // Constructor 1 MyClass(int x, int y) : b_(x, y) {} // Constructor 2 MyClass(const MyClass& other, int c) : b_(other.b_, c) {} // And a lot more constructors. // NotMyClass has its copy constructor deleted. NotMyClass b_; } int main() {

Factory Method pattern to avoid instantiation of objects based on conditional logics

早过忘川 提交于 2021-02-08 10:40:19
问题 In a scenario like below where an object needs to be intantiated based on some conditional logic, can the factory method pattern help to avoid client code getting cluttered due to number of if/elseif conditions (which would also be a maintenance nightmare if more and more products needs to get created due to different variations of logics). Or else is there a any other design pattern that could come to rescue? public interface IProduct { void Method1(); } public class ProductA : IProduct {

Generic Factory method to instantiate a derived class from the base class

不羁岁月 提交于 2021-02-07 20:02:14
问题 I am in the process of creating a factory method which uses a generic abstract type parameter to return the instance of the concrete derived type using reflection. For eg. public abstract class ServiceClientBase : IServiceClient { } public abstract class Channel : ServiceClientBase { } public class ChannelImpl : Channel { } public class ServiceClientFactory { public T GetService<T>() where T : class, IServiceClient { // Use reflection to create the derived type instance T instance = typeof(T)

Generic Factory method to instantiate a derived class from the base class

落花浮王杯 提交于 2021-02-07 20:01:09
问题 I am in the process of creating a factory method which uses a generic abstract type parameter to return the instance of the concrete derived type using reflection. For eg. public abstract class ServiceClientBase : IServiceClient { } public abstract class Channel : ServiceClientBase { } public class ChannelImpl : Channel { } public class ServiceClientFactory { public T GetService<T>() where T : class, IServiceClient { // Use reflection to create the derived type instance T instance = typeof(T)

Generic Factory method to instantiate a derived class from the base class

谁说我不能喝 提交于 2021-02-07 19:59:36
问题 I am in the process of creating a factory method which uses a generic abstract type parameter to return the instance of the concrete derived type using reflection. For eg. public abstract class ServiceClientBase : IServiceClient { } public abstract class Channel : ServiceClientBase { } public class ChannelImpl : Channel { } public class ServiceClientFactory { public T GetService<T>() where T : class, IServiceClient { // Use reflection to create the derived type instance T instance = typeof(T)

How to mock an Object Factory

心已入冬 提交于 2021-02-07 12:23:28
问题 I use Factories (see http://www.php.net/manual/en/language.oop5.patterns.php for the pattern) a lot to increase the testability of our code. A simple factory could look like this: class Factory { public function getInstanceFor($type) { switch ($type) { case 'foo': return new Foo(); case 'bar': return new Bar(); } } } Here is a sample class using that factory: class Sample { protected $_factory; public function __construct(Factory $factory) { $this->_factory = $factory; } public function

How does using the factory design pattern stop a class having to anticipate the class of objects it must create

你离开我真会死。 提交于 2021-02-04 08:15:06
问题 As I understand it the factory design pattern allows objects to be created through the use of a separate object that's sole aim is to create the first one. Different types of factory can be used to create different types of object. I understand that this hides the instantiation of the primary object however surely this is just replaced by the instantiation of the factory? A common advantage for this design pattern is that it stops a class having to anticipate the class of objects it must

Is it forbidden to use static methods in factory pattern?

♀尐吖头ヾ 提交于 2021-02-02 09:55:38
问题 I got told that using static methods when implementing the factory-method-pattern is wrong and should be avoided. Because I wasn't really familiar with the pattern I accepted that answer. After reading articles and getting deeper into it, I couldn't find any source which supports this statement. Can someone help me out with this situation. Should I avoid the static-keyword in factory-methods and if so, when are they useful? 回答1: The first thing to be 100% clear about is that there is no

PHP Call Superclass Factory Method from Subclass Factory Method

让人想犯罪 __ 提交于 2021-01-28 14:22:46
问题 I am writing a php app with subclasses and since I want to have multiple ways to create an object I am doing different factory methods instead of multiple constructors. I have a User with factory methods User::from_id User::from_netid I have several subclasses of User . I was previously calling the parent superconstructor, but when I switched to the factory method that constructor didn't exist. I have Student , a subclass of User . To get it to work, I had to duplicate almost all of my

Giving database context to object Factory

旧时模样 提交于 2021-01-27 13:21:50
问题 There is a question I always ask myself when I'm using a Factory pattern inside my code (C#, but it applies to any language I suppose). I have a "Service" that takes care of interacting with my database, do stuff with objects and interacts with my object model. This Service uses a Factory sometimes to delegate the instanciation of an object. But this factory obviously needs to interact by itself with the database to instanciate my object properly. Is it a good/bad practice to pass the