学习设计模式从一个具体事情说起:
比如一个孩子在睡觉,爸爸要照看它,怎么写这样一个程序。
首先想到的是写一个Dad 类,一个Child 类,让Dad 类监听Child。
看如下代码:
package com;
class Child{
private boolean wakeup=false;
/**
* 封装:一个对象特性 尽量只用自己来修改 为什么 不能直接访问属性 。
* 属性做成private 通过方法控制 , 可以达到比较精细的控制。有的只有get 方法(如身份)
* @return
*/
public boolean isWakeup() {
return wakeup;
}
public void setWakeup(boolean wakeup) {
this.wakeup = wakeup;
}
void wakeup(){
wakeup=true;
}
}
class Dad implements Runnable{ //让Dag 不停的监测。
Child c;//一个类监听另外一个类,持有它的引用完全可以。构造方法里传递给他。
@Override
public void run() {
while(c.isWakeup()){
try {
Thread.sleep(1000);//每隔一秒的看一次醒了没有。
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public Dad(Child c){
this.c=c;
}
}
public class Test {
public static void main(String []args){
Child c=new Child();
new Thread(new Dad(c)).start();
}
}
运行结果为:
Dad 线程不停运行,因为小孩没有醒过来。
如何设置小伙子5分钟后醒过来呢? 模拟线程,让Child 也是一个类,5秒钟后醒过来。
代码如下:
package com;
class Child implements Runnable{ //模拟程序使Child本身就是一个线程。
Private boolean wakeup=false;
/**
* 封装:一个对象特性 尽量只用自己来修改 为什么 不能直接访问属性 。
* 属性做成private 通过方法控制 , 可以达到比较精细的控制。
有的只有get 方法(如身份)
* @return
*/
public boolean isWakeup() {
return wakeup;
}
public void setWakeup(boolean wakeup) {
this.wakeup = wakeup;
}
void wakeup(){
wakeup=true;
}
@Override
public void run() {
try {
Thread.sleep(5000);//睡5秒钟
} catch (InterruptedException e) {
e.printStackTrace();
}
this.wakeup(); //5秒钟后醒过来了。
}
}
class Dad implements Runnable{ //让Dag 不停的监测。
Child c;//一个类监听另外一个类,持有它的引用完全可以。构造方法里传递给他。
@Override
public void run() {
while(c.isWakeup()){
try {
Thread.sleep(1000);//每隔一秒的看一次醒了没有。
} catch (InterruptedException e) {
e.printStackTrace();
}
}
feed(c);
}
public Dad(Child c){
this.c=c;
}
private void feed(Child c){
System.out.println("feed c");
}
}
public class Test {
public static void main(String []args){
Child c=new Child();
new Thread(c).start();
new Thread(new Dad(c)).start();
//本来下午还想干点别的事。现在变成模式你不能干别的事了要一直盯着他。3小时不醒一直循环着。
}
}
上面代码问题:
1.程序里不断while 主动监听很耗资源
2.一个线程wait 怎么醒过来-notify(),谁负责线程醒过来。负责notify 的线程同样的也要监听Child 。多次一举。解决办法:
( 关键点)让Child来监听( 确定的说是通知d.WakeUp(this) )Dad。代码如下:
package com;
class Child implements Runnable{//模拟程序使Child本身就是一个线程。
Dad d;
public Child(Dad d){
this.d=d;
}
boolean wakeup=false;
/**
* 封装:一个对象特性 尽量只用自己来修改 为什么 不能直接访问属性 。
* 属性做成private 通过方法控制 , 可以达到比较精细的控制。
有的只有get 方法(如身份)
* @return
*/
public boolean isWakeup() {
return wakeup;
}
public void setWakeup(boolean wakeup) {
this.wakeup = wakeup;
}
void wakeup(){
wakeup=true;
d.feed(this);//Child 拥有了Dad引用 醒后直接通知Dad了。
}
@Override
public void run() {
try {
Thread.sleep(5000);//每隔一秒的看一次醒了没有。
} catch (InterruptedException e) {
e.printStackTrace();
}
this.wakeup();
}
}
class Dad { //让Dag 不停的监测。
public Dad(){
}
public void feed(Child c){
System.out.println("feed c");
}
}
public class Test {
public static void main(String []args){
Dad d=new Dad();
new Thread(new Child(d)).start();
}
}
这样就不需Dad 一直监听着可以干别的事。Child 醒了告诉Dad。
但还有问题:
1.
醒了包含很多信息, 几点 醒的。针对不同醒的方式处理方式不同行,包含一些具体情况。
应该把具体情况告诉他爸爸。小孩有责任把具体情况通知责任人。
如醒了时间 Java中 time 用long表示。 地点location.
private long time;
private String location;
可以把 time location 当做Child 成员,feed(C), Dad.feed(this) 就知道具体醒的时间 地点了等情况了。
且 time location 不应该是Child 的属性 (合适的属性放在合适的类)
2.但醒了还有其他情况 可以把他放在一个WakeUpEvent 类里。
class WakeupEvent{ //不能写在Child 里面。
private long time;
private String location;
private Object source;
WakeupEvent(long time,String location,Object source){
this.time=time;
this.location=location;
this.source=source;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Object getSource() {
return source;
}
public void setSource(Object source) {
this.source = source;
}
}
如果Dad 要监听,
给Child 一个Dad 引用。
class Dad { //让Dag 不停的监测。
public Dad(){
}
public void ActionWakeUP(WakeupEvent w){
System.out.println("ActionWakeUP");//可以是时间 可以是location。
}
}
在Child 类里, Dad d ; 做为Child 的成员。
Child{
Dad d;
void wakeup(){ //
WakeupEvent w=new WakeupEvent(System.currentTimeMillis(),"bed",this); //注意参数里以this为参数的函数。
}
}
/注意参数里以this为参数的函数。像下面代码是写不出来的。只能写在 Child 函数里用this 作为参数
//WakeupEvent w=new WakeupEvent(System.currentTimeMillis(),"bed",c);
//Child c=new Child(w);
好了现在Child 醒了可以通知Dad 了,那么此时 GroudFather 该干什么呢? Ancle该干什么呢?
Child {
Dad d;
GroudFather gf;
Ancle an;
}
这样每加一个就要 修改Child .扩展性不好。
由此我们想到interface 方法:
Dad GroudFather Ancle 都继承 WakeUpLisener接口(接口多继承和多态特性)。并且具体实现WakeUpListener 接口的方法 ActionWakeUp () 。
这样当Child 醒了后,通知Dad GroudFather Ancle 并且把醒了后状况通知他们,他们根据具体情况采取具体动作。
代码如下:
package com;
import java.util.ArrayList;
import java.util.List;
class Child implements Runnable{//模拟程序使Child本身就是一个线程。
WakeupEvent w;
private List <WakeUpLisener> wul=new ArrayList<WakeUpLisener>();//监听器列表,用来存储各种监听类,如Dad GroudFather Ancle.
public void addWakupListener (WakeUpLisener k){
wul.add(k);
}
public Child(){
}
boolean wakeup=false;
/**
* 封装:一个对象特性 尽量只用自己来修改 为什么 不能直接访问属性 。
* 属性做成private 通过方法控制 , 可以达到比较精细的控制。
有的只有get 方法(如身份)
* @return
*/
public boolean isWakeup() {
return wakeup;
}
public void setWakeup(boolean wakeup) {
this.wakeup = wakeup;
}
void wakeup(){ //醒过来想让他爸爸干别的事。
for(int i=0;i<wul.size();i++){
WakeUpLisener wl=wul.get(i);
wl.ActionWakeUP(new WakeupEvent(System.currentTimeMillis(),"bed",this));
//wul[i].ActionWakeUp 不行,只有 wul.get(i)才有ActionWakeUp()方法。
}
}
@Override
public void run() {
try {
Thread.sleep(1000);//每隔一秒的看一次醒了没有。
} catch (InterruptedException e) {
e.printStackTrace();
}
this.wakeup();
}
}
class Dad implements WakeUpLisener{ //让Dag 不停的监测。
public Dad(){
}
public void ActionWakeUP(WakeupEvent w){
System.out.println("Dad ActionWakeUP"+"location:"+w.getLocation()+",time:"+w.getTime());//可以是时间 可以是location。
}
}
class GroudDad implements WakeUpLisener{ //让Dag 不停的监测。
public GroudDad(){
}
public void ActionWakeUP(WakeupEvent w){
System.out.println("GroundDad ActionWakeUP-"+"location:"+w.getLocation()+",time:"+w.getTime());//可以是时间 可以是location。
}
}
interface WakeUpLisener{
public void ActionWakeUP(WakeupEvent w);
}
class WakeupEvent{ //不能写在Child 里面。
private long time;
private String location;
private Object source;
WakeupEvent(long time,String location,Object source){
this.time=time;
this.location=location;
this.source=source;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Object getSource() {
return source;
}
public void setSource(Object source) {
this.source = source;
}
}
public class Test {
public static void main(String []args){
Dad d=new Dad();
//WakeupEvent w=new WakeupEvent(System.currentTimeMillis(),"bed",c);
//Child c=new Child(w);
Child c=new Child();
c.addWakupListener(d);
new Thread(c).start();
GroudDad gd=new GroudDad();//添加一个不需修改。
c.addWakupListener(gd);
}
}
//如果要爷爷来抱怎么办 Child 这个类改变很大。//叔叔呢? Child 类不断扩展。
运行结果为:
Dad ActionWakeUPlocation:bed,time:1318647314593
GroundDad ActionWakeUP-location:bed,time:1318647314593
总结:
1.用设计模式 类增加了但维护成本降低
2.Struct MVC 也是用了Observed 设计模式。
3.添加而不是修改。加一个新Observed只需建一个类 实现特点接口实现特点方法 在main方调用。如果不想建类可以在配置文件里Properties 或者把.properties 放在classpath 路径 通过JDK 中 Properties 来读取
4. 如果有代码复制来复制去 就要考虑代码封装。
还可以继续封装
如
class CryEvent extends Event{
}
abstract class Event{
}