Reputation: 51
broken down as simple as possible:
Is there a design pattern (name) for the following scenario?
There could be many general purpose Generators available implementing the IGenerator interface, but also some country-dependent ones (extending GeneratorBase). The latter can encapsulate country-dependent generators as well as country-independent generators, depending on the type of "SomeType" to hold an implementation for. Mehtod init()
is meant to be overriden containing registration / mapping process of available generators.
The abstract class GeneratorBase provides functionality for associating and looking up generators for a certain type of "SomyType". In parallel there can IGenerator implementations exist, which are neither aware of "SomeType" or a country.
side note: All usable (not available!) generators are maintained in a separate registry class, which is responsible for looking up the right IGenerator implementation.
The only Interface the client/user gets is the IGenerator interface.
public interface IGenerator
{
public String generate(SomeType s);
}
public abstract class GeneratorBase implements IGenerator
{
private Map generators;
protected String country;
public GeneratorBase(String country){
generators = new HashMap();
this.country = country;
init();
}
public abstract void init();
public String generate(SomeType s)
{
Generator gen = (Generator) generators.get(new Integer(s.getObjectType()));
...
return gen.generate(s);
}
}
Edit: I have come across the Adapter Pattern, Bridge Pattern and Decorator Pattern. None of them really fits this setting. The closest ones could be the Adapter or Bridge Pattern, but neither am I adapting something, nor bridging (abstract class is implementing exactly same interface as implementors)
Upvotes: 5
Views: 183
Reputation: 31648
I've seen this referred to as the Handler Pattern which can be thought of as a variation of the GOF Chain of Responsibility Pattern except the Handler can find the correct implementation to use in an O(1) look up instead of an O(n) walk through the chain.
This is also very similar to the end result of Refactoring to Patterns' Replace Conditional with Command which I would consider the Handler Pattern
See also: "Handler" pattern?
Upvotes: 0
Reputation: 2321
Perhaps it is a version of what Martin Fowler calls the registry pattern in Patterns of Enterprise Application Architecture. There's an article about it at his website
Upvotes: 0
Reputation: 43862
In Java I've heard this called the Adapter Pattern due to the naming of some Java classes such as MouseAdapter
. However, the information I've found on the Adapter Pattern indicates that it is generally used to mean something else.
This pattern is similar in implementation to the Decorator Pattern, though the intent is not exactly the same.
Upvotes: 1