factory-method

Abstract Factory, Factory Method, Builder

半世苍凉 提交于 2019-12-03 02:54:14
问题 It may seem as if this is question is a dupe, but please bear with me - I promise I've read the related posts (and the GOF book). After everything I've read, I still don't have it clear when to use an Abstract Factory, a Factory Method, or a Builder. I believe it will finally sink in after I see a simple example of a problem which is best approached by, say, a builder and it would be clearly silly to use, say, an abstract factory . Can you provide a simple example where you would clearly use

Is a switch statement applicable in a factory method? c#

送分小仙女□ 提交于 2019-12-02 23:24:10
I want to return an Interface and inside a switch statement I would like to set it. Is this a bad design? private IResultEntity GetEntity(char? someType) { IResultEntity entity = null; switch (someType) { case 'L': //life entity = new LifeEntity(); break; case 'P': //property entity = new PropertyEntity(); break; case 'D': //disability entity = new DisabilityEntity(); break; case 'C': //credit card entity = new CreditCardEntity(); break; } return entity; } Radu094 I don't usually mind switch statements in a factory, provided I can group and control all of the derived classes that I want my

Factory method anti-if implementation

可紊 提交于 2019-12-01 13:26:02
I'm applying the Factory design pattern in my C++ project, and below you can see how I am doing it. I try to improve my code by following the "anti-if" campaign, thus want to remove the if statements that I am having. Any idea how can I do it? typedef std::map<std::string, Chip*> ChipList; Chip* ChipFactory::createChip(const std::string& type) { MCList::iterator existing = Chips.find(type); if (existing != Chips.end()) { return (existing->second); } if (type == "R500") { return Chips[type] = new ChipR500(); } if (type == "PIC32F42") { return Chips[type] = new ChipPIC32F42(); } if (type ==

java parameterized generic static factory

醉酒当歌 提交于 2019-11-29 12:45:00
Is it possible in Java to create a static factory method/class that uses an interface as the parameterized type and return an implementing class of this given interface? Although my knowledge of Generics is limited, here is what I want to do: // define a base interface: public interface Tool { // nothing here, just the interface. } // define a parser tool: public interface Parser extends Tool { public ParseObject parse(InputStream is); } // define a converter tool: public interface Converter extends Tool { public ConvertObject convert(InputStream is, OutputStream os); } // define a factory

Why does Abstract Factory deal with families, and Factory Method with generating a single object?

落花浮王杯 提交于 2019-11-28 10:54:20
问题 From what I have read, the abstract factory pattern typically concerns itself with creating several objects which are all associated with the same family, and the factory method pattern concerns itself with generating a single object. Consider the following example, which flips those concerns: // Factory Method (base class) allowing for creation of families of objects public class BasePizzaCreator{ abstract ISauce CreateSauce(); abstract IToppings CreateToppings(); abstract ICrust CreateCrust

Factory method for objects - best practice?

天涯浪子 提交于 2019-11-28 06:22:41
This is a question regarding the best practice for creating an instance of a class or type from different forms of the same data using python. Is it better to use a class method or is it better to use a separate function altogether? Let's say I have a class used to describe the size of a document. (Note: This is simply an example. I want to know the best way to create an instance of the class not the best way to describe the size of a document.) class Size(object): """ Utility object used to describe the size of a document. """ BYTE = 8 KILO = 1024 def __init__(self, bits): self._bits = bits

java parameterized generic static factory

喜欢而已 提交于 2019-11-28 06:21:30
问题 Is it possible in Java to create a static factory method/class that uses an interface as the parameterized type and return an implementing class of this given interface? Although my knowledge of Generics is limited, here is what I want to do: // define a base interface: public interface Tool { // nothing here, just the interface. } // define a parser tool: public interface Parser extends Tool { public ParseObject parse(InputStream is); } // define a converter tool: public interface Converter

Autowiring of beans generated by EasyMock factory-method?

烈酒焚心 提交于 2019-11-27 16:02:40
问题 I have a problem that seems really strange to me. I have the following setup: An interface: package com.example; public interface SomeDependency { } A spring component: package com.example; @Component public class SomeClass { } A spring test config with a mocked bean generated by EasyMock: <beans ....> <context:component-scan base-package="com.example"/> <bean id="someInterfaceMock" class="org.easymock.EasyMock" factory-method="createMock"> <constructor-arg value="com.example.SomeDependency"

What is the difference between Builder Design pattern and Factory Design pattern?

别来无恙 提交于 2019-11-26 16:45:57
What is the difference between the Builder design pattern and the Factory design pattern? Which one is more advantageous and why ? How do I represent my findings as a graph if I want to test and compare/contrast these patterns ? With design patterns, there usually is no "more advantageous" solution that works for all cases. It depends on what you need to implement. From Wikipedia: Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the

What is the basic difference between the Factory and Abstract Factory Design Patterns? [closed]

邮差的信 提交于 2019-11-26 10:56:50
What is the basic difference between the Factory and Abstract Factory Patterns? With the Factory pattern, you produce instances of implementations ( Apple , Banana , Cherry , etc.) of a particular interface -- say, IFruit . With the Abstract Factory pattern, you provide a way for anyone to provide their own factory. This allows your warehouse to be either an IFruitFactory or an IJuiceFactory , without requiring your warehouse to know anything about fruits or juices. Sudhakar Kalmari Source for this information taken from: http://java.dzone.com/news/intro-design-patterns-abstract Abstract