英文:
How to implement the update method for Observer Pattern
问题
我正在尝试将观察者模式调整到我的代码中,因此在我的情况下,MSG0100是我的主题类,它更新变量msg100PreselectionAplication,而MainActivity类是一个具体的观察者,通常你需要创建观察者类,但我的问题是MainActivity必须扩展自那个类,这是一个问题,因为Java不支持多重继承。到目前为止,这是我的代码,有人能给我一个我需要做什么的想法吗?请看下面的代码:
MSG0100类:
public class MSG0100 implements PreselectionAplicationUseCases {
private OnMsg100PreselectionChanged listener = null;
public void setOnMsgPreselectionChanged(OnMsg100PreselectionChanged listener) {
this.listener = listener;
}
public void setMsg100PreselectionAplication(boolean msg100PreselectionAplication) {
if (listener != null) {
listener.onPreselectionChanged(msg100PreselectionAplication);
}
}
}
interface OnMsg100PreselectionChanged {
void onPreselectionChanged(boolean isChanged);
}
MainActivity类:
public class MainActivity extends AppCompatActivity {
private ExpandableListViewAdapter mExpandableListViewAdapter;
PreselectionAplicationUseCases preselectionAplicationUseCases;
public void Preselection() {
preselectionApplicationUseCases.setOnMsgPreselectionChanged(new OnMsg100PreselectionChanged {
@Override
void onPreselectionChanged(boolean isChanged) {
// 对变更的布尔值执行一些操作
}
});
}
}
希望这可以帮助你。
英文:
I'm trying to adjust the observer pattern to my code, so in my case MSG0100 it's my Subject class that it updates the variable msg100PreselectionAplication, and the MainActivity class it's an concrete observer, and usually you have to create the Observer class, but my problem is that the MainActivity has to extend from that class and that's a problem because Java does not support multiple inheritance. Here is what i have until now, can some one give me an idea of what i need next? Please
MSG0100 class:
public class MSG0100 implements PreselectionAplicationUseCases {
private OnMsg100PreselectionChanged listener = null;
public void setOnMsgPreselectionChanged(OnMsg100PreselectionChanged listener) {
this.listener = listener;
}
public void setMsg100PreselectionAplication(boolean msg100PreselectionAplication) {
if(listener != null) {
listener.onPreselectionChanged(msg100PreselectionAplication);
}
}
}
interface OnMsg100PreselectionChanged {
void onPreselectionChanged(boolean isChanged);
}
Main activity class:
public class MainActivity extends AppCompatActivity {
private ExpandableListViewAdapter mExpandableListViewAdapter;
PreselectionAplicationUseCases preselectionAplicationUseCases;
public void Preselection(){
preselectionApplicationUseCases.setOnMsgPreselectionChanged(new OnMsg100PreselectionChanged {
@Override
void onPreselectionChanged(boolean isChanged) {
//do something with changed boolean
}
});
}
}
答案1
得分: 1
你应该将Observer
创建为一个接口,由MainActivity
实现,代码如下:
interface Observer {
void update(Data data);
}
public class MainActivity extends AppCompatActivity implements Observer {
//…
@Override
public void update(Data data) {
// 在这里将数据更新到您的 UI
}
//…
}
然后修改您的主题以接收观察者:
interface Subject {
void registerObserver(Observer o);
void removeObserver(Observer o);
void notifyObservers();
}
public class MSG0100 implements PreselectionAplicationUseCases, Subject {
private Data data = new Data();
private Observer o;
@Override
public void registerObserver(Observer o) {
this.o = o;
}
@Override
public void removeObserver(Observer o) {
o = null;
}
@Override
public void notifyObservers() {
o.update(data);
}
// ... 其他可以更新数据类的方法
}
这就是观察者模式,但从我看到的方式,您似乎只想在 Activity 中实现 OnMsg100PreselectionChanged
,代码如下:
public class MainActivity extends AppCompatActivity implements OnMsg100PreselectionChanged {
void someMethod() {
MSG0100 msg0100 = new MSG0100();
msg0100.setOnMsgPreselectionChanged(this);
}
@Override
void onPreselectionChanged(boolean isChanged) {
// 在这里处理预选项是否已更改
}
}
英文:
You should create Observer
as an Interface which implemented by MainActivity
like this:
interface Observer {
void update(Data data)
}
public class MainActivity extends AppCompatActivity implements Observer {
//…
@Override
public void update(Data data) {
// update your data to your UI here
}
//…
}
and modify your subject to receive observer
interface Subject {
void registerObserver(Observer o);
void removeObserver(Observer o);
void notifyObservers();
}
public class MSG0100 implements PreselectionAplicationUseCases, Subject {
private Data data = new Data();
private Observer o;
@Override
public void registerObserver(Observer o) {
this.o = o;
}
@Override
public void removeObserver(Observer o) {
o = null;
}
@Override
public void notifyObservers() {
o.update(data);
}
// ... other method that can update the data class
}
that is an Observer Pattern, but the way I see, you simply want to implement OnMsg100PreselectionChanged on Activity like:
public class MainActivity extends AppCompatActivity, OnMsg100PreselectionChanged {
void someMethod() {
MSG0100 msg0100 = new MSG0100();
msg0100.setOnMsgPreselectionChanged(this);
}
@Override
void onPreselectionChanged(boolean isChanged) {
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论