factory

Is the Factory Method Pattern more flexible than Simple Factory?

岁酱吖の 提交于 2020-01-02 03:49:06
问题 I've been reading the book Head First: Design Patterns, which I have found to be a good introduction to design patterns. However, I've got a question about a claim they make in Chapter 4: They define the "Simple Factory" pattern as follows (Java pseudocode): public abstract class Product { // Product characteristics // Concrete Products should subclass this } public class SimpleFactory { public Product createProduct(){ // Return an instance of some subclass of Product } } public class Store {

DI container, factory, or new for ephemeral objects?

北慕城南 提交于 2020-01-02 02:00:33
问题 When dealing with objects that require data known only at runtime, such as a username and password, where should object instantiation happen: by using new, in a factory, or in a DI container? For example, I could just new an object once I have the data: UserCredentials creds = new UserCredentials(dialog.getUsername(), dialog.getPassword()); Or, I could use a factory: UserCredentials creds = CredentialsFactory.create(dialog.getUsername(), dialog.getPassword()); Or, I could use a provider

How to implement Singleton pattern in Dart using factory constructors?

荒凉一梦 提交于 2020-01-01 19:14:08
问题 I'm trying to implement the singleton pattern in a database helper class, but, I can't seem to understand the purpose of a factory constructor and if there's an alternative method to using it. class DbHelper { final String tblName =''; final String clmnName =''; final String clmnPass=''; DbHelper._constr(); static final DbHelper _db = new DbHelper._constr(); factory DbHelper(){ return _db;} Database _mydb; Future<Database> get mydb async{ initDb() { if(_mydb != null) { return _mydb; } _mydb =

How do I implement a delegate factory?

ε祈祈猫儿з 提交于 2020-01-01 05:17:05
问题 The documentation for Autofac has an interesting page describing its ability to automatically generate delegate factories. It also strongly suggests that you can get similar results without Autofac by writing them by hand. I'm using Unity for IoC and would like to avoid passing the container around to objects that need to create other objects, so how would you write a delegate factory without Autofac? 回答1: Well I've never used Unity so far, so my answer is quite vague. The principal is simple

Factories in Java without a switch statement

人走茶凉 提交于 2019-12-31 14:54:25
问题 I am trying to build a factory object, but having trouble working out a good way of doing it in Java. The application I am writing is used for processing files in various formats, so there is a CodecInterface which applies to all classes which are used for reading and writing files. Let's assume it defines the following methods. Each of these files has a unique human-designated ID string which is used for id'ing the encoder\decoder. String read(); void write(String data); String getID(); The

What is a Factory in OOP

让人想犯罪 __ 提交于 2019-12-30 20:28:23
问题 My understanding of "factory-related" design patterns and their OOP-implementations has always been pretty simple. A "Factory method" is a method inside a class that has an interface (or an abstract class) as a return type and constructs objects implementing this interface based on some internal logic. A "Factory" is a class that only contains factory methods An "Abstract factory" is an interface (or an abstract class) that only contains factory methods But I recently stumbled upon Wikipeda

(static initialization/template instantiation) problems with factory pattern

自闭症网瘾萝莉.ら 提交于 2019-12-30 07:04:04
问题 Why does following code raise an exception (in createObjects call to map::at ) alternativly the code (and its output) can be viewed here intererestingly the code works as expected if the commented lines are uncommented with both microsoft and gcc compiler (see here), this even works with initMap as ordinary static variable instead of static getter. The only reason for this i can think of is that the order of initialization of the static registerHelper_ object ( factory_helper_ )and the std:

(static initialization/template instantiation) problems with factory pattern

和自甴很熟 提交于 2019-12-30 07:03:25
问题 Why does following code raise an exception (in createObjects call to map::at ) alternativly the code (and its output) can be viewed here intererestingly the code works as expected if the commented lines are uncommented with both microsoft and gcc compiler (see here), this even works with initMap as ordinary static variable instead of static getter. The only reason for this i can think of is that the order of initialization of the static registerHelper_ object ( factory_helper_ )and the std:

Delphi Class of in C#

▼魔方 西西 提交于 2019-12-30 03:12:05
问题 I know this question has been asked before, but I have yet to see a short, clear answer, so I'm hoping they won't remove this question and I will now get a clear answer: I am currently working in C# 5.0; .NET 4.5; VS 2012. I am mostly a Delphi guy although I've done lots with C#. In Delphi I have written hundreds of class factories that use the following sort of design (MUCH SIMPLIFIED HERE): unit uFactory; interface type TClassofMyClass = class of TMyClass; TFactoryDict = TDictionary<TMyEnum

Java: factory with method type parameters for class generic

谁都会走 提交于 2019-12-29 08:41:09
问题 Initial Situation: I created a factory interface for some other generic interface A<T> : public interface Factory<T, X extends A<T>> { public X create(); } Problem Description: Now i have the problem that i need to instantiate a factory for every single type T . This becomes very unhandy, especially if some code wants to do some transformation from A<T> to A<T2> , where T2 is some previously unknown type or there are so many different T2 , that i do not want to specify hundreds of factories