Java design pattern-create analysis Builder pattern from Dota's weapon

Source: Internet
Author: User
Tags dota

Dota is one of the most popular games at the moment. Players are divided into two teams: tianhui (near guard) and Dayu (natural disasters). Each team has five members, money can be used to buy powerful weapons by making up the knife and killing the enemy to gain experience and money. In the end, the enemy's base will be defeated. We now consider the simplest weapon composition element: a weapon is composed of the weapon name, attack force, and weapon color. We have to find a way to create a new model of various weapons. Suppose we preset two weapons: the holy sword (attack force 300, yellow) and the Holy One (attack force 60, blue ), I will try to use the generator mode to create a small frame for weapon generation.

The purpose of the builder mode is to separate a complex object from its representation so that different representations can be created during the same build process.

We extract the abstract process of generating weapons: Define a weapon base class, including the weapon name, attack power, color, and printing weapon information; then, we name the weapon and assign the attack power value and color to generate a weapon, and finally return the generated weapon. The Java code is as follows:

Class Weapon {private String color; private String name; private int attack; public String getName () {return name;} public int getAttack () {return attack;} public String getColor () {return color;} public void setAttack (int _ attack) {attack = _ attack;} public void setName (String _ name) {name = _ name ;} public void setColor (String _ color) {color = _ color;} public void print () {System. out. printf ("Weapon name: % s, attack power: % d, color: % s \ n", name, attack, color) ;}} interface WeaponBuilder {void buildBasicInformation (); void buildColor (); Weapon getWeapon ();} class Weapons {public static void build (WeaponBuilder builder) {builder. buildBasicInformation (); builder. buildColor () ;}} class DivineRapierBuilder implements WeaponBuilder {private Weapon weapon = new Weapon (); public void buildBasicInformation () {weapon. setName (""); weapon. setattack( 300);} public void buildColor () {weapon. setColor ("yellow");} public Weapon getWeapon () {return weapon;} class SacredRelicBuilder implements WeaponBuilder {private Weapon weapon = new Weapon (); public void buildBasicInformation () {weapon. setName ("holy things"); weapon. setAttack (60);} public void buildColor () {weapon. setColor ("blue");} public Weapon getWeapon () {return weapon;} class Builder {public static void main (String [] args) {WeaponBuilder divineRapierBuilder = new DivineRapierBuilder (); Weapons. build (divineRapierBuilder); WeaponBuilder sacredRelicBuilder = new SacredRelicBuilder (); Weapons. build (sacredRelicBuilder); Weapon divineRapier = divineRapierBuilder. getWeapon (); Weapon sacredRelic = sacredRelicBuilder. getWeapon (); divineRapier. print (); sacredRelic. print ();}}

Output result:

Weapon name: Holy sword, attack force: 300, color: Yellow
Weapon name: Holy One relics, attack power: 60, color: Blue

The following is a brief analysis of the above Code. The Weapon class is a class that records Weapon information. We finally want to return a Weapon object we need. This Weapon object is a complex object, which consists of two parts: Name and attack power. We declare the necessary methods to generate a Weapon in the WeaponBuilder interface, the following describes how to implement two weapons, namely DivineRapierBuilder and SacredRelicBuilder ). Finally, we define a Weapons class for weapon manufacturing. The static method build in it passes in a WeaponBuilder object, which calls the build of this object and returns getWeapon as the result. As described in the main method, we can always call Weapons. build to create a new weapon. Weapons itself does not create a weapon and relies mainly on the imported WeaponBuilder. To create a new weapon, we only need to construct a class that inherits the WeaponBuilder interface.

As mentioned in the design patterns book, abstract factory patterns are similar to Builder patterns, which allow you to create complex objects. The difference is that the Builder mode focuses on building a complex object step by step, abstract Factory focuses on the creation of multiple series of product objects (such as ProduceMarine and ProduceFirebat of the previous blog). Builder returns the product in the last step, while Abstract Factory returns the product immediately.

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.