數(shù)據(jù)加載中……
          java代理模式
          package orj.jzkangta.proxydemo02;

          public class ComputerMaker implements SaleComputer {

              
          public void sale(String type) {
                  System.out.println(
          "賣出了一臺(tái)"+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(
          "我給你一些優(yōu)惠。。。");
              }

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

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

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

                  
          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("聯(lián)想");
                  
          //sc.sale("三星");
                  sc.sale("Dell");

              }


          }

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


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 武定县| 汶川县| 沙雅县| 简阳市| 女性| 美姑县| 卢湾区| 西吉县| 布尔津县| 三穗县| 安新县| 上饶县| 洛川县| 通州区| 灵石县| 扎鲁特旗| 南通市| 来安县| 湖口县| 洮南市| 闽侯县| 土默特左旗| 仁化县| 巢湖市| 双鸭山市| 吉林省| 察隅县| 翁源县| 滨海县| 贵德县| 莱西市| 萝北县| 吴旗县| 定陶县| 平罗县| 莆田市| 新津县| 遵义县| 固原市| 土默特右旗| 广州市|