Java learning notes-builder mode of design mode (1): Learning note design mode
I. Introduction
Make a little progress every day and be happy every day. I insist on writing something every day. I feel very good. I can review basic knowledge and gain a sense of accomplishment. Why not? The design pattern is something a cainiao must master to become a master. Therefore, today's topic is the builder pattern of the design pattern.
2. What is the builder model?
Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
Iii. Builder pattern class diagram
Class diagrams are an important tool for understanding design patterns. Therefore, they must be well understood.
4. Roles in builder Mode
1. Builder: provides an abstract interface to standardize the Builder's construction of each component of the product. This interface is only a specification and does not involve specific construction. The specific construction is implemented by the ConcreteBuilder inherited from it.
2. ConcreteBuilder: implements the builder interface to specify the construction of each object part based on different business logic, and finally returns a constructed product.
3. Director: the Director, as the name suggests, is responsible for standardizing the use of the process. No product creation is involved in the guidance. It is only responsible for ensuring that all parts of a complex object are created or created in a certain order.
4. Product: complex objects. It is also the object to be built.
V. Examples
Engineering Structure:
AbstractCar. java
Package designModeBulder; // abstract product class public abstract class AbstractCar {abstract public void seat (); // create a seat abstract public void light (); // construct the lighting abstract public void wheel (); // build the wheel abstract public void engine (); // build the engine // default process public void defaultCar () {seat (); light (); wheel (); engine ();}}
BenchiCar. java
Package designModeBulder; public class BenChiCar extends AbstractCar {@ Overridepublic void seat () {// TODO Auto-generated method stubSystem. out. println ("Mercedes-Benz built seat");} @ Overridepublic void light () {// TODO Auto-generated method stubSystem. out. println ("the Benz has built the light");} @ Overridepublic void wheel () {// TODO Auto-generated method stubSystem. out. println ("Mercedes-Benz built wheels");} @ Overridepublic void engine () {// TODO Auto-generated method stubSystem. out. println ("Mercedes-Benz built engine ");}}
BmwCar. java
Package designModeBulder; public class BmwCar extends AbstractCar {@ Overridepublic void seat () {// TODO Auto-generated method stubSystem. out. println ("BMW built seats");} @ Overridepublic void light () {// TODO Auto-generated method stubSystem. out. println ("BMW built lights");} @ Overridepublic void wheel () {// TODO Auto-generated method stubSystem. out. println ("BMW built wheels");} @ Overridepublic void engine () {// TODO Auto-generated method stubSystem. out. println ("BMW built engine ");}}
ICarBuilder. java
Package designModeBulder; // abstract builder public interface ICarBuilder {public void buildCar (); public AbstractCar getCar ();}
BenchiCarBuilder. java
Package designModeBulder; public class BenChiCarBuilder implements ICarBuilder {BenChiCar benChiCar = new BenChiCar (); // create a blank Mercedes-Benz First @ Overridepublic void buildCar () {// TODO Auto-generated method stubbenChiCar. wheel (); // first build the wheel benChiCar. engine (); // re-engineer the benChiCar engine. light (); // create a light benChiCar. seat (); // build the seat} @ Overridepublic AbstractCar getCar () {// TODO Auto-generated method stubreturn this. benChiCar; // a Mercedes-Benz has been built }}
BmwCarBuilder. java
package designModeBulder;public class BmwCarBuilder implements ICarBuilder{BmwCar bmwCar = new BmwCar();@Overridepublic void buildCar() {// TODO Auto-generated method stubbmwCar.wheel();bmwCar.engine();bmwCar.seat();bmwCar.light();}@Overridepublic AbstractCar getCar() {// TODO Auto-generated method stubreturn this.bmwCar;}}
CarDirector. java
Package designModeBulder; // The Director role public class CarDerictor {BenChiCarBuilder benChiCarBuilder = new BenChiCarBuilder (); // The instructor BmwCarBuilder bmwCarBuilder BmwCarBuilder = new BmwCarBuilder (); // The master who owns the construction of the BMW car is public AbstractCar getbenCar () {benChiCarBuilder. buildCar (); return benChiCarBuilder. getCar ();} public AbstractCar getBmaCar () {bmwCarBuilder. buildCar (); return bmwCarBuilder. getCar ();}}
BuilderTest. java
package designModeBulder;public class BuidlerTest {public static void main(String args[]){CarDerictor carDerictor = new CarDerictor();BenChiCar benChiCar =(BenChiCar)carDerictor.getbenCar();BmwCar bmwCar = (BmwCar)carDerictor.getBmaCar();}}
Running result:
Mercedes-Benz built wheels
Mercedes-Benz built an engine.
Mercedes-Benz built lights
Mercedes-Benz built a seat
BMW built wheels
BMW built an engine
BMW built a seat
BMW built lights
Vi. Summary
Using the builder mode, the client does not need to know the details of the product's internal composition. For example, we do not need to care about how each specific model is implemented internally. The generated object type is AbstractCar. the builders are mutually independent and easy to expand. Applicable scenarios: the builder mode can be used when different event results are generated in the same method and different execution sequence. Multiple parts or parts can be assembled into one object, however, you can use this mode when the running results are not the same. The product class is very complex, or the calling sequence in the product class produces different performance. It is very appropriate to use the builder mode in this case. Some other objects in the system will be used during the object creation process. These objects are not easy to obtain during the creation process of the product object. You can also use the builder mode to encapsulate the object creation process. This kind of scenario can only be a compensation method, because an object is not easy to obtain, but it is not found in the design stage. to soften the creation process through the Creator mode, it has already violated the original design goal.
For a very good article, refer to here.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. QQ group: 372702757