设计模式之State——交通篇(原创)

类别:Java 点击:0 评论:0 推荐:

设计模式之State——交通篇

 

“小朋友过马路,左右看,红灯停,绿灯行,阿姨夸我是乖宝宝。”,我给女儿念着儿歌,突然女儿问我,什么是红绿灯啊?为了给她说清楚,我特意带她看我们家附近的交通灯(NearMyFamilyTrafficLight)的运行。我们都知道,交通灯有三种状态:红灯(RedLight)、黄灯(YellowLight)和绿灯(GreenLight)。交通灯状态的改变是由控制中心(ControlCenter)来控制的。

 

下面让我们来看看这个过程是如何实现的。

1、  在这里,先定义交通灯(TrafficLight)接口类:

public interface TrafficLight {

  public void showRedLight();  //显示红灯

  public void showGreenLight();//显示绿灯

  public void showYellowLight(); //显示黄灯

}

2、我们家附近的交通灯(NearMyFamilyTrafficLight)是对交通灯(TrafficLight)接口的具体实现:

public class NearMyFamilyTrafficLight implements TrafficLight{

  public void showRedLight(){

    System.out.println("红灯亮了,不能通过!");

  }

  public void showGreenLight(){

    System.out.println("绿灯亮了,可以通过!");

  }

  public void showYellowLight(){

    System.out.println("黄灯亮了!");

  }

  //控制中心控制我们家附近的那盏交通灯

  protected void changeState(ControlCenter controlCenter, NearMyFamilyTrafficLight trafficLight) {

      controlCenter.changeState(trafficLight);

  }

}

3、定义控制中心(ControlCenter)类:

public class ControlCenter {

  private NearMyFamilyTrafficLight trafficLight; //我们家附近的交通灯

  public void changeState(NearMyFamilyTrafficLight trafficLight) {

      this.trafficLight = trafficLight;

  }

  public void showRedLight() { //显示红灯

      trafficLight.showRedLight();

  }

  public void showGreenLight() { //显示绿灯

      trafficLight.showGreenLight();

  }

  public void showYellowLight() { //显示黄灯

      trafficLight.showYellowLight();

  }

}

4、我们家附近的交通灯(NearMyFamilyTrafficLight)实际上有红、黄、绿三盏灯组成:

A:红灯(RedLight)类:

public class RedLight extends NearMyFamilyTrafficLight{

  public static boolean  existRedLight = false;

  public static RedLight getRedLight() {  //获得红灯

      if(existRedLight==false) {

          existRedLight = true;

          return new RedLight();

      }

      return null;

  }

}

B:绿灯(GreenLight)类:

public class GreenLight extends NearMyFamilyTrafficLight{

  public static boolean  existGreenLight = false;

  public static GreenLight getGreenLight() { //获得绿灯

      if(existGreenLight==false) {

          existGreenLight = true;

          return new GreenLight();

      }

      return null;

  }

}

C:黄灯(YellowLight)类:

public class YellowLight extends NearMyFamilyTrafficLight{

  public static boolean  existYellowLight = false;

  public static YellowLight getYellowLight() { //获得黄灯

      if(existYellowLight==false) {

          existYellowLight = true;

          return new YellowLight();

      }

      return null;

  }

}

5、编写测试类:

public class Test {

  public static void  main(String args[]){

    ControlCenter controlCenter = new ControlCenter();   //控制中心

    NearMyFamilyTrafficLight redLight = new RedLight();  //红灯

    NearMyFamilyTrafficLight greenLight = new GreenLight(); //绿灯

    NearMyFamilyTrafficLight yellowLight = new YellowLight(); //黄灯

    controlCenter.changeState(redLight); //改变成红灯状态

    controlCenter.showRedLight();  //显示红灯

    controlCenter.changeState(yellowLight); //改变成黄灯状态

    controlCenter.showYellowLight();  //显示黄灯

    controlCenter.changeState(greenLight);  //改变成绿灯状态

    controlCenter.showGreenLight(); //显示绿灯

  }

}

6、说明:

A:State的定义: 不同的状态,不同的行为;或者说,每个状态有着相应的行为。

B:我们可以看到,灯状态的改变是有控制中心来控制,通过显示不同的灯,实现了交通的正常运转。

C:因此当有状态切换这种事情要处理时,我们就可以用State这种模式了。

 

 

本文地址:http://com.8s8s.com/it/it14637.htm