Tag: 工厂模式

带inheritance的工厂方法模式的好方法

假设我有以下类层次结构: class abstract Parent{} class FirstChild extends Parent {} class SecondChild extends Parent {} 我想从每个孩子创建DTO对象: class abstract ParentDTO {} class FirstChildDTO extends ParentDTO{} class SecondChildDTO extends ParentDTO{} 我想我需要一个像这样的工厂方法: ParentDTO createFrom(Parent source); 有没有很好的方法在没有instanceof检查和if / else语句的情况instanceof在Java中执行此操作? 编辑 :此工厂方法不起作用: public ParentDTO create(Parent source) { return _create(source); } private FirstChildDTO _create(FirstChild source) { return new FirstDTO(); } private SecondChildDTO […]

了解抽象工厂模式

我在维基上读过关于抽象工厂模式的文章。 但是我不明白使用这种模式真的有利可图。 你能得到一个很难避免抽象工厂模式的例子吗? 请考虑以下Java代码: public abstract class FinancialToolsFactory { public abstract TaxProcessor createTaxProcessor(); public abstract ShipFeeProcessor createShipFeeProcessor(); } public abstract class ShipFeeProcessor { abstract void calculateShipFee(Order order); } public abstract class TaxProcessor { abstract void calculateTaxes(Order order); } // Factories public class CanadaFinancialToolsFactory extends FinancialToolsFactory { public TaxProcessor createTaxProcessor() { return new CanadaTaxProcessor(); } public […]

AspectJ构造函数强制工厂模式

我想将对象返回从调用更改为constuctor 从 public class A { public A(){ } public String sayHello() { return “hello”; } public String foo() { return “foo”; } } 至 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 […]

在工厂模式中使用reflection

在工厂模式中使用Reflection是一个好习惯吗? public class MyObjectFactory{ private Party party; public Party getObject(String fullyqualifiedPath) { Class c = Class.forName(fullyqualifiedPath); party = (PersonalParty)c.newInstance(); return party; } } PersonalParty实现派对

战略设计模式,generics和类型安全

我想创建以下策略模式与Factory结合,但我希望它是类型安全的。 到目前为止我做了以下事情: public interface Parser { public Collection parse(ResultSet resultSet); } public class AParser implements Parser { @Override public Collection parse(ResultSet resultSet) { //perform parsing, get collection Collection cl = performParsing(resultSet); //local private method return cl; } } public class ParserFactory { public enum ParserType { APARSER } public static Parser createParser(ParserType parserType) { Parser […]

在java中扩充工厂模式

我正在尝试使用工厂模式来创建一个QuestionTypeFactory,其中实例化的类将类似MultipleChoice,TrueFalseQuestion等。 工厂代码看起来像这样 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 EssayQuestion(); } throw new IllegalArgumentException(“Not recognized.”); } } 这个现在可行了。 如果我想添加另一个问题类型,我将需要修改工厂类,我不想这样做。 如何设置它以便每个问题类在Factory中注册自己,这样当我添加新的问题类型时,我不必更改工厂的代码? 我对java有点新,我不知道该怎么做。 编辑 附加信息 所有问题类都实现了IQuestion接口。 我正在寻找一种方法来实现像这样的方法 public static void […]

弹簧动态注塑,工厂般的图案

从dependency injection的延续,延迟注射实践 。 我有Main课程: package test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; import java.util.List; import java.util.Scanner; @Component public class Main { @Autowired private StringValidator stringValidator; @Autowired private StringService stringService; @Autowired private ValidationService validationService; public void main() { scanKeyboardCreateLists(); stringValidator.validate(); final List validatedList = stringValidator.getValidatedList(); for (String currentValid : validatedList) { System.out.println(currentValid); } } […]

没有if,切换实现Factory的最佳方法

我正在寻找许多方法来实现Java中的Factory模式,但仍然找不到一个完美的方法,如果/ switch plus不使用reflection,则不会受到影响。 我找到的最好的一个是Tom Hawtin在这里的答案: https : //stackoverflow.com/a/3434505/1390874 但我最担心的是它将匿名类的HashMap存储在内存中。 问题是除了Tom Hawtin的回答之外,人们还会考虑使用Class.newInstance()吗? 这样可以避免我们在内存中存储不必要的匿名类吗? 加上代码会更干净。 它看起来像这样: class MyFactory { private static final Map factoryMap = Collections.unmodifiableMap(new HashMap() {{ put(“Meow”, Cat.class); put(“Woof”, Dog.class); }}); public Animal createAnimal(String action) { return (Animal) factoryMap.get(action).newInstance(); } }

简单工厂和工厂方法模式的动机

我知道有很多关于不同工厂模式的差异的问题,但答案是如此不同和令人困惑。 我读过的书籍使用不清楚和(简化)简化的例子。 即使在阅读了维基百科的解释之后,我也有很多问题,以及关于它们的大量在线解释,包括所有这些网站上的解释。 我正在阅读的这本书是Head First Design Patterns。 在Simple Factory中,客户端使用单独的类(Creator)和工厂方法(可以是静态的)来返回Products。 在工厂方法模式中,创建者和客户端是相同的东西,他们在同一个类中使用抽象方法来创建新的产品,它们在同一个类中运行。 当然,造物主(或客户)是抽象的,因此关于制作混凝土产品的决定被推迟到子类。 我的理解是否正确(例如,FMP中的客户端和创建者是相同的,我从未在FMP图中看到客户端)? 在Factory Method Pattern中,它表明create方法不能在Creator之外重用,所以它只能在创建一个新的Creator时重用? 有什么情况我可以选择一个而不是另一个? (PS请不要将此标记为重复,我想在此网站上明确这一点)

在Java中实现Factory Pattern的最佳方法

我正在尝试编写工厂模式以在我的程序中创建MainMode或TestMode。 我以前用来创建这些对象的代码是: play = (isMode) ? new MainMode(numberRanges, numberOfGuesses) : new TestMode(numberRanges, numberOfGuesses, randNo()); 我的游戏(游戏)将根据布尔值(isMode)创建MainMode对象或TestMode对象。 正如您所看到的,我在TestMode对象中添加了一个额外的值(randNo())。 此值在TestMode中使用,以允许用户输入自己的“随机数”,而在MainMode构造函数中,这是随机生成的。 在这个程序中,MainMode和TestMode都是抽象类Game的子类。 现在我想用工厂模式替换这一行,虽然我不确定我的TestMode构造函数需要一个额外的对象,我不确定我需要传递这个值的位置。 如果我要创建一个工厂,它需要在一个新的类中,可能名为GameFactory或ModeFactory或类似的东西。 我怎么会这样呢? 编辑:这里的问题是上面的代码在我的GUI中,其中numberRanges,numberOfGuesses和randNo()方法的值是。 我想创建一个Factory类,但我无法通过这些值,因为randNo()激活自己。 这是我的randNo()方法。 private int randNo() { boolean isValidNumber = true; int testRandomNum = 0; while(isValidNumber) { try { testRandomNum = Integer.parseInt(JOptionPane.showInputDialog(“Enter Random Number”)); isValidNumber = false; } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, “Sorry, […]