简体   繁体   English

有这样的设计模式吗? 怎么称呼它?

[英]Is there such design pattern? How to call it?

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? 如果是这样,如何命名?

It looks to me like an Adapter . 在我看来,它就像一个适配器 It adapts ConcreteSth to MyInterface . 它使ConcreteSth适应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. 听起来确实像是Adapter。 You want to have a certain class adapted to your interface. 您想让某个类适合您的界面。 The interface here is MyInterface and the adaptee is ConcreteSth . 这里的接口是MyInterface ,而适应者是ConcreteSth

我将其称为适配器:它在现有接口(即ConcreteSth )周围包装了另一个方法( MyInterface.getSth ),而没有更改功能。

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." Factory方法模式的本质是“定义用于创建对象的接口,但是让子类决定实例化哪个类。Factory方法使类将实例化推迟到子类。”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM