java技術博客

          jsp博客
          數據加載中……
          java代理模式
          package orj.jzkangta.proxydemo02;

          public class ComputerMaker implements SaleComputer {

              
          public void sale(String type) {
                  System.out.println(
          "賣出了一臺"+type+"電腦");

              }


          }



          package orj.jzkangta.proxydemo02;

          import java.lang.reflect.Proxy;

          public class ComputerProxy {
              
          public static SaleComputer getComputerMaker(){
                  ProxyFunction pf
          =new ProxyFunction();
                  
          return (SaleComputer)Proxy.newProxyInstance(ComputerMaker.class.getClassLoader(), ComputerMaker.class.getInterfaces(), pf);
              }

          }





          package orj.jzkangta.proxydemo02;

          import java.lang.reflect.InvocationHandler;
          import java.lang.reflect.Method;

          public class ProxyFunction implements InvocationHandler {
              
          private ComputerMaker cm;
              
              
          public void youHui(){
                  System.out.println(
          "我給你一些優惠。。。");
              }

              
              
          public void giveMouse(){
                  System.out.println(
          "我還要送你一個鼠標。。。 ");
              }

              
          public Object invoke(Object arg0, Method arg1, Object[] arg2)
                      
          throws Throwable {
                  String type
          =(String)arg2[0];
                  
          if(type.equals("聯想")||type.equals("三星")){
                      
          if(cm==null){
                          cm
          =new ComputerMaker();
                          youHui();
                          giveMouse();
                          arg1.invoke(cm, type);
                      }

                  }
          else{
                      System.out.println(
          "我沒有你要的這個牌子的電腦。。。。");
                  }

                  
          return null;
              }


          }

          package orj.jzkangta.proxydemo02;

          public interface SaleComputer {
              
          public void sale(String type);
          }


          package orj.jzkangta.proxydemo02;

          public class Test {

              
              
          public static void main(String[] args) {
                  SaleComputer sc
          =ComputerProxy.getComputerMaker();
                  
          //sc.sale("聯想");
                  
          //sc.sale("三星");
                  sc.sale("Dell");

              }


          }

          posted on 2008-10-31 07:49 郭興華 閱讀(727) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 汝南县| 峨眉山市| 新建县| 泰州市| 绥阳县| 宁乡县| 泗洪县| 阿图什市| 东丽区| 延庆县| 滦平县| 临澧县| 长子县| 桓仁| 荥阳市| 榆中县| 山阴县| 长春市| 阆中市| 长武县| 苍溪县| 延长县| 德钦县| 西安市| 台州市| 沂南县| 丽江市| 务川| 亚东县| 高台县| 都匀市| 临桂县| 怀化市| 石狮市| 沿河| 巴中市| 肇州县| 西藏| 湖州市| 双牌县| 昂仁县|