Nice TWiki > Doc > CodeExamples > VisitorPatternMultiMethodExample > VisitorPatternExample (r1.5) TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Doc . { Changes | Index | Search | Go }
/* Please try the NoviceExamples before the MultiMethodExamples
To compile:
   nicec --sourcepath .. -a visitor.jar visitor
To run:
   java -jar visitor.jar
*/


// Visitor Pattern: Types
//-----------------------

abstract class Shape {
   <T> T process(IShapeVisitor<T> v);
}

class Square extends Shape { 
   double side; 

   process(IShapeVisitor v) = v.forSquare(this);
}

class Circle extends Shape { 
   double radius; 

   process(IShapeVisitor v) = v.forCircle(this);
}

class Translated extends Shape { 
   Point d; 
   Shape shape; 

   process(IShapeVisitor v) = v.forTranslated(this);
}


class Point { double x; double y; }



// Visitor Pattern: Operations
//----------------------------

interface IShapeVisitor<T> {
   T forSquare(Square s);   
   T forCircle(Circle s);   
   T forTranslated(Translated s);     
}


<T| boolean <: T <: boolean>
class ContainsPointVisitor<T> implements IShapeVisitor<T> {
   Point point; 
   
   forSquare(Square s){     
      let d = s.side/2;
      return 
         (point.x >= -d && point.x < d) && 
         (point.y >= -d && point.y < d);
   }


   forCircle(Circle s) =
      // just test the bounding box     
      (point.x >= -s.radius && point.x < s.radius) && 
      (point.y >= -s.radius && point.y < s.radius);
   

