如何在java中的多个类中使用一个接口? [英] How to use one interface in multiple class in java?

查看:259
本文介绍了如何在java中的多个类中使用一个接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个活动和2个片段。



我想在活动中点火 listener.receivePreview(obj) )然后


  1. 执行:OneFragment - > receivePreview。

  2. 执行:TwoFragment - > receivePreview。




 公共类MainAct扩展AppCompatActivity {
public interface OnReceiveListener {
//这可以是要发送给活动的任意数量的事件
void receivePreview(Object ... obj);
}
private OnReceiveListener监听器;


}

公共类OneFragment扩展Fragment实现OnReceiveListener {

@Override
public void receivePreview(Object .. .obj){

}
}

公共类TwoFragment扩展Fragment实现OnReceiveListener {

@Override
public void receivePreview(Object ... obj){

}
}


解决方案

我认为你可以使用观察者模式,这在你的情况下是一个很好的做法。


如GoF所述:



定义一对多对象之间的依赖关系,这样当一个对象改变状态时,它的所有依赖关系都会被自动通知和更新。




片段是实现观察者类和您的活动具有主题的作用,如上图所示。



我希望这可以帮助您以非常好的方式实现代码。
可以在以下链接中找到一些教程:



https://dzone.com/articles/observer-pattern-java
http://www.tutorialspoint.com/design_pattern/observer_pattern.htm



编辑:在给定情况下:

  public interface OnReceiveListener {//这是你的观察者界面! 
//这可以是要发送给活动的任意数量的事件
void receivePreview(Object ... obj);
}

使用此设计模式,片段的定义正确,因此我不会更改其代码:



公共类OneFragment扩展Fragment实现OnReceiveListener {

  @Override 
public void receivePreview(Object ... obj){

}

}

 公共类TwoFragment扩展Fragment实现OnReceiveListener {

@Override
public void receivePreview(Object ... obj){

}

you需要引用活动中的片段(作为观察者)。

  ArrayList< OnReceiveListener> observers = new ArrayList< OnReceiveListener>(); 

确实观察者可以订阅或注册自己的主题(片段持有对活动的引用(更好)使用单例模式!:D)。像这样:

 公共类MainAct扩展AppCompatActivity {
私有静态MainAct实例;

public static MainAct getInstance(){
if(instance!= null)
return instance;
}

// I认为最好在MainAct活动的onCreate()方法中创建实例变量

onCreate(...)
{



instance = this;
...
}

public void registerObserver(OnReceiveListener observer){
observers.add(observer)

}

notifyObservers(){
//在你想要的监听器中调用此方法

for(Observer obser:observers)
obser.adminPreview(param)

}
...

//在片段初始化中:
MainAct。 getInstance()。registerObserver(this)


I have one activity and 2 fragment.

I want when in activity fire listener.receivePreview(obj) then

  1. execute : OneFragment -> receivePreview .
  2. execute : TwoFragment -> receivePreview .

public class MainAct extends AppCompatActivity {
    public interface OnReceiveListener {
        // This can be any number of events to be sent to the activity
        void receivePreview(Object... obj);
    }
    private OnReceiveListener listener;


}

public class OneFragment extends Fragment implements OnReceiveListener{

    @Override
    public void receivePreview(Object... obj) {

    }
}

public class TwoFragment extends Fragment implements OnReceiveListener{

    @Override
    public void receivePreview(Object... obj) {

    }
}

解决方案

I think you can use Observer Pattern that is a good practice in you situation.

As described by GoF:

"Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically".

Read more at http://www.java2blog.com/2013/02/observer-design-pattern-in-java.html#TLio7G2ruqxvfZUR.99

In your situation you have such relation (one-to-many) and when an event occurred in the activity you want to aware that two fragment.

Fragments are implement observer class and your activity has the role of subject as illustrate in above figure.

I hope this could help you to implements your code in a very nice way. some tutorial can be find in the following links :

https://dzone.com/articles/observer-pattern-java http://www.tutorialspoint.com/design_pattern/observer_pattern.htm

Edit: in the given situation:

public interface OnReceiveListener { // this is your observer interface  !
        // This can be any number of events to be sent to the activity
        void receivePreview(Object... obj);
    }

Fragment are in correct definition with this design pattern so I do not change their code :

public class OneFragment extends Fragment implements OnReceiveListener{

@Override
public void receivePreview(Object... obj) {

}

}

public class TwoFragment extends Fragment implements OnReceiveListener{

@Override
public void receivePreview(Object... obj) {

}

you need to have references to the fragments in the activity (as observer).

ArrayList< OnReceiveListener > observers =  new ArrayList< OnReceiveListener>();

indeed an observer can subscribe or register itself to a subject ( fragment hold a reference to the activity (better to use singleton pattern ! :D). like this :

public class MainAct extends AppCompatActivity {
private static MainAct instance;

public static MainAct getInstance() {
 if(instance != null)
    return instance;
}

// I think it is better to create the instance variable in the onCreate() method of the MainAct activity

onCreate(...)
{
.
.
.
instance = this;
...
}

public void registerObserver(OnReceiveListener observer){
observers.add(observer)

}

notifyObservers(){
// call this method in the listener you want 

for( Observer obser : observers)
    obser. receivePreview(param )

}
...

//in fragment initialization: MainAct.getInstance().registerObserver(this)

这篇关于如何在java中的多个类中使用一个接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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