MasterofProject

JAVA design patterns: Visitor patterns

188 people read comment(0) Collection report
Classification:
  • Visitor mode:
    • The operation of each element in an object structure allows you to add a new operation to these elements without changing the data structures of the various elements.
    • Structural object is a necessary condition for visitor mode, and the structure object must exist a method to traverse its own individual objects.
    • Applicable to: the data structure is relatively stable, the data structure and the role of its other operations, so that the operation is relatively free.
    • advantage
    • 1, in accordance with the principle of a single responsibility
    • 2, good scalability: the elements of the class can be accepted by different visitors to achieve the expansion of different operations.
    • Disadvantages:
    • 1, if you want to add, will make the operation become more complex
    • 2, in a certain program to undermine the principle of encapsulation
      *
      Five visitors to the role of mechanical objects:
      1.Visitor Abstract visitor role
      2.ConcreteVisitor. specific visitor role
      3.Element accepts access to the operating element
      4.ConcreteElement concrete elements
      5.ObjectStructure structure object role, which is required to use the role of the visitor pattern.
/ * *
* Abstract visitor: declare a access operation interface for the specific element role in the structure of the object.
* the name and the parameters of the operation interface identify the specific elements of the specific visitor to the specific visitor,
* so that the visitor can directly access it through the specific interface of the element role.
*@description:
*@date2016-1-15 PM 4:00:29
* /
Public Interface Visitor{
    VoidVisit (element Element);
}
/ * *
* the specific visitor role, to achieve the Visitor declaration of the interface.
*@description:
*@date2016-1-15 PM 4:20:46
* /
Public Class ConcreteVisitor Implements Visitor{

    @Override
    Public Void Visit(element Element) {
E= element (Staff) Staff;
        For example: A / / calculation method: position * * corresponding coefficient raises + seniority system and divided by 10 and multiplied by the wage now
System.out.println (e.getName) +"The salary to be paid is:"+ (e.getDegree) *Zero point eight+e.getWorkAges () *Zero point five) /Ten*e.getSalary ());/ /
}
}
/ * *
* define a parameter for the operator class, the visitor (Visitor) operator function.
*@description:
*@date2016-1-15 PM 3:58:28
* /
Public Abstract Class Element{
    Public Abstract Void Acceppt(visitor Visitor);
}
/ * *
* specific elements, to achieve the abstract elements (Element) defined by the interface to accept the operation.
*@description:
*@date2016-1-15 PM 4:04:24
* /
Public Class Staff Extends Element{
    PrivateName String;
    Private FloatSalary;
    Private IntWorkAges;
    Private IntDegree;

    Public Staff(name String,FloatSalary,IntWorkAges,IntDegree) {
        Super();
        This.name = name;
        This.salary = salary;
        This.workAges = workAges;
        This.degree = degree;
}

    PublicStringGetName(1)
        ReturnName;
}

    Public Void SetName(name String) {
        This.name = name;
}

    Public Float GetSalary(1)
        ReturnSalary;
}

    Public Void SetSalary(FloatSalary) {
        This.salary = salary;
}

    Public Int GetWorkAges(1)
        ReturnWorkAges;
}

    Public Void SetWorkAges(IntWorkAges) {
        This.workAges = workAges;
}

    Public Int GetDegree(1)
        ReturnDegree;
}

    Public Void SetDegree(IntDegree) {
        This.degree = degree;
}

    @Override
    Public Void Acceppt(visitor Visitor) {
Visitor.visit (This);
}

}
/ * *
* structural object: This is the role of the use of the visitor pattern.
* it has the following characteristics:
* can enumerate its elements;
* you can provide a high-level interface to allow visitors to access its elements;
* if necessary, can be designed to be a composite object or a collection (such as a list or a collection of disorder).
*@description:
*@date2016-1-15 PM 4:26:30
* /
Public Class StaffObject{
    PrivateStaff>, employees HashMap<String;

    Public StaffObject(1)
Employees =NewHashMap<String, Staff> ();
}

    公共 无效 添加一个新员工(工作人员){
        如果(!员工。containsKey(E. getname())){
员工。把(如getname(),E);
}
}

    公共 无效 removeemployee(工作人员){
        如果(员工。containsKey(E. getname())){
雇员(电子);
}
}

    公共工作人员GetEmployee(字符串名称){
        返回雇员(名字);
}

    公共 无效 接受(访客五){
        对于(员工:员工。values()){
e.acceppt(V);
}
}
}

测试类

公共 试验{
    公共 静态 无效 主要(string [] args){
staffobject E =新的staffobject();
e.addemployee(新的工作人员(“张三”三千女,);
e.addemployee(新的工作人员(“李四”五千女,);
e.addemployee(新的工作人员(“王五”八千女,);
e.addemployee(新的工作人员(“沈七”一万女,);
e.accept(新的concretevisitor());

}

}

运行结果:
李四要加的薪水是:1800
张三要加的薪水是:540
沈七要加的薪水是:7700
王五要加的薪水是:4320

猜你在找
查看评论
*以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:48916次
    • 积分:二千二百六十二
    • 等级:
    • 排名:10193名第
    • 原创:183篇
    • 转载:37篇
    • 译文:1篇
    • 评论:13条
    文章分类
    文章存档
    最新评论