package com.eyugame.modle; /** * 创建者模式 * * @author JYC506 * */ /*导演者*/ public class Director { IBuilder myBuilder=new MyBuilder(); public PhoneProduct createMiPhone() { myBuilder.setNameAndType("小米", "note"); myBuilder.des("售价2300"); return myBuilder.getPhoneProduct(); } public PhoneProduct createIPhone() { myBuilder.setNameAndType("苹果", "6Plus"); myBuilder.des("售价5600"); return myBuilder.getPhoneProduct(); } } /* 创建接口 */ interface IBuilder { void setNameAndType(String name, String type); void des(String des); PhoneProduct getPhoneProduct(); } /* 创建者 */ class MyBuilder implements IBuilder { PhoneProduct phoneProduct = new PhoneProduct(); @Override public void setNameAndType(String name, String type) { phoneProduct.setName(name); phoneProduct.setType(type); } @Override public void des(String des) { des = phoneProduct.getName() + phoneProduct.getType() + des; phoneProduct.setDes(des); } @Override public PhoneProduct getPhoneProduct() { return phoneProduct; } } /* 产品 */ class PhoneProduct { private String name; private String type; private String des; public void show(){ System.out.println(des); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getDes() { return des; } public void setDes(String des) { this.des = des; } } class TestBuilder{ public static void main(String[] args) { Director director=new Director(); PhoneProduct xiaoMi=director.createMiPhone(); xiaoMi.show(); PhoneProduct iPhone=director.createIPhone(); iPhone.show(); } }
时间: 2024-10-31 19:58:43