factory-pattern

Generate an array with random data without using a for loop

拟墨画扇 提交于 2020-08-04 05:45:07
问题 I am using the faker.js library to generate random data and I have a couple of factory functions that generate a series of user data: const createUser = () => { return { name: faker.name.findName(), email: faker.internet.email(), address: faker.address.streetAddress(), bio: faker.lorem.sentence(), image: faker.image.avatar(), }; }; const createUsers = (numUsers = 5) => { return Array(numUsers).fill(createUser()); }; let fakeUsers = createUsers(5); console.log(fakeUsers); The problem with this

How to dynamically register class in a factory class at runtime period with c++

旧城冷巷雨未停 提交于 2020-06-16 04:05:13
问题 Now, I implemented a factory class to dynamically create class with a idenification string, please see the following code: void IOFactory::registerIO() { Register("NDAM9020", []() -> IOBase * { return new NDAM9020(); }); Register("BK5120", []() -> IOBase * { return new BK5120(); }); } std::unique_ptr<IOBase> IOFactory::createIO(std::string ioDeviceName) { std::unique_ptr<IOBase> io = createObject(ioDeviceName); return io; } So we can create the IO class with the registered name: IOFactory

Factory pattern in Go

夙愿已清 提交于 2020-05-26 10:08:05
问题 I'm trying to implement a factory pattern with Go, here is the example https://play.golang.org/p/ASU0UiJ0ch I have a interface Pet and a struct Dog so Dog should have the properties of Pet , on this case only one which is the specie, when trying to initialize the object Dog via the factory NewPet , can someone please advise. 回答1: Your NewPet factory returns type Pet , not *Pet in the type assertion. (you rarely ever want a pointer to an interface) return Pets[pet].(func(string) Pet)(name)

how to avoid “cascading if\case”-statements in a factory class

[亡魂溺海] 提交于 2020-02-05 03:48:28
问题 I have to create an object based on a specific situation. I read, that the solution could be the Factory Pattern, but in my case it has a lot of disadvantages. For instance: I have an application that manages animals. At some point a client gives to me a list of animals that must be created. A solution by using the factory pattern should be: //PRODUCTS public interface Animal { String getCall(); } public class Dog implements Animal { public String getCall() { return "Bau"; } } public class

how to avoid “cascading if\case”-statements in a factory class

半世苍凉 提交于 2020-02-05 03:48:07
问题 I have to create an object based on a specific situation. I read, that the solution could be the Factory Pattern, but in my case it has a lot of disadvantages. For instance: I have an application that manages animals. At some point a client gives to me a list of animals that must be created. A solution by using the factory pattern should be: //PRODUCTS public interface Animal { String getCall(); } public class Dog implements Animal { public String getCall() { return "Bau"; } } public class

Factory Pattern: How to access properties of concrete product classes from client?

扶醉桌前 提交于 2020-01-25 06:47:13
问题 I'm considering using Factory Pattern for creation of objects in a web application, but what I fail to grasp is how do I access the properties of these objects. Simplified Example: I have a CarFactory Interface, implemented by two concrete classes TruckCarFactory and PickupCarFactory, also a Car Interface implemented by concrete Pickup and Truck classes. Now when creating a new Truck my client speaks to the CarFactory Interface creating a new Car. Truck objects however are supposed to have

Alternatives to using overridden methods in constructors, Java

拥有回忆 提交于 2020-01-24 10:22:13
问题 In a Java project I am coding I have ended up using methods that are overridden in constructors. Something like: class SuperClass { SuperClass() { intialise(); } protected void initialise() { //Do some stuff common to all subclasses methodA(); methodB(); } protected abstract void methodA(); protected abstract void methodB(); } class SubClass1() { SubClass() { super(); } protected void methodA() { //Do something } protected void methodB() { //Do something } } class SubClass2() { SubClass() {

Dependency Injection Container - Factory Pattern

[亡魂溺海] 提交于 2020-01-22 19:48:11
问题 I have been trying to learn about dependency injection and have been reading about and trying to code a small dependency injection container similar to this: http://fabien.potencier.org/article/12/do-you-need-a-dependency-injection-container The one thing that is confusing me is this: Isnt a dependency injection container just a glorified implementation of the factory pattern? If so, why not just call it that, why the need for a fancy term only to confuse matters. If it isnt, can someone

how to create a factory in zend framework 2?

非 Y 不嫁゛ 提交于 2020-01-21 04:47:08
问题 in my Module.php i have the fallowing methods that i would like to move them in a factory class so that i wont clutter the Module class : public function getControllerConfig() { return array( 'factories' => array( 'account-index' => function ($controllerManager) { $serviceManager = $controllerManager->getServiceLocator(); $accountService = $serviceManager->get('account-service'); return new Controller\IndexController($accountService); } ) ); } public function getServiceConfig() { return array

Factory Pattern in C++ — doing this correctly?

百般思念 提交于 2020-01-21 01:36:32
问题 I am relatively new to "design patterns" as they are referred to in a formal sense. I've not been a professional for very long, so I'm pretty new to this. We've got a pure virtual interface base class. This interface class is obviously to provide the definition of what functionality its derived children are supposed to do. The current use and situation in the software dictates what type of derived child we want to use, so I recommended creating a wrapper that will communicate which type of