factory

Factory methods in Ruby

五迷三道 提交于 2019-12-29 03:31:27
问题 What is the slickest, most Ruby-like way to have a single constructor return an object of the appropriate type? To be more specific, here's a dummy example: say I have two classes Bike and Car which subclass Vehicle . I want this: Vehicle.new('mountain bike') # returns Bike.new('mountain bike') Vehicle.new('ferrari') # returns Car.new('ferrari') I've proposed a solution below, but it uses allocate which seems way too implementation-heavy. What are some other approaches, or is mine actually ok

Factory methods in Ruby

て烟熏妆下的殇ゞ 提交于 2019-12-29 03:31:08
问题 What is the slickest, most Ruby-like way to have a single constructor return an object of the appropriate type? To be more specific, here's a dummy example: say I have two classes Bike and Car which subclass Vehicle . I want this: Vehicle.new('mountain bike') # returns Bike.new('mountain bike') Vehicle.new('ferrari') # returns Car.new('ferrari') I've proposed a solution below, but it uses allocate which seems way too implementation-heavy. What are some other approaches, or is mine actually ok

How can I return “none” as a default case from a factory?

倾然丶 夕夏残阳落幕 提交于 2019-12-25 11:54:48
问题 I have this code (the whole story behind it is here: https://codereview.stackexchange.com/questions/28990/fancy-pants-vs-cowboy-coding): public class BeltPrinterFactory : IBeltPrinterFactory { public IBeltPrinter NewBeltPrinter() { switch (printerChoice) { case BeltPrinterType.ZebraQL220: return new ZebraQL220Printer(); case BeltPrinterType.ONiel: return new ONielPrinter(); default: return new ZebraQL220Printer(); } } } ...but have added a "None" to the enum, as many customers will not have

Implementation of Facade in front of Factory class Laravel 5.4

时间秒杀一切 提交于 2019-12-25 05:20:13
问题 For some context - earlier today I was struggling to figure out how to implement a facade similar to Cache - where I could set a provider (like disk()), but also have a generic fall back provider when not supplied. Now, I got the basic infrastructure working, but I think my implementation is nasty. Having call default() or provider() just stinks. However, there is a concept or something I'm missing to fill in the gaps here. Implementing similar functionality to Cache::disk('x') in Laravel

Is a private static list an appropriate way of limiting the set of instances of a class

淺唱寂寞╮ 提交于 2019-12-24 19:25:09
问题 I am trying to avoid multiple instances of a class being created with the same internal data. I tried an implementation with a separate class for building the MCode but trying to protect the MCode constructor did not work so I have come back to this implementation. I wonder if this is good design or what better solution there may be? public class MCode : IEquatable<MCode> { private readonly static List<MCode> Instances; public AEnum AE { get; } public byte C { get; } public BEnum BE { get; }

Factory pattern with static registration

帅比萌擦擦* 提交于 2019-12-24 15:55:18
问题 I'm having a problem when trying to register my types using their static constructors, with the following factory: public class Factory<T> { public static Factory<T> Instance { get { return _instance; } } private static Factory<T> _instance = new Factory<T>(); private Factory() { } static Factory() { } static Dictionary<string, Type> _registeredType = new Dictionary<string, Type>(); public void Register(string id, T obj) { if (obj.GetType().IsAbstract || obj.GetType().IsInterface) throw new

How to trigger fieldset factory in ZF3

♀尐吖头ヾ 提交于 2019-12-24 15:26:29
问题 I need to use factory for fieldset. I know how to do it for form, but how to do it for fieldset? The form code is: namespace Application\Form; use Application\Fieldset\Outline; use Zend\Form\Element; use Zend\Form\Form; class Message extends Form { public function __construct() { parent::__construct('message'); $this->setAttribute('method', 'post'); $this->add([ 'type' => Outline::class, 'options' => [ 'use_as_base_fieldset' => true, ], ]); $this->add([ 'name' => 'submit', 'attributes' => [

How to convert a string into a piece of code (Factory Method Pattern?)

≯℡__Kan透↙ 提交于 2019-12-24 09:29:38
问题 Let's say we have a String like this: String string2code = "variable = 'hello';"; How could we convert that String to a piece of code like this?: variable = "hello"; 回答1: GroovyShell is the answer: String string2code = "variable = 'hello'; return variable.toUpperCase()"; def result = new GroovyShell().evaluate string2code assert result == "HELLO" 回答2: If you're into more complex stuff later, you can compile whole classes using GroovyClassLoader . private static Class loadGroovyClass( File

Resolve instances by key and auto-registration with SimpleInjector

微笑、不失礼 提交于 2019-12-24 07:48:28
问题 I'm trying to resolve instances by key with SimpleInjector. In my case, the keys are strings which are coming from a configuration file, and I need the factory to return the correct type based on the string. I used a similar solution like the one described in the link above, but changed it slightly, so the instances can provide their own keys. (there will be many classes that implement IFoo , so I'd like to auto-register them with their keys) Here is the complete working example (.NET Core

Resolve instances by key and auto-registration with SimpleInjector

牧云@^-^@ 提交于 2019-12-24 07:48:25
问题 I'm trying to resolve instances by key with SimpleInjector. In my case, the keys are strings which are coming from a configuration file, and I need the factory to return the correct type based on the string. I used a similar solution like the one described in the link above, but changed it slightly, so the instances can provide their own keys. (there will be many classes that implement IFoo , so I'd like to auto-register them with their keys) Here is the complete working example (.NET Core