打字猴:1.700472293e+09
1700472293
1700472294 结构对象是产生出不同的元素对象,我们使用工厂方法模式来模拟,如代码清单25-15所示。
1700472295
1700472296 代码清单25-15 结构对象
1700472297
1700472298 public class ObjectStruture{
1700472299
1700472300 //对象生成器,这里通过一个工厂方法模式模拟
1700472301
1700472302 public static Element createElement(){
1700472303
1700472304 Random rand=new Random();
1700472305
1700472306 if(rand.nextInt(100)>50){
1700472307
1700472308 return new ConcreteElement1();
1700472309
1700472310 }else{
1700472311
1700472312 return new ConcreteElement2();
1700472313
1700472314 }
1700472315
1700472316 }
1700472317
1700472318 }
1700472319
1700472320 进入了访问者角色后,我们对所有的具体元素的访问就非常简单了,我们通过一个场景类模拟这种情况,如代码清单25-16所示。
1700472321
1700472322 代码清单25-16 场景类
1700472323
1700472324 public class Client{
1700472325
1700472326 public static void main(String[]args){
1700472327
1700472328 for(int i=0;i<10;i++){
1700472329
1700472330 //获得元素对象
1700472331
1700472332 Element el=ObjectStruture.createElement();
1700472333
1700472334 //接受访问者访问
1700472335
1700472336 el.accept(new Visitor());
1700472337
1700472338 }
1700472339
1700472340 }
1700472341
1700472342 }
[ 上一页 ]  [ :1.700472293e+09 ]  [ 下一页 ]