1 PackageCom.tn.pattern;2 3 Public classClient {4 Public Static voidMain (string[] args) {5Director director=director.getinstance ();6Director.construct (NewConcreteBuilder1 ()); 7Director.construct (NewConcreteBuilder2 ());8 }9 }Ten One classdirector{ A StaticDirector director; - PrivateDirector () {} - Public StaticDirector getinstance () { the if(director==NULL) -Director=NewDirector (); - returnDirector; - } + Public voidconstruct (Builder builder) { - Builder.buildpart1 (); + Builder.buildpart2 (); A } at } - - Interfacebuilder{ - voidBuildPart1 (); - voidBuildPart2 (); - Product Retrieveresult (); in } - to classConcreteBuilder1Implementsbuilder{ + product product; - @Override the Public voidBuildPart1 () { *System.out.println ("Concretebuilder1.buildpart1 ()"); $product=NewProduct1 ();Panax Notoginseng } - the @Override + Public voidBuildPart2 () {} A the @Override + PublicProduct Retrieveresult () { - returnproduct; $ } $ } - - classConcreteBuilder2Implementsbuilder{ the product product; - @OverrideWuyi Public voidBuildPart1 () {} the - @Override Wu Public voidBuildPart2 () { -System.out.println ("Concretebuilder2.buildpart2 ()"); Aboutproduct=NewProduct2 (); $ } - - @Override - PublicProduct Retrieveresult () { A returnproduct; + } the } - $ Interfaceproduct{} the the classProduct1Implementsproduct{ the PublicProduct1 () { theSystem.out.println ("Product1.product1 ()"); - } in } the the classProduct2Implementsproduct{ About PublicProduct2 () { theSystem.out.println ("Product2.product2 ()"); the } the}
"Java design mode" "Create mode creational pattern" Construction Mode builder pattern