开发者

Loose coupling : Can we use Interfaces when we need cloneables params?

开发者 https://www.devze.com 2022-12-15 03:55 出处:网络
As I was advised 开发者_JAVA技巧by PMD, I want to reduce coopling by using interfaces instead of implementation ...

As I was advised 开发者_JAVA技巧by PMD, I want to reduce coopling by using interfaces instead of implementation ...

In this case, knowing that I need a cloneable param, can I overcome the clone Dilemma (no clone() method in the Cloneable interface) ??

public MyConstructor(ArrayList<E> myParam) {
    this.myAttribute = (ArrayList<E>) myParam.clone();
}


You don't need to clone that way; I'd do it like this:

public MyConstructor(List<E> myParam) 
{
    this.myAttribute = new ArrayList<E>(myParam);
}


I don't know PMD well, but this would be a shallow copy, instead of deep copy.

0

精彩评论

暂无评论...
验证码 换一张
取 消