Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract class implementing interface and aggregating mutliple implementations of same interface. Is there a pattern (name) for it?

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)

like image 682
Johannes Avatar asked Nov 10 '22 11:11

Johannes


1 Answers

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.

like image 86
Alexis King Avatar answered Nov 14 '22 22:11

Alexis King