factory-pattern

correctly override __new__ in python3

喜欢而已 提交于 2019-12-23 03:01:46
问题 So I am trying override __new__ and let it exist as a factory to create derived instances. After reading a bit on SO, I am under the impression that I should be calling __new__ on the derived instance as well. BaseThing class BaseThing: def __init(self, name, **kwargs): self.name = name # methods to be derived ThingFactory class Thing(BaseThing): def __new__(cls, name, **kwargs): if name == 'A': return A.__new__(name, **kwargs) if name == 'B': return B.__new__(name, **kwargs) def __init__

How can I inject parameter through constructor in Roboguice? [android]

狂风中的少年 提交于 2019-12-22 10:15:38
问题 This question is probably exact duplicate of this one Pass parameter to constructor with Guice Difference is that I use roboguice for android, not just Guice, so answers there does not work for me. Question is - how can I pass initialize parameters into created object? I.e. I have injected interface which should be initialize with some parameter which roboguice does not know. What I see in link I provide, I should create factory interface and register it like this void configure(Binder binder

Understanding the factory method pattern

折月煮酒 提交于 2019-12-22 08:38:58
问题 I'm reading about the Factory Method pattern. I can understand when there is a single factory class, i.e. StoreFactory#getStore() , that returns a Store implementation based on some runtime or other state. But, from reading (e.g. this link), there seems to be a general pattern of people creating an abstract factory class, to which other factory classes extend: public abstract class AbstractFactory { public abstract Store getStore(int store); } public class StoreFactoryA extends

Java Pattern class doesn't have a public constructor, why?

别说谁变了你拦得住时间么 提交于 2019-12-22 03:44:23
问题 I've been reviewing Java Regex Library, surprised by the fact the Pattern class does not have a public constructor which I've taken for granted for years. One reason I suspect the static compile method is being used in favor of constructor could be that constructor would always return a new object while a static method might return a previously created (and cached) object provided that the pattern string is the same. However, it is not the case as demonstrated by the following. public class

AspectJ constructor force factory pattern

纵饮孤独 提交于 2019-12-21 21:33:45
问题 I want to change the object return from call to a constuctor FROM public class A { public A(){ } public String sayHello() { return "hello"; } public String foo() { return "foo"; } } TO public class AWrapped extends A { private A wrapped; public AWrapped() { super(); } public AWrapped(A pWrapped) { wrapped=pWrapped; } public String foo() { return wrapped.foo(); } public String sayHello { return "gday mate"; } } What i want to do is to change the object that is returned from a call A a = new A(

Simple Factory vs Factory Method: Switch statement in factory vs. client

我怕爱的太早我们不能终老 提交于 2019-12-21 16:52:11
问题 I understand that one of the main advantages of the Factory Method over Simple Factory is that it doesn't violate the Open-Closed SOLID Principle. That is, the former doesn't require modifying the switch statement when new types are added. There is one piece on which I am hoping to get clarification. If I were to use a simple factory, I would have a factory like this (simplified): public class ObjectFactory { public static IObject CreateObject(ObjectTypeEnum objectType) { switch (objectType)

DDD Book, Eric Evans: What is meant by “The FACTORY should be abstracted to the type desired rather than the concrete class(es) created.”?

我怕爱的太早我们不能终老 提交于 2019-12-21 04:35:06
问题 In the book Domain Driven Design, by Eric Evans, in Chapter 6 in the section on "Factories" (page 139) it says the following: "The two basic requirements for any good FACTORY are: ... "2. The FACTORY should be abstracted to the type desired rather than the concrete class(es) created." Could you please elaborate on what is meant by that statement about basic requirement number 2. 回答1: Carlos Loth's answer is correct, but you should always remember to use an Abstract Factory as well, as this

Do we ever need to prefer constructors over static factory methods? If so, when?

大兔子大兔子 提交于 2019-12-21 03:44:16
问题 I have been reading Effective Java by Joshua Bloch and so far it really lives up to its reputation. The very first item makes a convincing case for static factory methods over constructors . So much that I began to question the validity of the good old constructors :). The advantages/disadvantages from the book are summarized below: Advantages: They have names! We have total instance control (Singletons, performance, etc.) They can return a subtype/interface Compiler can provide type

augment the factory pattern in java

核能气质少年 提交于 2019-12-20 16:46:10
问题 I am trying to use a factory pattern to create a QuestionTypeFactory where the instantiated classes will be like MultipleChoice, TrueFalseQuestion etc. The factory code looks something like this class QuestionFactory { public enum QuestionType { TrueFalse, MultipleChoice, Essay } public static Question createQuestion(QuestionType quesType) { switch (quesType) { case TrueFalse: return new TrueFalseQuestion(); case MultipleChoice: return new MultipleChoiceQuestion(); case Essay: return new

Am I implementing a generics-based Java factory correctly?

烈酒焚心 提交于 2019-12-20 12:05:11
问题 I don't believe I am implementing the factory pattern correctly because the Application class' createDocument method accepts any class type, not just subclasses of Document . In other words, is there a way I can restrict the createDocument method to only accept subclasses of Document ? Document.java package com.example.factory; public abstract class Document { public Document() { System.out.println("New Document instance created: " + this.toString()); } } DrawingDocument.java package com