开发者

Is there such design pattern? How to call it?

开发者 https://www.devze.com 2023-01-17 23:32 出处:网络
I have such design: public interface MyInterface { public abstract List<Sth> getSth(); } public class MyConcreteImplementation implements MyI开发者_C百科nterface {

I have such design:

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

public class MyConcreteImplementation implements MyI开发者_C百科nterface {
    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?


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.


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.


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号