factory

simple Java "service provider frameworks"?

I refer to "service provider framework" as discussed in Chapter 2 of Effective Java, which seems like exactly the right way to handle a problem I am having, where I need to instantiate one of several classes at runtime, based on a String to select which service, and an Configuration object (essentially an XML snippet): But how do I get ...

Design decision: class implementing multiple patterns or other method?

I want to create a "singleton-factory class" to retrieve my specialized objects. It is possible to create such a class and does it give me some performance surplus over a simpler solution like a static factory? Or are there any other solutions? This class would be a key component of a data intensive application dealing with constant dat...

Extending spring's default component factory

This is a short one: I want to modify the way spring instantiates my beans (in this case, by creating a proxy instead of simply instantiating it). I can't use Jdk Dynamic Proxies nor cglib to generate the proxies (if this was the case, I could use spring aop). In my opinion, the best way to do this would be to extend spring's bean fa...

Force Singleton Pattern on a Class implementing an Interface.

I better explain the question with an example. I have an Interface Model which can be used to access data. There can be different implementations of Model which can represent the data in various format say XMl , txt format etc. Model is not concerned with the formats. Lets say one such implementation is myxmlModel. Now i want to force m...

How to pass a type to a method - Type argument vs generics.

I have a method of an object which is something like a factory. You give it a type, it creates an instance and does a few other things. An elegant way to do it (in my opinion) is like this: public T MagicMethod<T>() where T: SomeBaseClass { // Magic goes here } But this upsets FxCop who says that this is a bad style - I get a "CA1...

How can I code a factory in Perl and Moose?

Is there a simpler or better (=>easier to maintain) way to use Perl and Moose to instantiate classes based on incoming data? The following code is a stripped down sample from a project I'm working on. package FooBar; use Moose; has 'SUBCLASS' =>('isa'=>'Str',required=>'1',is=>'ro'); has 'MSG' =>('isa'=>'Str',required=>'1',is=>'ro'); s...

C# - Programmer Challenge for Interviews - Programming to an Interface & Patterns

What is a good simple problem to throw at Jr. and Mid level developers to to find out given the opportunity to Program to an Interface (like a simple Factory pattern) if they will do it? ...

Where to find a good database (factory) class with connectionpooling?

I found a lot of information about how to make a database factory or how to deal with connection pooling but i nowhere found a complete solution. I have always used my own solutions, but they're not complete i think or could be improved. Are there some good designed classes any of you use that i can find on the net? Thanks ;-) ...

DRYer tests with associations in factory_girl

Can anyone suggest a better way to make a factory use a pre-built model instance for its association? For example, so that it would be possible below to define a child of the Message factory so that a call to Factory(:my_message) could substitute for Factory(:message,:sender=>@me) ? Sometimes the setup hash is more involved than in this...

Factories, or Dependency Injection for object instantiation in WCF, when coding against an interface?

Hi I am writing a client/server application, where the client is a Windows Forms app, and the server is a WCF service hosted in a Windows Service. Note that I control both sides of the application. I am trying to implement the practice of coding against an interface: i.e. I have a Shared assembly which is referenced by the client appli...

"Singleton" factories, ok or bad ?

I've a lot of (abstract) factories and they're usually implemented as singletons. Usually for the convenience of not having to pass them through layers who really have no business with using or knowing these factories. Most of the times I only need to make a decision at startup of which factory implementation the rest of the code prog...

Which namespace does a factory class belong?

I have a factory class, DocumentLoaderFactory, which simply returns an instance that implements an interface, IDocumentLoader. All implementation resides under the following namespace Skim.Ssms.AddIn.ActiveFileExplorer.Loader But what I am wondering is, which namespace does DocumentLoaderFactory belong? I have placed the factory c...

How to store a factory used in a derived class to initialize its base class?

Assuming you had some kind of factory-created resource that would still belong to the factory after construction like this: public class Resource : IDisposable { public void Dispose() { /* ... */ } } public class ResourceManager : IDisposable { public Resource Load() { /* create resource and add to list */ } public void Dispose()...

C# - how to create an inherited generic collection from a factory method

I am trying to write a factory method that will create a derived instance of an abstract generic collection class. Here are the base classes ... abstract class ItemBase { } abstract class CollectionBase<T> : Collection<T> where T : ItemBase, new() { } ...and their derived classes ... class Item : ItemBase { } class ItemCollection :...

Static Factory Methods to avoid Duplicate Objects

I was reading Joshua Bloch's "Effective Java Programming Language Guide". He explains static factory methods could be used to avoid unnecessary duplicate objects. I haven't quite understood this. Could anyone explain? ...

Convert fixtures into Factory Girl in Rails

I'd like to migrate my fixtures to "Factory Girl" in Rails. Is there any easy way to convert all yml files in a factories.rb file? ...

Generic Factories and Map Values

I have a class with Maps from Ks to Set<V>s, for several different Vs. I'd like to use a generic factory ala: protected static <T> Set<T> SetFactory(int size) { return new HashSet<T>(size); } to do something like for (K key : keySet) map.put(key, SetFactory()); and have the generic part work (I get a compile error, type misma...

When is it a poor design choice to use a factory instead of simply returning a bean?

Our application makes heavy use of factories to spit out objects, but I often wonder if they are always really necessary, and it wouldn't simply be better to create an instance of a "known object" as a bean. The argument is that developers are often customizing the implementation of these returned objects (which are returned with their i...

Java: Using one class to generate objects of another

I have two classes. SpeciesReader takes files and parses them. Species stores certain data about a species, which has been parsed from the file. Currently, I have a method: SpeciesReader.generateSpecies(), which uses the file with which it was instantiated to create a Species object. Is this bad practice/design? Should I somehow find a ...

How to organize my classes/interfaces using repository pattern and factory method pattern?

I am creating a sample, application to understand repository and factory method patterns together, because will use in a bigger project. What i want to achieve is be able to make the website work with different ORM tools. For example the website will have LINQ to SQL and Ado entity frame work classes implemented then using the factory ...