   forTranslated(Translated s){ 
      let p' = 
         new Point(
            x: point.x - s.d.x,
            y: point.y - s.d.y );
 
      return s.shape.process( 
         new ContainsPointVisitor(point: p'));
   }
}



/*
   =================================================
   Can we add a type and operation without modifying 
   the original code?
   ================================================= 
*/


// Visitor Pattern: Adding Type Variants
//--------------------------------------

interface IUnionVisitor<T> extends IShapeVisitor<T> {
   T forUnion(Union s);  
}


class Union extends Shape {
   Shape shape1; 
   Shape shape2; 

   process(IShapeVisitor v) = cast(v).forUnion(this);
}


<T| boolean <: T <: boolean>
class ContainsPointUnionVisitor<T> 
extends ContainsPointVisitor<T> implements IUnionVisitor<T> {

   forUnion(Union s) = 
      s.shape1.process(this) || s.shape2.process(this);
}


// Visitor Pattern: Adding Operations
//-----------------------------------

<T| Shape <: T <: Shape>
class ShrinkVisitor<T> implements IShapeVisitor<T> {
   double toPercent;

   forSquare(Square s) = 
      new Square(side: (s.side*toPercent)/100);

   forCircle(Circle s) = 
      new Circle(radius: (s.radius*toPercent)/100); 
  
   forTranslated(Translated s) = 
      new Translated(d: s.d, shape: s.shape.process(this));        
}


// Visitor Pattern: Testing
//-------------------------

void main(String[] args){
   let s = new Square(side: 4);
   let c = new Circle(radius: 2);

   let t = 
      new Translated(
         d: new Point(x: 1.5, y: 0),  
         shape: c 
         );

   let p = new Point(x: 3, y: 0);
   let v = new ContainsPointVisitor(point: p);

   println("square contains point " + s.process(v));
   println("circle contains point " + c.process(v));
   println("translated contains point " + t.process(v));

   let vshrink = new ShrinkVisitor(toPercent: 50);
   let t' = t.process(vshrink); 
   println("shrunk translated contains point " + t'.process(v));
                            
   let u = new Union(shape1: s, shape2: t);
   println("union contains point " +   u.process(v) );  // FAILS AT RUNTIME
}


/* 

/* Notes 
See the detailed discussion in
"Synthesizing Object-Oriented and Functional Design to Promote Re-use"
Section 3.1 figures 7, 8, 9, 10, 11
http://citeseer.nj.nec.com/krishnamurthi98synthesizing.html


square contains point false
circle contains point false
translated contains point true
shrunk translated contains point false
Exception in thread "main" nice.tools.code.EnsureTypeProc has wrong type
*/
[[http://tianjinhuoyun.shop263.com 天津货运]]
[[http://tianjinwuliu.shop263.com 天津物流]]
[[http://buxiugang.shop263.com 不锈钢]]
[[http://dayinji.shop263.com 打印机]]
[[http://gangjiegou.shop263.com 钢结构]]
[[http://jisuanji.shop263.com 计算机]]
[[http://jiameng.shop263.com 加盟]]
[[http://jianzhucailiao.shop263.com 建筑材料]]
[[http://lvxingshe.shop263.com 旅行社]]
[[http://qiyeguanli.shop263.com 企业管理]]
[[http://reliziyuan.shop263.com 人力资源]]
[[http://wangyesheji.shop263.com 网页设计]]
[[http://shichangyingxiao.shop263.com 市场营销]]
[[http://shineisheji.shop263.com 室内设计]]
[[http://shouji.shop263.com 手机]]
[[http://siliao.shop263.com 饲料]]
[[http://tuliao.shop263.com 涂料]]
[[http://weixingdianshi.shop263.com 卫星电视]]
[[http://wuliu.shop263.com 物流]]
[[http://yuanchengjiaoyu.shop263.com 远程教育]]
[[http://zhanlan.shop263.com 展览]]
[[http://baozhuangjixie.shop263.com 包装机械]]
[[http://baozhuangsheji.shop263.com 包装设计]]
[[http://boligang.shop263.com 玻璃钢]]
[[http://cizhuan.shop263.com 瓷砖]]
[[http://diban.shop263.com 地板]]
[[http://dianchi.shop263.com 电池]]
[[http://diandonggongju.shop263.com 电动工具]]
[[http://dianhua.shop263.com 电话]]
[[http://dianlian.shop263.com 电缆]]
[[http://dinaqi.shop263.com 电器]]
[[http://dianxiandianlan.shop263.com 电线电缆]]
[[http://dianyue.shop263.com 电源]]
[[http://diaoshu.shop263.com 雕塑]]
[[http://erji.shop263.com 耳机]]
[[http://ershoudiannao.shop263.com 二手电脑]]
[[http://ershoufang.shop263.com 二手房]]
[[http://fadianji.shop263.com 发电机]]
[[http://fangdaomen.shop263.com 防盗门]]
[[http://fangzhi.shop263.com 纺织]]
[[http://jipiao.shop263.com 机票]]
[[http://fuyinji.shop263.com 复印机]]
[[http://gangtie.shop263.com 钢铁]]
[[http://gongchengjixie.shop263.com 工程机械]]
[[http://guanggaosheji.shop263.com 广告设计]]
[[http://jidianqi.shop263.com 继电器]]
[[http://jiadian.shop263.com 家电]]
[[http://jianzhusheji.shop263.com 建筑设计]]
[[http://jiaohuanji.shop263.com 交换机]]
[[http://jieju.shop263.com 洁具]]
[[http://kaiguan.shop263.com 开关]]
[[http://yueqi.shop263.com 乐器]]
[[http://mudiban.shop263.com 木地板]]
[[http://pige.shop263.com 皮革]]
[[http://pijiu.shop263.com 啤酒]]
[[http://runhuayou.shop263.com 润滑油]]
[[http://shexiangji.shop263.com 摄像机]]
[[http://shexiangtou.shop263.com 摄像头]]
[[http://shumashexiangji.shop263.com 数码摄像机]]
[[http://suliaojixie.shop263.com 塑料机械]]
[[http://tiyuyongpin.shop263.com 体育用品]]
[[http://wangluodianhua.shop263.com 网络电话]]
[[http://wangyuanjing.shop263.com 望远镜]]
[[http://wenju.shop263.com 文具]]
[[http://wujingongju.shop263.com 五金工具]]
[[http://xianshiqi.shop263.com 显示器]]
[[http://xiangji.shop263.com 相机]]
[[http://xiangshui.shop263.com 香水]]
[[http://xudianchi.shop263.com 蓄电池]]
[[http://yibiao.shop263.com 仪表]]
[[http://zhongyangkongtiao.shop263.com 中央空调]]
[[http://zhubao.shop263.com 珠宝]]
[[http://pingmiansheji.shop263.com 平面设计]]
[[http://chuanganqi.shop263.com 传感器]]
[[http://motuoche.shop263.com 摩托车]]
[[http://boli.shop263.com 玻璃]]
[[http://dianji.shop263.com 电机]]
[[http://gongyipin.shop263.com 工艺品]]
[[http://huagong.shop263.com 化工]]
[[http://huazhuangpin.shop263.com 化妆品]]
[[http://jixie.shop263.com 机械]]
[[http://kongtiao.shop263.com 空调]]
[[http://shizhuang.shop263.com 时装]]
[[http://shipin.shop263.com 食品]]
[[http://suliao.shop263.com 塑料]]
[[http://taoci.shop263.com 陶瓷]]
[[http://wanju.shop263.com 玩具]]
[[http://wujin.shop263.com 五金]]
[[http://yinshua.shop263.com 印刷]]
[[http://zhoucheng.shop263.com 轴承]]
[[http://gangcai.shop263.com 钢材]]
[[http://qichepeijian.shop263.com 汽车配件]]
[[http://xiangjiao.shop263.com 橡胶]]
[[http://bianyaqi.shop263.com 变压器]]
[[http://dingfang.shop263.com 订房]]
[[http://dingpiao.shop263.com 订票]]
[[http://shuichuli.shop263.com 水处理]]
[[http://xianshiping.shop263.com 显示屏]]
[[http://lipin.shop263.com 礼品]]
[[http://lvyou.shop263.com 旅游]]
[[http://qiche.shop263.com 汽车]]
[[http://fuzhuang.shop263.com 服装]]
[[http://dianying.shop263.com 电影]]
[[http://shuomaxiangji.shop263.com 数码相机]]
[[http://wenyaqi.shop263.com 稳压器]]
[[http://zhaopin.shop263.com 招聘]]
[[http://chumoping.shop263.com 触摸屏]]
[[http://jigui.shop263.com 机柜]]
[[http://jiudianyuding.shop263.com 酒店预定]]
[[http://moban.shop263.com 模具]]
[[http://zaixiandianying.shop263.com 在线电影]]
[[http://bijiben.shop263.com 笔记本]]
[[http://xunizhuji.shop263.com 虚拟主机]]
[[http://guanlizixun.shop263.com 管理咨询]]
[[http://yumingzhuce.shop263.com 域名注册]]
[[http://chengrendianying.shop263.com 成人电影]]
[[http://shenbing.shop263.com 肾病]]
[[http://mianfeidianying.shop263.com 免费电影]]
[[http://zhongliu.shop263.com 肿瘤]]
[[http://xianweijing.shop263.com 显微镜]]
[[http://hunjie.shop263.com 婚介]]
[[http://gupiao.shop263.com 股票]]
[[http://jiaohuaji.shop263.com 交换机]]
[[http://tangniaobing.shop263.com 糖尿病]]
[[http://qiyeyouxiang.shop263.com 企业邮箱]]
[[http://yigan.shop263.com 乙肝]]
[[http://duijiangji.shop263.com 对讲机]]
[[http://guanzhuangpin.shop263.com 化妆品]]
[[http://gugutouhuaisi.shop263.com 股骨头坏死]]
[[http://dianzishangwu.shop263.com 电子商务]]
[[http://wangzhantuiguang.shop263.com 网站推广]]
[[http://meinv.shop263.com 美女]]
[[http://UPS.shop263.com UPS]]
[[http://yejing.shop263.com 液晶]]
[[http://niaoduzheng.shop263.com 尿毒症]]
[[http://tiaomadayinji.shop263.com 条码打印机]]
[[http://diannao.shop263.com 电脑]]
[[http://jiaju.shop263.com 家具]]
[[http://touyingmu.shop263.com 投影幕]]
[[http://fushi.shop263.com 服饰]]
[[http://wangzhankongjian.shop263.com 网站空间]]
[[http://luntai.shop263.com 轮胎]]
[[http://wangshanggouwu.shop263.com 网上购物]]
[[http://huazhuangshiji.shop263.com 化学试剂]]
[[http://meiti.shop263.com 媒体]]
[[http://shoutao.shop263.com 手套]]
[[http://ershoubijiben.shop263.com 二手笔记本]]
[[http://fadongji.shop263.com 发动机]]
[[http://mianfeixiazaidiannao.shop263.com 免费下载电影]]
[[http://shuini.shop263.com 水泥]]
[[http://shuliaozhipin.shop263.com 塑料制品]]
[[http://jichuang.shop263.com 机床]]
[[http://mianfeiyinyuexiazai.shop263.com 免费音乐下载]]
[[http://suishenting.shop263.com 随身听]]
[[http://fangshuicailiao.shop263.com 防水材料]]
[[http://fangzhijixie.shop263.com 纺织机械]]
[[http://naihuocailiao.shop263.com 耐火材料]]
[[http://feijiehe.shop263.com 肺结核]]
[[http://feiyan.shop263.com 肺炎]]
[[http://fengji.shop263.com 风机]]
[[http://jiajuzhuangshi.shop263.com 家居装饰]]
[[http://tuofa.shop263.com 脱发]]
[[http://gangban.shop263.com 钢板]]
[[http://qichemeirong.shop263.com 汽车美容]]
[[http://wangluodiannao.shop263.com 网络电话]]
[[http://jianli.shop263.com 监理]]
[[http://jiansuji.shop263.com 减速机]]
[[http://gangbanwang.shop263.com 钢板网]]
[[http://gangguan.shop263.com 钢管]]
[[http://wangluoshebei.shop263.com 网络设备]]
[[http://xiyiji.shop263.com 洗衣机]]
[[http://xianka.shop263.com 显卡]]
[[http://daoju.shop263.com 刀具]]
[[http://dianshiji.shop263.com 电视机]]
[[http://guolu.shop263.com 锅炉]]
[[http://ershouche.shop263.com 二手车]]
[[http://huaban.shop263.com 滑板]]
[[http://yanying.shop263.com 眼镜]]
[[http://bangongyongpin.shop263.com 办公用品]]
[[http://fangqiemen.shop263.com 防盗门]]
[[http://yiqi.shop263.com 仪器]]
[[http://baowencailiao.shop263.com 保温材料]]
[[http://xudianqi.shop263.com 继电器]]
[[http://bianpingqi.shop263.com 变频器]]
[[http://bangongjiaju.shop263.com 办公家具]]
[[http://huoyun.shop263.com 货运]]
[[http://gaoerfu.shop263.com 高尔夫]]
[[http://chache.shop263.com 叉车]]
[[http://chajian.shop263.com 插件]]
[[http://chaosheng.shop263.com 超声]]
[[http://yongzhuang.shop263.com 泳装]]
[[http://baozhuang.shop263.com 箱包]]
[[http://xiaotiqin.shop263.com 小提琴]]
[[http://chongya.shop263.com 冲压]]
[[http://chushiji.shop263.com 除湿机]]
[[http://youqi.shop263.com 油漆]]
[[http://xiaoweihanyang.shop263.com 小尾寒羊]]
[[http://yuanqijian.shop263.com 元器件]]
[[http://cixingcailiao.shop263.com 磁性材料]]
[[http://cixiu.shop263.com 刺绣]]
[[http://yundongxie.shop263.com 运动鞋]]
[[http://xingcai.shop263.com 型材]]
[[http://dahuoji.shop263.com 打火机]]
[[http://dazhejipiao.shop263.com 打折机票]]
[[http://dengpao.shop263.com 灯泡]]
[[http://dianzichanpin.shop263.com 电子产品]]
[[http://dianzijishu.shop263.com 电子技术]]
[[http://diancangban.shop263.com 垫仓板]]
[[http://diaogou.shop263.com 吊钩]]
[[http://diaowang.shop263.com 吊网]]
[[http://hechengxianweidiaozhuangdai.shop263.com 合成纤维吊装带]]
[[http://hongganji.shop263.com 烘干机]]
[[http://jiaodai.shop263.com 胶带]]
[[http://jiehe.shop263.com 结核]]
[[http://kafei.shop263.com 咖啡]]
[[http://kongyaji.shop263.com 空压机]]
[[http://lengqueta.shop263.com 冷却塔]]
[[http://reshuiqi.shop263.com 热水器]]
[[http://riyongpin.shop263.com 日用品]]
[[http://rupinjixie.shop263.com 乳品机械]]
[[http://shicai.shop263.com 石材]]
[[http://shipinjixie.shop263.com 食品机械]]
[[http://shouyinji.shop263.com 收音机]]
[[http://shoubiao.shop263.com 手表]]
[[http://hagongyuanliao.shop263.com 化工原料]]
[[http://duodiannaoqiehuanqi.shop263.com 多电脑切换器]]
[[http://chaye.shop263.com 茶叶]]
[[http://yiliaoshebei.shop263.com 医疗设备]]
[[http://weixingdayinji.shop263.com 微型打印机]]
[[http://yinshuiji.shop263.com 饮水机]]
[[http://wushuichuli.shop263.com 污水处理]]
[[http://yinshuaji.shop263.com 印刷机]]
[[http://binxiang.shop263.com 冰箱]]
[[http://caiwurujian.shop263.com 财务软件]]
[[http://caigangban.shop263.com 彩钢板]]
[[http://yinxiang.shop263.com 音响]]
[[http://cangchu.shop263.com 仓储]]
[[http://muzhipin.shop263.com 木制品]]
[[http://yaokongqi.shop263.com 遥控器]]
[[http://sheyingqicai.shop263.com 摄影器材]]
[[http://shuomachanpin.shop263.com 数码产品]]
[[http://chuangshangyongpin.shop263.com 床上用品]]
[[http://mianliao.shop263.com 面料]]
[[http://lalian.shop263.com 拉链]]
[[http://jixiang.shop263.com 机箱]]
[[http://baozhuangcailiao.shop263.com 包装材料]]
[[http://tuzhuang.shop263.com 涂装]]
[[http://shuibiao.shop263.com 水表]]
[[http://guanlipeixun.shop263.com 管理培训]]
[[http://menchuang.shop263.com 门窗]]
[[http://posuji.shop263.com 破碎机]]
[[http://zidongmen.shop263.com 自动门]]
[[http://zixibeng.shop263.com 自吸泵]]
[[http://yingpan.shop263.com 硬盘]]
[[http://lvseshipin.shop263.com 绿色食品]]
[[http://ranqishebei.shop263.com 燃气设备]]
[[http://rongqi.shop263.com 容器]]
[[http://ruanguan.shop263.com 软管]]
[[http://muye.shop263.com 木业]]

Topic VisitorPatternExample . { Edit | Attach | Ref-By | Printable | Diffs | r1.6 | > | r1.5 | > | r1.4 | More }
Revision r1.5 - 31 Jan 2005 - 06:28 GMT - LiYan
Parents: WebHome > CodeExamples > VisitorPatternMultiMethodExample
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.