Java design pattern-builder mode

Source: Internet
Author: User

Builder Pattern is a design pattern that creates shapes, and uses multiple simple objects to build a complex object step-by-step.

Main Solution: The main solution in the software system, sometimes facing the creation of "a complex object", which is usually made up of the sub-objects of the various parts with a certain algorithm, because of the change in demand, the various parts of this complex object often face drastic changes, But the algorithms that combine them are relatively stable.

when to use: some basic components do not change, and their combinations often change.

Application Example: 1, to KFC, Hamburger, Cola, French fries, fried chicken wings and so on is the same, and its combination is often changed, generated so-called "package." 2. StringBuilder in JAVA.

Advantages: 1, the builder is independent, easy to expand. 2, easy to control the details of risk.

Disadvantage: 1, the product must have common ground, the scope is limited. 2, if the internal changes complex, there will be a lot of construction class.

usage Scenario: 1. Objects that need to be generated have complex internal structures. 2. The intrinsic properties of the objects that need to be generated depend on each other.

Note: the difference from the factory model is that the builder mode is more concerned with the order in which the parts are assembled.

Class Diagram:

Code:

First, the establishment of packaging interface

1  Public Interface Packing {2    Public String Pack (); 3 }

Second, establish the interface of the menu item

1  Public Interface Item {2    Public String name (); 3    Public Packing Packing (); 4    Public float Price (); 5 }

Third, the establishment of the implementation of the packaging interface of the entity class

 1  public  class  Wrapper implements   Packing { 2  3   @Override  4  public   String Pack () { 5  return  "Wrapper"  6  }  7  8 } 
1  Public class Implements Packing {23    @Override4public       String Pack () {5         return "Buttle"; 6     }78 }

Iv. creating an abstract class to implement the menu item interface

 1  public  abstract  class  Burger implements   Item { 2  3   @Override  4  public   Packing Packing () { 5  return  new   Wrapper ();  6  }  7  }
1  Public Abstract class Implements Item {23    @Override4public       Packing Packing () {5         returnnew  Bottle (); 6     }7 }

Establish the entity class products which inherit abstract class respectively

1  Public classChickenburgerextendsBurger {2 3 @Override4      PublicString name () {5         return"Chickenburger";6     }7 8 @Override9      Public floatPrice () {Ten         return50.5f; One     } A  -}
1  Public classVegburgerextendsBurger {2 3 @Override4      PublicString name () {5         return"Vegburger";6     }7 8 @Override9      Public floatPrice () {Ten         return25.0f; One     } A  -}
1  Public classCokeextendsColddrink {2 3 @Override4      PublicString name () {5         return"Coke";6     }7 8 @Override9      Public floatPrice () {Ten         return30.0f; One     } A  -}
1  Public classPepsiextendsColddrink {2 3 @Override4      PublicString name () {5         return"Pepsi";6     }7 8 @Override9      Public floatPrice () {Ten         return35.0f; One     } A  -}

Vi. Create a menu class that provides a way to order, settle accounts, and get the name of a dish

1  Public classMeal {2    Privatelist<item> items =NewArraylist<item>();3    4     Public voidAdditems (item item) {5 Items.Add (item);6    }7    8     Public floatGetcost () {9        floatTotal = 0.0f;Ten         for(Item item:items) { Onetotal+=Item.price (); A     } -        returnTotal ; -    } the     -     Public voidShowitems () { -         for(Item item:items) { -System.out.print ("Item:" +item.name ()); +System.out.print (", Packing:" +item.packing (). Pack ()); -System.out.println (", Price:" +Item.price ()); +     } A    } at}

Set up the product combination class, responsible for creating the menu

1  Public classMealbuilder {2     PublicMeal preparevegmeal () {3Meal Meal =NewMeal ();4Meal.additems (NewVegburger ());5Meal.additems (NewCoke ());6        returnmeal;7    }8    9     PublicMeal preparenonvegmeal () {TenMeal Meal =NewMeal (); OneMeal.additems (NewChickenburger ()); AMeal.additems (NewPepsi ()); -        returnmeal; -    } the}

Eight, the user, you can choose a different combination package

1  Public classBuiderpatterndemo {2 3      Public Static voidMain (string[] args) {4Mealbuilder Mealbuilder =NewMealbuilder ();5         6Meal Meal1 =mealbuilder.preparenonvegmeal ();7 Meal1.showitems ();8         9Meal Meal2 =mealbuilder.preparenonvegmeal ();Ten Meal2.showitems (); One     } A  -}

Java design pattern-builder mode

Related Article

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.