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 郭興華 閱讀(728) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 邢台市| 平罗县| 昆山市| 柞水县| 永靖县| 江都市| 商洛市| 仙居县| 道孚县| 繁峙县| 平乡县| 准格尔旗| 五莲县| 谷城县| 南溪县| 易门县| 深水埗区| 来凤县| 梁山县| 铜山县| 军事| 加查县| 安福县| 体育| 嵩明县| 建德市| 娱乐| 陆河县| 静乐县| 长顺县| 扎鲁特旗| 旺苍县| 太保市| 灵璧县| 个旧市| 宽城| 河间市| 诏安县| 茶陵县| 宜章县| 库尔勒市|