factory-pattern

A map of functions with variadic signatures in c++

跟風遠走 提交于 2019-12-08 03:20:58
问题 From Martin Reddy's API Design for C++ - Chapter 3 (section 3.3.3 Extensible Factory Example) I found this implementation of Factory pattern to be pretty efficient which allows a user to register callback functions (essentially constructors for the derived classes) at run time, which can eventually be called when creating an object of that type. The code is shown below, as taken from the textbook - File : rendererfactory.h class RendererFactory { public: typedef IRenderer *(*CreateCallback)()

Another example of virtual calls vs. type-checking

泪湿孤枕 提交于 2019-12-08 00:20:31
问题 Problem I swear, every time I get it pounded into my brain that I should be using virtual calls vs. type-checking (eg: if (obj is Foo) ... else if (obj is Bar) ... ... I come up with another example where I don't know how to implement the former. I'm implementing a packetized protocol over a serial port. Some pseudo-code will explain this best: OnDataReceived: RcvPacket p = RcvPacket.ReadPacket(comport); // Call factory method if (p is RcvPacketFoo) OnFoo(); if (p is RcvPacketBar) OnBar();

Convert a class to a subclass on instantiation

故事扮演 提交于 2019-12-07 17:17:21
问题 I'm writing a framework for querying the Mediawiki API. I have a Page class which represents articles on the wiki, and I've also got a Category class, which is-a Page with more specific methods (like being able to count the number of members in the category. I've also got a method Page#category? which determines if an instantiated Page object is actually representative of a Mediawiki category page, by querying the API to determine the namespace of the article. class Page def initialize(title)

Design pattern for cost calculator app?

橙三吉。 提交于 2019-12-07 16:59:32
问题 I have a problem that I’ve tried to get help for before, but I wasn’t able to solve it then, so I’m trying to simplify the problem now to see if I can get some more concrete help with this because it is driving me crazy… Basically, I have a working (more complex) version of this application, which is a project cost calculator. But because I am at the same time trying to learn to design my applications better, I would like some input on how I could improve this design. Basically the main thing

Optimum Way To Restore Domain Object

六月ゝ 毕业季﹏ 提交于 2019-12-07 14:32:44
问题 This is such a simple and common scenario I wonder how did I managed until now and why I have problems now. I have this object (part of the Infrastructure assembly) public class Queue {} public class QueueItem { public QueueItem(int blogId,string name,Type command,object data) { if (name == null) throw new ArgumentNullException("name"); if (command == null) throw new ArgumentNullException("command"); BlogId = blogId; CommandType = command; ParamValue = data; CommandName = name; AddedOn =

Understanding Abstract Factory pattern

隐身守侯 提交于 2019-12-07 13:46:58
问题 I've read about abstract factory patter on wiki. But I don't understand really profit by using this pattern. Can you get an example in which is hard to avoid abstract factory pattern. Consider the following Java code: public abstract class FinancialToolsFactory { public abstract TaxProcessor createTaxProcessor(); public abstract ShipFeeProcessor createShipFeeProcessor(); } public abstract class ShipFeeProcessor { abstract void calculateShipFee(Order order); } public abstract class

In which cases it makes sense to use factory classes instead of static functions?

喜欢而已 提交于 2019-12-07 04:18:04
问题 Currently I have created a ABCFactory class that has a single method creating ABC objects. Now that I think of it, maybe instead of having a factory, I could just make a static method in my ABC Method. What are the pro's and con's on making this change? Will it not lead to the same? I don't foresee having other classes inherit ABC, but one never knows! Thanks 回答1: In reality, if you want to get the benefits of a factory class, you need the static method in it's own class. This will allow you

Spring formBackingObject, Business Object Creation, and Factories

二次信任 提交于 2019-12-06 13:31:51
问题 Design question for using Business Objects as formBackingObjects in a Spring SimpleFormController. Our controller's responsibility is to allow an End User to add a new Business Object to our web application. So we are passing our Business Object though the formBackingObject(HttpServletRequest request) method. However, we've run into a conundrum. The factory that we are using to create our new Business Object enforces the business rules that some of the attributes cannot be null. But since we

Another example of virtual calls vs. type-checking

折月煮酒 提交于 2019-12-06 09:06:09
Problem I swear, every time I get it pounded into my brain that I should be using virtual calls vs. type-checking (eg: if (obj is Foo) ... else if (obj is Bar) ... ... I come up with another example where I don't know how to implement the former. I'm implementing a packetized protocol over a serial port. Some pseudo-code will explain this best: OnDataReceived: RcvPacket p = RcvPacket.ReadPacket(comport); // Call factory method if (p is RcvPacketFoo) OnFoo(); if (p is RcvPacketBar) OnBar(); OnFoo: raise Foo event OnBar: raise Bar event Basically, ReadPacket is a factory method in the base class

Nice way of factory method pattern with inheritance

江枫思渺然 提交于 2019-12-06 07:33:08
Suppose I have following class hierarchy: class abstract Parent{} class FirstChild extends Parent {} class SecondChild extends Parent {} And I'd like to create DTO objects from each child: class abstract ParentDTO {} class FirstChildDTO extends ParentDTO{} class SecondChildDTO extends ParentDTO{} I think I need a factory method something like this: ParentDTO createFrom(Parent source); Is there any nice way to do this in Java without instanceof checks and if/else statements? EDIT : This factory method does not work: public ParentDTO create(Parent source) { return _create(source); } private