views:

142

answers:

5

I have such design:

public interface MyInterface {
    public abstract List<Sth> getSth();
}

public class MyConcreteImplementation implements MyInterface {
    private ConcreteSth mSth = new ConcreteSth();

    public List<Sth> getSth(){
        return mSth.getSth(additionalParams); 
    }
}

Purpose of code above is to provide unified method to be called from other classes.

Can this be called a pattern? If so how to name it?

+1  A: 

Adapter

Purpose of code above is to provide unified method to be called from other classes.

That's really sound like Adapter. You want to have a certain class adapted to your interface. The interface here is MyInterface and the adaptee is ConcreteSth.

nanda
+1, deserved by not yet received
Bozho
A: 

Factory method?

The essence of the Factory method Pattern is to "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses."

http://en.wikipedia.org/wiki/Factory_method_pattern

Andrija
there's no creation in there.
xtofl
This is not a factory pattern.
Erick Robertson
A: 

I would call it an Adapter: it wraps another method (MyInterface.getSth) around an existing interface (i.e. the ConcreteSth), without changing the functionality.

xtofl
+1, deserved by not received yet
Bozho
+3  A: 

You are just following basic object oriented design here. This looks like simple Composition to me and if you are really keen on a design pattern I could stretch it to being a form of delegate.

willcodejavaforfood
Then maybe we shouldn't have design pattern after all. They are all after all just combination of composition, relation, etc...
nanda
Design patterns are good for inexperienced programmers to learn what kinds of things can be done in object-oriented design. It's something entirely different to retro-fit a pattern onto code that you've already created to solve a problem.
Erick Robertson
I like this answer (+1)
Pascal Thivent
guys the key is here: "Purpose of code above is to provide unified method to be called from other classes." Sound really an adapter for me...
nanda
Before you start worrying about design patterns you really need to understand the very basics of Object Oriented Design :)
willcodejavaforfood
are you assuming OP and me don't understand basics of OOD?
nanda
+5  A: 

It looks to me like an Adapter. It adapts ConcreteSth to MyInterface.

Anyway, if it does the work you expect from it, you should be asking about whether it's a pattern only out of curiousity.

Bozho
I don't think it's required to be able to tell the Adapter which Adaptee it should use.
xtofl
@bozho - What do you think about my answer? :)
willcodejavaforfood
@xtofl I think you should. Otherwise how would it know what to adapt to?
Bozho
See the example in Java section of: http://en.wikipedia.org/wiki/Adapter_pattern. It's not required to tell the adapter which adaptee it use.
nanda
in Java section. Code of LegacyLine, LegacyRectangle, Shape
nanda
@nanda @xtofl - although I don't like the example in wikipedia with instantiating the adaptee, I should agree it's a valid case for adapter. Updated the answer
Bozho
@Bozho... well not to be such jerk, but I think the proper response is to close your answer, don't you think so? :)
nanda
@nanda - em, why?
Bozho
@Bozho don't know but I think that should be the etiquette of duplicating answer that doesn't give more value. Maybe I'm wrong, though...
nanda
@nanda At the time I was writing my answer there was no other answer specifying that it is the adapter pattern. If there was, I wouldn't add mine. (yes, there is a 2 minute difference, but I was writing it at that time)
Bozho
@Bozho your original answer is Adapter (almost...) while your modified is just Adapter. And by the time you modified your answer you know there is an exact answer as your modified answer. Anyway... if you decide to keep your answer, so be it.
nanda