打字猴:1.70047226e+09
1700472260 public interface IVisitor{
1700472261
1700472262 //可以访问哪些对象
1700472263
1700472264 public void visit(ConcreteElement1 el1);
1700472265
1700472266 public void visit(ConcreteElement2 el2);
1700472267
1700472268 }
1700472269
1700472270 具体访问者如代码清单25-14所示。
1700472271
1700472272 代码清单25-14 具体访问者
1700472273
1700472274 public class Visitor implements IVisitor{
1700472275
1700472276 //访问el1元素
1700472277
1700472278 public void visit(ConcreteElement1 el1){
1700472279
1700472280 el1.doSomething();
1700472281
1700472282 }
1700472283
1700472284 //访问el2元素
1700472285
1700472286 public void visit(ConcreteElement2 el2){
1700472287
1700472288 el2.doSomething();
1700472289
1700472290 }
1700472291
1700472292 }
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
[ 上一页 ]  [ :1.70047226e+09 ]  [ 下一页 ]