Introduction to the creator pattern of Java design Patterns _java

Source: Internet
Author: User

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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.