Java接口,创建者模式和“getInstance()”方法或等同物 [英] Java interfaces, the creator patterns and the "getInstance()" method or equivalent

查看:208
本文介绍了Java接口,创建者模式和“getInstance()”方法或等同物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想首先提到我的问题起因于Java中的接口不允许静态方法。已经有人讨论过这个问题的原因(这里)。所以不要再这么说了。我正在寻找一种我的界面来创建一个自己的实例(而不是它的实现)并返回的方式。尽管使用Singleton模式,Factory和AbstractFactory模式,我仍然无法实现我的目标。

I would like to start off by mentioning that my problem stems from the fact that interfaces in Java do not allow static methods. There have been discussions about the reason for this on SO (here , for example). So lets not dwell on that. I am looking for a way for my interface to create an instance of itself (rather, its implementation) and return that. In spite of playing around with Singleton pattern, Factory and AbstractFactory patterns, I still cannot achieve my goal.

详细说明我正在尝试的内容 - 这是我的界面: / p>

To elaborate on what I'm trying - here's my interface:

public interface NoteDataStore {
    public boolean deleteNote(long noteId);
    public boolean addNote(Note note);
    public List<Note> listNotes();
    public boolean editNote(long noteId, Note note);
    public List<Note> search(String searchString);
}

这里是我的业务逻辑层:

And here's my business logic layer:

public class BusinessLogicLayer {

    public BusinessLogicLayer(){
        /*
         * GOAL: To get an instance of NoteDataStore here,
         *  without being aware of the implementation class.
         */
    }

}

尝试使用像这样的工厂模式:

I tried to use a factory pattern like so:

public interface NoteDataStoreFactory {
    public NoteDataStore getDataStoreInstance();
}

public class NoteDataStoreFactoryImpl implements NoteDataStoreFactory{
    public NoteDataStore getDataStoreInstance(){
        return NoteDataStoreImpl.getInstance();
        /*
         * Here, NoteDataStoreImpl is an implementation of NoteDataStore.
         * This should be transparent to my business logic layer.
         */
    }
}

然而,这仍然需要业务逻辑层知道实现类 NoteDataStoreFactoryImpl 因此:

However, this still requires the Business Logic layer to know the implementation class NoteDataStoreFactoryImpl thus:

NoteDataStore = new NoteDataStoreFactoryImpl().getDataStoreInstance();

我该如何解决?如何将我的BusinessLogicLayer保留在使用确切实现类的黑暗中?

How do I get around this? How do I keep my BusinessLogicLayer in the dark regarding the exact implementation class to use?

一些答案​​建议使用像Spring这样的框架。唉,我不能这样做,因为这个应用程序针对各种移动平台(Android,Blackberry,JavaME)。我应该在原来的问题上明确表示道歉,不要这样做。

A few of the answers suggest the use of frameworks like Spring. Alas, I cannot do so because this application targets various mobile platforms (Android, Blackberry, JavaME). I should have made this clear in my original question - apologies for not doing so.

我的主要目的是跨平台安装一个应用程序。 UI,数据库访问,HTTP传输层等必须针对每个平台进行专门编码。然而,业务逻辑足够简单,以保证所有平台的共同层。我打算将业务逻辑层分发为JAR库。所以也是解析和框架层(用于JSON / XML)。

My main intention is to have an app across platforms. The UI, database access, HTTP transport layers etc will have to be coded specifically for each platform. However, the business logic is simple enough to warrant a common layer across all platforms. I intend to distribute the business logic layer as a JAR library. So also, the parsing and framing layer (for JSON/XML).

在SO上已经有了一个讨论(关于我是否应该下去这个路径) - 逻辑代码重用。然而,假设这是可以的,我继续分层的方法和意图在代码中有一层通用。现在,我的情况是这样的,我有:

There has already been a discussion about this at SO (on whether I should even be going down this path) - Logic Code reuse. However, assuming this is OK and that I proceed with the layered approach and the intention to have one layer common in code. Now, my situation is such that I have:


  1. 一个常见的业务逻辑层。

  2. 平台特定数据层(由 NoteDataStore 接口表示)

  3. 平台特定的应用核心层(Controller,如果我可以称之为)

  1. A common Business Logic Layer.
  2. Platform-specific data layer (represented by the NoteDataStore interface)
  3. Platform-specific Application core layer (Controller, if I may call it so).

请注意,如果我使用Factory模式或其他类型,我可以负担得起每个平台具体的层。所以,工厂方法/类本身可以知道 NoteDataStore 实现类。但是,业务逻辑层必须不知道任何实现类。

Note that if I use the Factory pattern or other such, I can afford to have that layer specific to each platform. So, the factory method/class itself can know about the NoteDataStore implementation class. However, the Business Logic Layer must be unaware of any of the implementation classes.

各层的典型用法如下:

public class NoteDataStoreAndroid implements NoteDataStore{

    public boolean deleteNote(long noteId){
        /*
         * Android-specific database code
         */
    }

    /* ... similarly, other methods */
}


public class AndroidCoreApp{

    public void doBusinessLogic(){
        BusinessLogicLayer businessLogic = new BusinessLogicLayer();
        businessLogic.startBusinessLogic();
    }
}

有关如何处理这种情况的任何输入? p>

Any inputs on how to handle this scenario?

推荐答案

我终于找到了@Ray Tayek在原始问题的评论中提供的建议。在创建 BusinessLogicLayer 时,我只需传递一个 NoteDataStore 的实例。

I finally went for the suggestion provided by @Ray Tayek in a comment to the original question. I simply pass in an instance of the NoteDataStore at the time of creating the BusinessLogicLayer.

这个简单的解决方案非常适合我的需要,因为我不需要工厂。我的主要目标是BL层不知道它使用的接口的确切实现类。现在,而不是工厂,它是核心的控制器层,创建了一个具体的接口实现,并将它们提供给BL层。这是完美的!

This simple solution suits my needs pretty well since I do not really require a Factory. My main objective was for the BL Layer to be unaware of the exact implementation classes of the interfaces it uses. Now, instead of a Factory, it is the core "Controller" layer that creates a concrete implementation of the interfaces and supplies them to the BL Layer. This is just perfect!

这是代码片段。

public class NoteDataStoreAndroid implements NoteDataStore{

    public boolean deleteNote(long noteId){
        /*
         * Android-specific database code
         */
    }

    /* ... similarly, other methods */
}


public class AndroidCoreApp{

    public void doBusinessLogic(){
        BusinessLogicLayer businessLogic = new BusinessLogicLayer(new NoteDataStoreAndroid());
        businessLogic.startBusinessLogic();
    }
}

public class BusinessLogicLayer {

    private NoteDataStore mDataStore;
    public BusinessLogicLayer(NoteDataStore dataStore){
        this.mDataStore = dataStore;

        //Do something useful with mDataStore
    }

    public void startBusinessLogic(){
        //Do something useful with mDataStore
    }

}

这篇关于Java接口,创建者模式和“getInstance()”方法或等同物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