简体   繁体   English

避免函数参数操作的最佳实践

[英]Best practice to avoid function's parameter manipulation

I have the following abstract code snippet:我有以下抽象代码片段:

// definitions
    MyClass1 mc1 = new MyClass1();
    MyClass2 mc2 = new MyClass2();
    List<String> elements = new ArrayList<>();

// some black magic...

// setters based on condition 0
    mc2.setElements(elements);
    String id = saveMyClass2(mc2)
    mc1.setMc1Id(id);

// Some other business black magic

// setters based on condition 1
    mc2.setElements(elements);
    String id = saveMyClass2(mc2)
    mc1.setMc1Id(id);

// Some other business black magic

// setters based on condition 2
    mc2.setElements(elements);
    String id = saveMyClass2(mc2)
    mc1.setMc1Id(id);

I'm using the setters flow in multiple parts of my code.我在代码的多个部分使用了 setter 流程。 I wanted to elaborate this by a wrapper function like this:我想通过这样的包装函数来详细说明:

void doSettings(MyClass1 mc1, MyClass2 mc2, List<String> elements) {
        mc2.setElements(elements);
        String id = saveMyClass2(mc2)
        mc1.setMc1Id(id);
}

But I am doing a parameter manipulation in this function which I want to avoid.但是我正在这个函数中进行参数操作,我想避免。 Also cloning the classes is not an option, they are 3 layers deep and it takes too much effort to create deep copy.克隆类也不是一种选择,它们有 3 层深,创建深拷贝需要花费太多精力。

So do you know any best practice or coding pattern which helps to avoid code duplication?那么,您是否知道有助于避免代码重复的任何最佳实践或编码模式?

One really good way of doing this is to use an Immutable decorator.一个真正好的方法是使用Immutable装饰器。 The Immutable pattern is a wrapper around the primary object that prevents changes to the underlying data. Immutable模式是主要对象的包装器,可防止对基础数据进行更改。 These exist out of the box for the Collections using the Unmodifable concept:这些使用Unmodifable概念的Collections开箱即用:

 String[] array = ... ;
 List<String[]> list = List.<String[]>of(array);

or copyOf() in the case of another collection:copyOf()在另一个集合的情况下:

 List<String> mylist = ...;
 List<String> unmodifableList = List.copyOf(mylist);

For the other objects that you are defining, you might want to create a subclass that throws UnsupportedOperationException when a mutator is called.对于您正在定义的其他对象,您可能希望创建一个子类,在调用更改器时抛出UnsupportedOperationException

class UnmodifiableMyClass1 extends MyClass1 {
    UnmodifiableMyClass1( MyClass1 copy) {
        // do copy constructor stuff here 
        // (probably just referencing the primary object)
    }
    @override void setMc1Id( int id ) {
        throw new UnsupportedOperationException("nope");
    }
}

MyClass1 mc1 = new MyClass1();
UnmodifiableMyClass1 umc1 = new UnmodifiableMyClass1(mc1);

of course, it's best if you can do this on interfaces instead of implementations, because there's less need to maintain the object state, but you gotta work with what you have, and you didn't mention having interfaces for MyClass{1,2} .当然,如果您可以在接口而不是实现上执行此操作,那是最好的,因为维护对象状态的需求较少,但是您必须使用现有的东西,并且您没有提到拥有MyClass{1,2}的接口.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM