当前位置:首页 > 设计模式 > 正文内容

代理模式

canca19年前 (2007-03-26)设计模式464

//代理模式--真实例子
//CopyRight(C)CAnca Software Office.2006
//Created by CAnca.

 
public class ProxyMode_RealWorld{
    public static void main(String[] args)
    {
      // Create math proxy 
      MathProxy p = new MathProxy();
 
      // Do the math 
      System.out.println("4 + 2 = " + p.Add(4, 2));
      System.out.println("4 - 2 = " + p.Sub(4, 2));
      System.out.println("4 * 2 = " + p.Mul(4, 2));
      System.out.println("4 / 2 = " + p.Div(4, 2));
    }
  }
 
  // "Subject" 
 
  interface IMath
  {
    double Add(double x, double y);
    double Sub(double x, double y);
    double Mul(double x, double y);
    double Div(double x, double y);
  }
 
  // "RealSubject" 
 
  class Math implements IMath
  {
    public double Add(double x, double y){return x + y;}
    public double Sub(double x, double y){return x - y;}
    public double Mul(double x, double y){return x * y;}
    public double Div(double x, double y){return x / y;}
  }
 
  // "Proxy Object" 
 
  class MathProxy implements IMath
  {
    Math math;
     public MathProxy()
    {
      math = new Math();
    }
 
    public double Add(double x, double y)
    { 
      return math.Add(x,y); 
    }
    public double Sub(double x, double y)
    { 
      return math.Sub(x,y); 
    }
    public double Mul(double x, double y)
    { 
      return math.Mul(x,y); 
    }
    public double Div(double x, double y)
    { 
      return math.Div(x,y); 
    }
  }

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/664.html

标签: 设计模式
分享给朋友:
返回列表

上一篇:原型模式

下一篇:桥接模式

“代理模式” 的相关文章

抽象工厂模式

//抽象工厂模式//Copyright CAnca Software office//Created by CAnca.2006public class AbstractFactory{ public static void main(String[] args){  ...

单例模式

//CopyRight CAnca Software Office. 2006//Created by CAnca. import java.util.*; public class singletonMode{ public static void main(String[] args)...

工厂方法模式

//工厂方法模式:现实例子//CopyRight(C)CAnca Software Office. 2006//Created by CAnca. import java.util.*; public class Real_World_Example{ public static void...

原型模式

//原型模式//CopyRight(C) CAnca Software Office.2006//Created by CAnca. public class PrototypeMode{ public static void main(String[] args){  ...

适配器模式

//适配器模式//CopyRight(C)CAnca Software Office.//Created by CAnca. public class AdapterMode{ public static void main(String[] args){  Adapt...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。