的鏈接想象一下,一類Person
。它的構造函數需要很多參數,而類不受你控制。如果你想簡化對象的創建,你可以創建一個構建器,讓你將指令鏈接在一起。是創建一個包裝,使制定者接受
但是,如果您想在創建後更改Person
的實例,則必須單獨訪問其所有設置器。
如果你將不得不像這樣經常做大的改變,是否可以接受爲Person
創建一個允許鏈接setter的包裝?我以前大部分都只看到了建造者的鏈接,讓制定者返回他們改變的對象是不是一個好主意?
示例代碼,以顯示我的想法:
public class Person {
String name;
int age;
public void setName(String name){ this.name = name; }
public void setAge(int age){ this.age = age; }
}
public class PersonWrapper {
Person person = new Person();
public PersonWrapper setName(String name){
this.person.setName(name);
return this;
}
public PersonWrapper setAge(int age){
this.person.setAge(age);
return this;
}
}
public class Example {
public void example() {
Person p = new Person();
PersonWrapper pw = new PersonWrapper();
//some time later, we want to change these objects
p.setName("John");
p.setAge(20); //this becomes
pw.setName("John").setAge(20); //this instead
}
}
顯然,如果是一個有點大量的屬性來設置這隻會是有用的。我只提供了兩個屬性來保持簡短。
注意:這是問題Can I do multiple actions to one component in a more elegant way?,讓我想知道這一點。起初,我只是想問這是對接受答案的評論,但顯然我的代表太低評論。如果我錯誤地發佈這個作爲一個新問題,請讓我知道,所以下次我不會犯同樣的錯誤。
我寧願'Builder'或'Factory'模式。爲複雜對象創建包裝類會使整個數據模型更加複雜,並減少對隱藏在包裝類中的複雜對象的控制。例如,如果您想更改複雜對象的屬性,但您的包裝器沒有適合此目的的委託方法。 – 2016-05-14 12:56:06
@Rafael無論是''Builder''和''Factory''是,雖然創建對象,我想知道在創建之後改變對象的狀態。無論如何,如果我正確地理解了你,對複雜對象進行大的修改,只需使用它的setter?或儘可能避免大型,易變,複雜的對象? – SecularisticSloth
一般而言,複雜的對象本身並不是一個好主意。如果對象是太複雜了,它看起來像這個對象「知道」到多,負責[太多** **不同的行動](https://en.wikipedia.org/wiki/God_object)。重新考慮複雜類的設計,將這些類拆分爲更簡單的類,以使它**方便**與它們一起工作。在開發過程中,_convenience_和簡單表明您正處於正確的道路上。反之亦然。 – 2016-05-14 17:39:10