這篇文章主要介紹“如何使用傳統(tǒng)建造者模式”,在日常操作中,相信很多人在如何使用傳統(tǒng)建造者模式問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何使用傳統(tǒng)建造者模式”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
10余年的會(huì)昌網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。網(wǎng)絡(luò)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整會(huì)昌建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“會(huì)昌網(wǎng)站設(shè)計(jì)”,“會(huì)昌網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
傳統(tǒng)建造者模式 由四種角色組成 1.產(chǎn)品類 2.建造者抽象類 3.建造者類 4.Director指揮者角色.
Builder知道怎么建造一個(gè)對(duì)象, Director可以自由發(fā)揮,指揮builder完成不同部件的建造.
鏈?zhǔn)浇ㄔ煺吣J?是傳統(tǒng)建造者模式的簡(jiǎn)化.
public class Computer { private String cpu; private String ram; public String getCpu() { return cpu; } public void setCpu(String cpu) { this.cpu = cpu; } public String getRam() { return ram; } public void setRam(String ram) { this.ram = ram; } @Override public String toString() { return "Computer{" + "cpu='" + cpu + '\'' + ", ram='" + ram + '\'' + '}'; } }
public interface Builder { void buildCpu(); void buildRam(); Computer getComputer(); }
public class LenovoBuilder implements Builder { private Computer computer; public LenovoBuilder() { computer = new Computer(); } @Override public void buildCpu() { computer.setCpu("聯(lián)想cpu"); } @Override public void buildRam() { computer.setRam("聯(lián)想ram"); } @Override public Computer getComputer() { return computer; } }
public class AppleBuilder implements Builder{ private Computer computer; public AppleBuilder() { computer = new Computer(); } @Override public void buildCpu() { computer.setCpu("蘋果cpu"); } @Override public void buildRam() { computer.setRam("蘋果ram"); } @Override public Computer getComputer() { return computer; } }
public class Director { public Computer buildAComputer(Builder builder){ builder.buildRam(); builder.buildCpu(); return builder.getComputer(); } }
public static void main(String[] args) { Director director = new Director(); LenovoBuilder lenovoBuilder = new LenovoBuilder(); AppleBuilder appleBuilder = new AppleBuilder(); Computer lenovoComputer = director.buildAComputer(lenovoBuilder); Computer appleComputer = director.buildAComputer(appleBuilder); System.out.println(lenovoComputer); System.out.println(appleComputer); }
到此,關(guān)于“如何使用傳統(tǒng)建造者模式”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!