问题描述
我有
public class mammal implements Imammle
{
public String getName() {
return "Mammal";
}
public String speak() {
return getName() + "Maaaauuu";
}
}
我需要知道我的动物发出声音,以便我可以像吃进食一样实时采取相应的行动。
我应该使用什么样的设计模式?
谢谢。
I need to know when my animal makes a sound so that I can take corresponding actions in real time like eat feed.
What kind of design pattern should I use?Thanks.
推荐答案
设计模式可能用于解决您的问题。在观察者模式中,有两种不同类型的对象:
The Observer design pattern could possibly be used to solve your problem. In the Observer Pattern, there are two distinct types of objects:
- 观察者对象是要成为
- 可观察对象是观察者可以观看的对象
- Observer objects are objects that want to be notified whenever Observable objects change.
- Observable objects are objects that can be watched by Observers
观察者注册了他们希望观看的可观察对象。每当可观察对象发生变化时,它会通知已经注册的所有观察者。
Observers are registered with the Observable objects they wish to watch. Whenever an Observable object changes, it notifies all of the Observers that have been registered with it.
基于你的代码示例我将假设你正在使用Java。在这种情况下,您可以使用和以减少数量
Based on your code example I am going to assume you are using Java. In that case you can use Observer and Observable to reduce the number of changes you have to make to your existing code.
public class mammal extends java.util.Observable implements Imammle
{
public String getName() {
return "Mammal";
}
public String speak() {
setChanged(); // We have been changed
notifyObservers(); // Notify everyone that we have changed
return getName() + "Maaaauuu";
}
}
现在我们创建一个希望的类当有变化时,要注意:
Now we create a class that wishes to watch for when something changes:
public class Watcher implements java.util.Observer
{
public void update(Observable o, Object arg) {
System.out.println("Something has changed");
}
}
最后,我们注册了一个哺乳动物
,以便随时更改通知。
Finally, we register our watcher(s) with a mammal
so that they can be informed whenever it changes.
public class Main
{
public static void main(String args[]) {
mammal cow = new mammal();
Watcher farmer = new Watcher();
cow.addObserver(farmer);
cow.speak();
}
}
这种方法有很多方法:
- 观察者可以注册任何Observable对象。您可能希望您的观察者只能观察
哺乳动物
s。另一方面,您可能希望只有某些类型的观察者能够观察到哺乳动物
。 - 真的没有'没有任何方式让你的观察者知道什么确切地改变了
哺乳动物
没有
-
哺乳动物
使用方法 - 或者Observer检查在
哺乳动物
-
- Observers can be registered with any Observable object. You may wish for your Observer to only be able to observe
mammal
s. On the flip side, you may wish for only certain kinds of Observers to be able to observe amammal
. - There really isn't any way for your Observer to know what exactly changed with the
mammal
without- The
mammal
passing in some kind of message to the Observer using the notifyObservers(Object arg) method - OR the Observer checking a public flag set in the
mammal
- The
这些抽奖对你来说是一个问题,那么你可能希望避免观察者和观察者,并考虑修改现有的类(哺乳动物
以及您的观察者)自己的,应用程序的具体方法。
If either of these draw-backs are a problem for you, then you may want to avoid Observer and Observable and look into modifying your existing classes (mammal
and whatever your Observers are) to implement their own, application specific methods.
这篇关于观察?设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!