factory-pattern

Replacing abstract factory with Guice?

微笑、不失礼 提交于 2019-12-06 04:06:10
问题 I am new to Guice and I was wondering how far I can take it. I have an interface UserInfo with multiple implementing classes GoogleUserInfo , FacebookUserInfo , TwitterUserInfo etc. These classes are created using a factory public class UserInfoFactory { public UserInfo createFromJsonString(String jsonspec) { . . . } } The creation is controlled by the JSON string jsonspec which controls which of the implementing classes of UserInfo is returned. Specifically, there is a JSON string element

Convert a class to a subclass on instantiation

雨燕双飞 提交于 2019-12-06 02:14:12
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) # do initialization stuff end def category? # query the API to get the namespace of the page and then

Design pattern for cost calculator app?

霸气de小男生 提交于 2019-12-05 19:54:49
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 I want is input on the conditionals that (here) appear repeated in two places. The suggestions I got

Simple factory and Factory Method Design pattern difference

三世轮回 提交于 2019-12-05 18:36:50
I am learning design patterns newly & I am trying to understand the difference between Simple Factory & Factory Method Pattern. First I want to clear that , I tried reading lot of articles from Stack-overflow and other sites regarding the same but that doesn't helped me. Here is my question: Lets consider I have a product hierarchy as shown below: I have written a simple Factory class as shown below public class SimpleItemFactory { static Item getItem(String type) { if(type.equals("Cake")) { return new Cake(); }else if(type.equals("Chocolate")) { return new Chocolate(); }else { return null; }

Using Ninject IOC to replace a factory

為{幸葍}努か 提交于 2019-12-04 23:40:13
I've got a factory method inside a parser. Essentially as I load a token I look up the handler for that token, or drop through to the default handler. I've implemented this as a switch and as a Dictionary<string,Type> but both approaches require me to store the mapping somewhere else than the handler class. We are using Ninject for IOC and so I've realized I can also do it using kernel.Get<ITokenHandler>(tokenName); but that doesn't save me storing the information on what token the handler can deal with in 2 locations. Is there a way I can decorate the handler so this gets mapped automatically

Dependency Injection Container

帅比萌擦擦* 提交于 2019-12-04 20:44:54
I have a Data Access Layer library that I would like to make "portable". The reason I like it to be portable is because I want to work with SQL Azure & Azure File Storage (eg, data + pdf reports) as well as Sql Server 2008R2 and File System storage on a concrete server. Based on spec the system is supposed to go live with the later implementation (sql + file system storage), while upon a meeting a certain scalability threshold we plan on moving to Azure. The data access class I use implements IDataProvider interface (which I built) and it defines the methods that any data access concrete

Use of factory pattern

◇◆丶佛笑我妖孽 提交于 2019-12-04 17:38:04
Which way use of Factory is better(correct)? IPacket info = PacketFactory.CreatePacketObject(PacketType.Info, currentUser, DateTime.Now, " disconnected"); or should I throw out second method in PacketFactory and use this one? IPacket info = PacketFactory.CreatePacketObject(PacketType.Info); info.CreationTime = DateTime.Now; info.Creator = currentUser; info.Data = " disconnected"; or maybe some other? PacketFactory code: public static class PacketFactory { public static IPacket CreatePacketObject(PacketType type) { IPacket packetToCreate = null; switch (type) { case PacketType.Info:

python 3: class “template” (function that returns a parameterized class)

空扰寡人 提交于 2019-12-04 17:14:16
问题 I am trying to create a function that is passed a parameter x and returns a new class C . C should be a subclass of a fixed base class A , with only one addition: a certain class attribute is added and is set to equal x . In other words: class C(A): C.p = x # x is the parameter passed to the factory function Is this easy to do? Are there any issues I should be aware of? 回答1: First off, note that the term "class factory" is somewhat obsolete in Python. It's used in languages like C++, for a

AspectJ constructor force factory pattern

Deadly 提交于 2019-12-04 16:39:38
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(); a.sayHello() returns "gday mate" a is an instaceof AWrapped I understand that this would usually be

Using Reflection in factory pattern [closed]

╄→尐↘猪︶ㄣ 提交于 2019-12-04 16:22:54
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 years ago . Is it a good practice to use Reflection in Factory pattern? public class MyObjectFactory{ private Party party; public Party getObject(String fullyqualifiedPath) { Class c = Class.forName(fullyqualifiedPath); party = (PersonalParty)c.newInstance(); return party; } }