Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 OOP设计-需要准备每个方法调用并在调用后清理_Java_Oop_Design Patterns - Fatal编程技术网

Java OOP设计-需要准备每个方法调用并在调用后清理

Java OOP设计-需要准备每个方法调用并在调用后清理,java,oop,design-patterns,Java,Oop,Design Patterns,我的问题是创建一个类,该类允许安全地使用对象(设置容器或简单的容器)。我有10多种方法需要重复相同的操作:保存容器状态、更改状态、执行一些工作、恢复状态、返回结果。例如: public List<String> childKeys() { //saving state (repeated many time in other methods) final String clientGroup = clientSettings.group(); //changi

我的问题是创建一个类,该类允许安全地使用对象(设置容器或简单的容器)。我有10多种方法需要重复相同的操作:保存容器状态、更改状态、执行一些工作、恢复状态、返回结果。例如:

public List<String> childKeys() {
    //saving state (repeated many time in other methods)
    final String clientGroup = clientSettings.group();
    //changing state  (repeated many time in other methods)
    clientSettings.endAllGroups();
    clientSettings.beginGroup(currentGroup);
    //doing job (exclusive for each method)
    final List<String> childKeys = clientSettings.childKeys();
    //restoring state  (repeated many time in other methods)
    clientSettings.endAllGroups();
    clientSettings.beginGroup(clientGroup);

    return childKeys;
}
公共列表子密钥(){
//保存状态(在其他方法中重复多次)
最后一个字符串clientGroup=clientSettings.group();
//改变状态(在其他方法中重复多次)
clientSettings.endAllGroups();
clientSettings.beginGroup(当前组);
//做作业(每种方法除外)
最终列表childKeys=clientSettings.childKeys();
//恢复状态(在其他方法中重复多次)
clientSettings.endAllGroups();
clientSettings.beginGroup(clientGroup);
返回子密钥;
}

我需要这样做,以防止用户更改系统设置,并希望避免多次编写相同的代码。
是否有办法重新设计代码,以便自动准备和清理方法调用

也许您可以创建一个原子方法来创建这两个方法(endAllGroups和beginGroup),一个名为changeState(Group)的方法。。。在这种情况下,只要您愿意,该方法就会被更改或修改…

也许您可以创建一个原子方法来创建这两个方法(endAllGroups和beginGroup),一个名为changeState(Group)的方法。。。在这种情况下,只要您愿意,就会更改或修改方法…

创建一个具有基本流并使用具体类必须实现的
abstract
方法的
abstract

创建一个
abstract
类,该类具有基本流,并使用具体类必须实现的
abstract
方法

如果我很了解您,您想确保在特定方法集之前和/或之后调用特定代码吗

你考虑过吗?这将是一种很好的方法,可以将相同的代码添加到一组方法调用中,而不必到处使用超类/额外调用来污染代码


是一个很好的实现,它在引擎盖下使用AspectJ。看看。

如果我很了解您,您想确保在某组方法之前和/或之后调用某个代码吗

你考虑过吗?这将是一种很好的方法,可以将相同的代码添加到一组方法调用中,而不必到处使用超类/额外调用来污染代码


是一个很好的实现,它在引擎盖下使用AspectJ。看看。

有几个选项,其中一个是应用命令模式。如果不同的函数(方法)仅在几行中不同,那么这可能没有意义,在这种情况下,AOP解决方案可能是更好的选择-如@Guillaume所述

例如:

public abstract class Command<T> {

    public final T execute() {

        /* do common stuff... */
        clientSettings.endAllGroups();
        clientSettings.beginGroup(currentGroup);

        /* do the concrete stuff... */
        T t = this.performConcrete();

        /* do common stuff... */
        clientSettings.endAllGroups();
        clientSettings.beginGroup(clientGroup);

        return t;
    }

    protected abstract T performConcrete();
}
公共抽象类命令{
公共最终执行{
/*做一些普通的事情*/
clientSettings.endAllGroups();
clientSettings.beginGroup(当前组);
/*做具体的事情*/
T=this.performConcrete();
/*做一些普通的事情*/
clientSettings.endAllGroups();
clientSettings.beginGroup(clientGroup);
返回t;
}
受保护的抽象T性能混凝土();
}
以及具体实施:

public class ChildKeysCommand<List<String>> extends Command {

    protected List<String> performConcrete() {
        return clientSettings.childKeys();
    }

}
公共类childKeyCommand扩展命令{
受保护列表performConcrete(){
返回clientSettings.childKeys();
}
}
打电话的人:

ChildKeysCommand c = new ChildKeysCommand();
List<String> keys = c.execute();
ChildKeysCommand c=new ChildKeysCommand();
列表键=c.execute();

有几个选项,其中一个是应用命令模式。如果不同的函数(方法)仅在几行中不同,那么这可能没有意义,在这种情况下,AOP解决方案可能是更好的选择-如@Guillaume所述

例如:

public abstract class Command<T> {

    public final T execute() {

        /* do common stuff... */
        clientSettings.endAllGroups();
        clientSettings.beginGroup(currentGroup);

        /* do the concrete stuff... */
        T t = this.performConcrete();

        /* do common stuff... */
        clientSettings.endAllGroups();
        clientSettings.beginGroup(clientGroup);

        return t;
    }

    protected abstract T performConcrete();
}
公共抽象类命令{
公共最终执行{
/*做一些普通的事情*/
clientSettings.endAllGroups();
clientSettings.beginGroup(当前组);
/*做具体的事情*/
T=this.performConcrete();
/*做一些普通的事情*/
clientSettings.endAllGroups();
clientSettings.beginGroup(clientGroup);
返回t;
}
受保护的抽象T性能混凝土();
}
以及具体实施:

public class ChildKeysCommand<List<String>> extends Command {

    protected List<String> performConcrete() {
        return clientSettings.childKeys();
    }

}
公共类childKeyCommand扩展命令{
受保护列表performConcrete(){
返回clientSettings.childKeys();
}
}
打电话的人:

ChildKeysCommand c = new ChildKeysCommand();
List<String> keys = c.execute();
ChildKeysCommand c=new ChildKeysCommand();
列表键=c.execute();

正如人们所建议的,使用AOP框架显然是最好的选择


如果由于某种原因无法使用,或者希望对代码库进行最小的更改,则可以通过使用java.lang.reflect.proxy创建代理类来实现相同的功能。您只需要非常好地记录它…

正如人们所建议的,使用AOP框架显然是最好的选择


如果由于某种原因无法使用,或者希望对代码库进行最小的更改,则可以通过使用java.lang.reflect.proxy创建代理类来实现相同的功能。您只需要非常好地记录它…

能否在开始时保存一次,然后在结束时恢复?在任何情况下,当真正的修复程序不需要您这样做时,这听起来像是一个解决办法。@PeterLawrey,不,
clientSetting
状态可以从外部多次请求,并且必须在环境中保持不变。您能在开始时保存一次,在结束时恢复吗?在任何情况下,这听起来都像是一种解决办法,因为真正的修复程序不需要您这样做。@PeterLawrey,不,
clientSetting
状态可以从外部多次请求,并且在环境中必须保持不变。是的,您得到了