The Java creator pattern is somewhat similar to the factory pattern, but the focus is different. factory models tend to care only about what you want, not about what the specifics of the thing are. The creation of a pattern is relative to the creation of specific details about the thing . To create a character, we are concerned not only with creating a character, but also with regard to his sex, skin color, and name, you can use the creator mode.
The program instance looks like this:
Package builder; /** * * DOC Race Role */public class Race {private string name;//name private string skincolor;//skin color Private STR
ing sex;//sex public String getName () {return this.name;
public void SetName (String name) {this.name = name;
Public String Getskincolor () {return this.skincolor;
} public void Setskincolor (String skincolor) {this.skincolor = Skincolor;
Public String Getsex () {return this.sex;
} public void Setsex (String sex) {this.sex = sex;
}} package builder;
/** * DOC We are concerned not only to create a character, but also to care about the creation of its features * * * */public class Racebuilder {private Race Race;
/** * DOC Creates a race * * @return/Public Racebuilder Builder () {this.race = new race ();
return this;
/** * DOC takes name * * @return/public racebuilder setname (String name) {this.race.setName (name);
return this;
/** * DOC Select Gender * * @return/public racebuilder setsex (String sex) { This.race.setSex (Sex);
return this; /** * DOC Select skin color * * @return/public Racebuilder Setskincolor (String skincolor) {This.race.setSkin
Color (Skincolor);
return this;
/** * DOC returns this created good race * * @return * * * public Race Create () {return this.race;
}
}
The test classes are as follows:
Package builder;
public class Main {public
static void Main (string[] args) {
Race Race = new Racebuilder (). Builder (). SetName ("John") . Setsex ("male"). Setskincolor ("white"). Create ();