5

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)

Johannes
  • 51
  • 4
  • Usually abstract classes in Java begins with 'Abstract' in name. In your case this should be AbstractGenerator. Also in Java it is not necessary to start the interface with "I", usually the interface name should be a simple noun like Generator. – kavai77 Mar 18 '14 at 17:20
  • nice hint, but completely useless for answering my question in any way. – Johannes Mar 18 '14 at 17:30
  • This isn't meant to answer your question, but I want to point out that calling overridable methods in a constructor is a [very bad idea](http://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors). –  May 14 '14 at 12:43

3 Answers3

1

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.

Alexis King
  • 43,109
  • 15
  • 131
  • 205
  • That is exactly what I have come up with so far. But it is not fully answering my question. Should have added it to the post. – Johannes Mar 18 '14 at 17:25
  • @Johannes I would conclude that there is no formal name for this particular pattern, or at least no well-known one. – Alexis King Mar 18 '14 at 17:26
0

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

Scott Shipp
  • 2,241
  • 1
  • 13
  • 20
  • I am not quite convinced, I am not exposing the functionality of adding, removing, getting objects to the user. The more I am delegating the requested operation (silently) to the underlying specific implementation – Johannes Mar 18 '14 at 17:35
  • Yet you are asking the client code to use this object to look up another, no? If you are not quite convinced, perhaps it is a combination of facade + registry design patterns. – Scott Shipp Mar 18 '14 at 17:43
0

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?

Community
  • 1
  • 1
dkatzel
  • 31,188
  • 3
  • 63
  • 67
  • I don't think this is like the Command or Handler pattern linked. Whereas those pass an object that implements an interface into the method via a parameter, and then the method calls the interface's single method (such as .execute()), in this case what is passed in is a type (SomeType) and then a separate lookup takes place to actually locate the correct generator for that type. Then the single method (.generate() in this case) is called on the looked-up object. Meanwhile the calling code does not know what Generator object corresponds to the SomeType that it passed in. – Scott Shipp Mar 19 '14 at 04:39
  • The `generate(SomeType)` is the command. I don't think it matters how the handler part finds which implementation of `IGenerator` to call, it's still the same pattern. – dkatzel Mar 19 '14 at 14:01
  • I wonder, though. Look at how the client code interacts with the API here. In the command pattern, the client code holds a particular implementation of the interface, and passes it into the API. It is telling the API, execute this command that I'm giving you. But here things are completely reversed. The client code has no idea what implementation of IGenerator it wants to call. The GeneratorBase figures that out for the client code instead. To top it off, this pattern shows an abstract base class maintaining a map of all its implementations so that it can respond to client calls. – Scott Shipp Mar 19 '14 at 17:09
  • I agree with @ScottShipp – Johannes Mar 20 '14 at 17:11