1. 意图
5. 效果
package builder.builders;import builder.cars.CarType;import builder.cars.Manual;import builder.components.Engine;import builder.components.GPSNavigator;import builder.components.Transmission;import builder.components.TripComputer;/** * @author GaoMing * @date 2021/7/18 - 10:03 * Unlike other creational patterns, Builder can construct unrelated products, * which don't have the common interface. * In this case we build a user manual for a car, using the same steps as we * built a car. This allows to produce manuals for specific car models, * configured with different features. */public class CarManualBuilder implements Builder{ private CarType type; private int seats; private Engine engine; private Transmission transmission; private TripComputer tripComputer; private GPSNavigator gpsNavigator; @Override public void setCarType(CarType type) { this.type = type; } @Override public void setSeats(int seats) { this.seats = seats; } @Override public void setEngine(Engine engine) { this.engine = engine; } @Override public void setTransmission(Transmission transmission) { this.transmission = transmission; } @Override public void setTripComputer(TripComputer tripComputer) { this.tripComputer = tripComputer; } @Override public void setGPSNavigator(GPSNavigator gpsNavigator) { this.gpsNavigator = gpsNavigator; } public Manual getResult() { return new Manual(type, seats, engine, transmission, tripComputer, gpsNavigator); }}
Cars
cars/Car.java: 汽车产品
package builder.cars;import builder.components.Engine;import builder.components.GPSNavigator;import builder.components.Transmission;import builder.components.TripComputer;/** * @author GaoMing * @date 2021/7/18 - 9:48 * Car is a product class */public class Car { private final CarType carType; private final int seats; private final Engine engine; private final Transmission transmission; private final TripComputer tripComputer; private final GPSNavigator gpsNavigator; private double fuel = 0; public Car(CarType carType, int seats, Engine engine, Transmission transmission, TripComputer tripComputer, GPSNavigator gpsNavigator) { this.carType = carType; this.seats = seats; this.engine = engine; this.transmission = transmission; this.tripComputer = tripComputer; if (this.tripComputer != null) { this.tripComputer.setCar(this); } this.gpsNavigator = gpsNavigator; } public CarType getCarType() { return carType; } public double getFuel() { return fuel; } public void setFuel(double fuel) { this.fuel = fuel; } public int getSeats() { return seats; } public Engine getEngine() { return engine; } public Transmission getTransmission() { return transmission; } public TripComputer getTripComputer() { return tripComputer; } public GPSNavigator getGpsNavigator() { return gpsNavigator; }}
cars/Manual.java: 手册产品
package builder.cars;import builder.components.Engine;import builder.components.GPSNavigator;import builder.components.Transmission;import builder.components.TripComputer;/** * @author GaoMing * @date 2021/7/18 - 9:56 * Car manual is another product. Note that it does not have the same ancestor as a Car. They are not related */public class Manual { private final CarType carType; private final int seats; private final Engine engine; private final Transmission transmission; private final TripComputer tripComputer; private final GPSNavigator gpsNavigator; public Manual(CarType carType, int seats, Engine engine, Transmission transmission, TripComputer tripComputer, GPSNavigator gpsNavigator) { this.carType = carType; this.seats = seats; this.engine = engine; this.transmission = transmission; this.tripComputer = tripComputer; this.gpsNavigator = gpsNavigator; } public String print() { String info = ""; info += "Type of car: " + carType + "\n"; info += "Count of seats: " + seats + "\n"; info += "Engine: volume - " + engine.getVolume() + "; mileage - " + engine.getMileage() + "\n"; info += "Transmission: " + transmission + "\n"; if (this.tripComputer != null) { info += "Trip Computer: Functional" + "\n"; } else { info += "Trip Computer: N/A" + "\n"; } if (this.gpsNavigator != null) { info += "GPS Navigator: Functional" + "\n"; } else { info += "GPS Navigator: N/A" + "\n"; } return info; }}
cars/CarType.java
package builder.cars;/** * @author GaoMing * @date 2021/7/18 - 9:51 */public enum CarType { CITY_CAR, SPORTS_CAR, SUV}
Components
components/Engine.java: 产品特征 1
package builder.components;/** * @author GaoMing * @date 2021/7/18 - 9:27 * feature of a car */public class Engine { private final double volume; private double mileage; private boolean started; public Engine(double volume, double mileage){ this.volume = volume; this.mileage = mileage; } public void on(){ started = true; } public void off(){ started = false; } public boolean isStarted(){ return started; } public void go(double mileage){ if(started){ this.mileage += mileage; }else { System.err.println("Cannot go(), you must start engine first!"); } } public double getVolume(){ return volume; } public double getMileage(){ return mileage; }}
components/GPSNavigator.java: 产品特征 2
package builder.components;/** * @author GaoMing * @date 2021/7/18 - 9:33 * Another feature of a car */public class GPSNavigator { private String route; public GPSNavigator(){ this.route = "221b, Baker Street, London to Scotland Yard, 8-10 Broadway, London"; } public GPSNavigator(String manualRoute){ this.route = manualRoute; } public String getRoute(){ return route; }}
components/Transmission.java: 产品特征 3
package builder.components;/** * @author GaoMing * @date 2021/7/18 - 9:35 * Another feature of a car */public enum Transmission { SINGLE_SPEED, MANUAL, AUTOMATIC, SEMI_AUTOMATIC}
components/TripComputer.java: 产品特征 4
package builder.components;import builder.cars.Car;/** * @author GaoMing * @date 2021/7/18 - 9:48 * Another feature of a car */public class TripComputer { private Car car; public void setCar(Car car){ this.car = car; } public void showFuellevel(){ System.out.println("Fuel level: " + car.getFuel()); } public void showStatus(){ if(this.car.getEngine().isStarted()){ System.out.println("Car is started"); }else { System.out.println("Car isn't started"); } }}
Director
director/Director.java: 主管控制生成器
package builder.director;import builder.cars.CarType;import builder.components.Engine;import builder.components.GPSNavigator;import builder.components.Transmission;import builder.components.TripComputer;import builder.builders.Builder;/** * @author GaoMing * @date 2021/7/18 - 10:08 * Director defines the order of building steps. It works with a builder object * through common Builder interface. Therefore it may not know what product is * being built. */public class Director { public void constructSportsCar(Builder builder) { builder.setCarType(CarType.SPORTS_CAR); builder.setSeats(2); builder.setEngine(new Engine(3.0, 0)); builder.setTransmission(Transmission.SEMI_AUTOMATIC); builder.setTripComputer(new TripComputer()); builder.setGPSNavigator(new GPSNavigator()); } public void constructCityCar(Builder builder) { builder.setCarType(CarType.CITY_CAR); builder.setSeats(2); builder.setEngine(new Engine(1.2, 0)); builder.setTransmission(Transmission.AUTOMATIC); builder.setTripComputer(new TripComputer(......原文转载:http://www.shaoqun.com/a/898405.html
跨境电商:https://www.ikjzd.com/
cicpa:https://www.ikjzd.com/w/1375
extra:https://www.ikjzd.com/w/1736
米谷:https://www.ikjzd.com/w/1788
1.意图 5.效果packagebuilder.builders;importbuilder.cars.CarType;importbuilder.cars.Manual;importbuilder.components.Engine;importbuilder.components.GPSNavigator;importbuilder.components.Transmission;impor
new old stock:https://www.ikjzd.com/w/2341
乐一番:https://www.ikjzd.com/w/1562
学生自助游几点经验 - :http://www.30bags.com/a/408037.html
学生族旅游盘点!学生族中最具影响力的十大国内旅游圣地:http://www.30bags.com/a/414776.html
学士服颜色分类 学士服颜色分别代表什么学历:http://www.30bags.com/a/425289.html
学习海南成都要尽力把客留住 :http://www.30bags.com/a/409576.html
在体育器材室被学长玩 坐在学长腰上动高H:http://lady.shaoqun.com/a/247425.html
交换第一次 夫妇野外交换全过程:http://lady.shaoqun.com/a/247063.html
深圳大芬油画村几点开门关门:http://www.30bags.com/a/525611.html
深圳大芬油画村油画价格(一般多少钱+便宜吗):http://www.30bags.com/a/525612.html
2021暑期深圳免费开放的体育场馆汇总:http://www.30bags.com/a/525613.html
性交中有养生的诀窍,关键在于掌握这几点!:http://lady.shaoqun.com/a/436471.html
No comments:
Post a Comment