设计模式学习之访问者模式

阅读更多

1.名词解释:

访问者模式:标识一个作用于某对象结构中的各元素的操作。它可使你在不改变各元素的前提下作用于这些元素的新操作。

 

2.代码示例:

 

访问者抽象类
package DesignPattern.VisitorPattern;

public abstract class Visitor {

	public abstract void VisitConcreateElementA(ConcreateElementA concreateElementA);
	
	public abstract void VisitConcreateElementB(ConcreateElementB concreateElementB); 
}

 

实际访问者类A
package DesignPattern.VisitorPattern;

public class ConcreateVisitorA extends Visitor{

	@Override
	public void VisitConcreateElementA(ConcreateElementA concreateElementA) {
		// TODO Auto-generated method stub
		System.out.println(concreateElementA.getName() + "失败时,闷头喝酒,谁也不用劝。");
		
	}

	@Override
	public void VisitConcreateElementB(ConcreateElementB concreateElementB) {
		// TODO Auto-generated method stub
		System.out.println(concreateElementB.getName() + "失败时,眼泪汪汪,谁也劝不住。");
	}

}

 

实际访问者类B
package DesignPattern.VisitorPattern;

public class ConcreateVisitorB extends Visitor{

	@Override
	public void VisitConcreateElementA(ConcreateElementA concreateElementA) {
		// TODO Auto-generated method stub
		System.out.println(concreateElementA.getName() + "成功时,背后多半有一个伟大的女人。");
	}

	@Override
	public void VisitConcreateElementB(ConcreateElementB concreateElementB) {
		// TODO Auto-generated method stub
		System.out.println(concreateElementB.getName() + "成功时,背后大多有一个不成功的男人。");
	}

}

 

实际访问者类C
package DesignPattern.VisitorPattern;

public class ConcreateVisitorC extends Visitor{

	@Override
	public void VisitConcreateElementA(ConcreateElementA concreateElementA) {
		// TODO Auto-generated method stub
		System.out.println(concreateElementA.getName() + "恋爱时,凡事不懂也要装懂。");
		
	}

	@Override
	public void VisitConcreateElementB(ConcreateElementB concreateElementB) {
		// TODO Auto-generated method stub
		System.out.println(concreateElementB.getName() + "成功时,遇事懂也装作不懂。");
		
	}

}

 

抽象元素类
package DesignPattern.VisitorPattern;

public abstract class Element {

	public abstract void Accept(Visitor visitor);
}

 

实际元素类A
package DesignPattern.VisitorPattern;

public class ConcreateElementA extends Element{
	
	private String name = "";
	
	public ConcreateElementA(String name) {
		this.name = name;
	}

	@Override
	public void Accept(Visitor visitor) {
		// TODO Auto-generated method stub
		visitor.VisitConcreateElementA(this);
	}
	
	public String getName() {
		return this.name;
	}

}

 

实际元素类B
package DesignPattern.VisitorPattern;

public class ConcreateElementB extends Element{

	private String name = "";
	
	public ConcreateElementB(String name) {
		this.name = name;
	}
	
	@Override
	public void Accept(Visitor visitor) {
		// TODO Auto-generated method stub
		visitor.VisitConcreateElementB(this);
	}
	
	public String getName() {
		return this.name;
	}

}

 

主控程序
package DesignPattern.VisitorPattern;

public class Client {
    
	public static void main(String[] args) {
		ObjectStructure objectStructure = new ObjectStructure();
		objectStructure.add(new ConcreateElementA("男人"));
		objectStructure.add(new ConcreateElementB("女人"));
		ConcreateVisitorA v1 = new ConcreateVisitorA();
		ConcreateVisitorB v2 = new ConcreateVisitorB();
		ConcreateVisitorC v3 = new ConcreateVisitorC();
		objectStructure.Accept(v1);
		objectStructure.Accept(v2);
		objectStructure.Accept(v3);
	}
}

 
 

 

 

你可能感兴趣的:(访问者模式)