Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 访客模式的使用_Java_Oop_Design Patterns - Fatal编程技术网

Java 访客模式的使用

Java 访客模式的使用,java,oop,design-patterns,Java,Oop,Design Patterns,我有一节这样的课 public EmployeeRepositoryImpl{ public Employee save(final Employee employee) { return employeeDao.save(sanitizeEmployee(employee)); } Employee sanitizeEmployee(Employee employee){ employee.setName

我有一节这样的课

 public  EmployeeRepositoryImpl{
    public Employee save(final Employee employee) {

      return    employeeDao.save(sanitizeEmployee(employee));
       }

        Employee sanitizeEmployee(Employee employee){

            employee.setName(cleanUpBadData(employee.getName());
            employee.setPhone(cleanUpBadData(employee.getPhone());
            employee.setAddress(cleanUpBadData(employee.getAddress());
      ......

            return employee;
        }

    private static String cleanUpBadData(String attribute) {
            //cleanbaddata here
            return attribute;
        }
    }
我想用visitor模式重构它,以防明天我们需要添加额外的逻辑,比如我今天添加的cleanup。我不确定我是否正确地使用了visitor模式,因为我没有accept方法和Visitable。有人能告诉我正确的方法吗

我创造

public interface EmployeeVisitor {


    void visitEmployee(Employee employee);

}

public class EmployeeVisitorImpl implements EmployeeVisitor {

public void visitEmployee(Employee employee)
{

                employee.setName(cleanUpBadData(employee.getName());
                employee.setPhone(cleanUpBadData(employee.getPhone());
                employee.setAddress(cleanUpBadData(employee.getAddress());
          ......

                return employee;
            }

        private static String cleanUpBadData(String attribute) {
                //cleanbaddata here
                return attribute;
            }

}

访问者模式的正确实现如下所示:

interface EmployeeVisitor {
    void visitEmployee(Employee employee);
}

class CleanUpEmployee implements EmployeeVisitor {
    void visitEmployee(Employee employee) {
        ...
    }
}

class Employee {
    void accept(EmployeeVisitor visitor) {
        visitor.visitEmployee(this);
    }
}
访问者模式是专门设计用来将一个或多个算法从它们所操作的类结构中分离出来的。访问者模式没有任何意义,除非您试图使算法独立于类结构。在您的例子中,您似乎没有
Employee
的任何扩展,因此使用访问者实际上没有任何价值-只需创建类的算法方法即可


如果您的目的是能够在不更改类的情况下向类添加其他操作,那么还有其他模式可能更有用。例如,允许在运行时选择行为,并允许使用其上下文封装行为

访问者模式的正确实现如下所示:

interface EmployeeVisitor {
    void visitEmployee(Employee employee);
}

class CleanUpEmployee implements EmployeeVisitor {
    void visitEmployee(Employee employee) {
        ...
    }
}

class Employee {
    void accept(EmployeeVisitor visitor) {
        visitor.visitEmployee(this);
    }
}
访问者模式是专门设计用来将一个或多个算法从它们所操作的类结构中分离出来的。访问者模式没有任何意义,除非您试图使算法独立于类结构。在您的例子中,您似乎没有
Employee
的任何扩展,因此使用访问者实际上没有任何价值-只需创建类的算法方法即可


如果您的目的是能够在不更改类的情况下向类添加其他操作,那么还有其他模式可能更有用。例如,允许在运行时选择行为,并允许使用其上下文封装行为

似乎不太适合访问者模式。这里可以使用什么模式。用例是明天,可以添加更多逻辑,就像清理数据一样。可能是清理xml错误字符。为什么需要模式?只需更新你的
cleanubaddata()
方法。为什么你的代码看起来这么凌乱?您应该让IDE自动格式化它。这似乎不适合访问者模式。这里可以使用什么模式。明天的用例是可以添加一些逻辑,就像清理数据一样。可能是清理xml错误字符。为什么需要模式?只需更新你的
cleanubaddata()
方法。为什么你的代码看起来这么凌乱?您应该让IDE自动格式化它。