Builder mode: The construction of a complex object is separated from its representation, allowing the same build process to create different representations.
Simple example:
1 Public classProduct2 {3 PrivateString Parta;4 PrivateString PartB;5 6 Public voidSetparta (String parta)7 {8 This. Parta =Parta;9 }Ten One Public voidSETPARTB (String PartB) A { - This. PartB =PartB; - } the - @Override - PublicString toString () - { + return"[Parta =" + Parta + ", PartB =" + PartB + "]"; - } + } A at Public InterfaceFactory - { - Public voidinit (); - Public voidBuildparta (); - Public voidBUILDPARTB (); - PublicProduct getproduct (); in } - to Public classFactory1a2bImplementsFactory + { - Privateproduct product; the * @Override $ Public voidInit ()Panax Notoginseng { -Product =NewProduct (); the } + A @Override the Public voidBuildparta () + { -Product.setparta ("1"); $ } $ - @Override - Public voidBUILDPARTB () the { -PRODUCT.SETPARTB ("2");Wuyi } the - @Override Wu PublicProduct getproduct () - { About returnproduct; $ } - } - - Public classDirector A { + PrivateFactory Factory =Newfactory1a2b (); the - PublicProduct newproduct () $ { the factory.init (); the Factory.buildparta (); the FACTORY.BUILDPARTB (); the returnfactory.getproduct (); - } in } the the Public classTest About { the Public Static voidMain (string[] args) the { theDirector Director =NewDirector (); + System.out.println (Director.newproduct ()); - } the}
The builder pattern for Java design patterns