熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java設計模式-----Visitor訪問者模式

2022-06-13   來源: Java核心技術 

  源自

  Chain of Responsibility職責鏈模式

  為了避免請求的發送者和接收者之間的耦合關系使多個接受對象都有機會處理請求將這些對象連成一條鏈並沿著這條鏈傳遞該請求直到有一個對象處理它為止

  例子

  view plaincopy to clipboardprint?
    public class Boy {

  private boolean hasCar; // 是否有車
        private boolean hasHouse; // 是否有房
        private boolean hasResponsibility; // 是否有責任心

  public Boy() {
        }

  public Boy(boolean hasCar boolean hasHouse boolean hasResponsibility) {
            thishasCar = hasCar;
            thishasHouse = hasHouse;
            thishasResponsibility = hasResponsibility;
        }

  public boolean isHasCar() {
            return hasCar;
        }

  public void setHasCar(boolean hasCar) {
            thishasCar = hasCar;
        }

  public boolean isHasHouse() {
            return hasHouse;
        }

  public void setHasHouse(boolean hasHouse) {
            thishasHouse = hasHouse;
        }

  public boolean isHasResponsibility() {
            return hasResponsibility;
        }

  public void setHasResponsibility(boolean hasResponsibility) {
            thishasResponsibility = hasResponsibility;
        }
    }

  public interface Handler {
         public void handleRequest(Boy boy);
    }

  public class HouseHandler implements Handler {

  private Handler handler;

  public HouseHandler(Handler handler) {

  thishandler = handler;
        }

  public Handler getHandler() {
            return handler;
        }

  public void setHandler(Handler handler) {
            thishandler = handler;
        }

  public void handleRequest(Boy boy) {
            if (boyisHasHouse()) {
                Systemoutprintln(沒想到吧我還有房子);
            } else {
                Systemoutprintln(我也沒有房);
                handlerhandleRequest(boy);
            }
        }
    }

  public class CarHandler implements Handler {

  private Handler handler;

  public CarHandler(Handler handler) {
            thishandler = handler;
        }

  public Handler getHandler() {
            return handler;
        }

  public void setHandler(Handler handler) {
            thishandler = handler;
        }

  public void handleRequest(Boy boy) {
            if (boyisHasCar()) {
                Systemoutprintln(呵呵我有輛車);
            } else {
                Systemoutprintln(我沒有車);
                handlerhandleRequest(boy);
            }
        }
    }

  public class ResponsibilityHandler implements Handler {

  private Handler handler;

  public ResponsibilityHandler(Handler handler) {
            thishandler = handler;
        }

  public Handler getHandler() {
            return handler;
        }

  public void setHandler(Handler handler) {
            thishandler = handler;
        }

  public void handleRequest(Boy boy) {
            if (boyisHasResponsibility()) {
                Systemoutprintln(我只有一顆帶Responsibility的心);
            } else {
                Systemoutprintln(更沒有責任心);
                handlerhandleRequest(boy);
            }
        }
    }

  public class Girl {

  public static void main(String[] args) {

  Boy boy = new Boy(false false true);// 這個boy沒有車也沒有房不過很有責任心
            Handler handler = new CarHandler(new HouseHandler(
                    new ResponsibilityHandler(null)));// 也可以使用setHanlder方法
            handlerhandleRequest(boy);
        }
    }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26552.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.