建造者模式例题
创始人
2025-01-08 00:04:15
0

        假定现在有这样一个需求:电脑可以由主板、硬盘、CPU、内存、显卡、显示器和键盘等元素构成,华硕公司可以生产里面的各种元素,Lenovo公司也可以生产里面的各种元素。假定你现在就想要一台电脑,这个电脑可以全部是来自华硕的品牌机,也可以是来自Lenovo的品牌机,也可以一部分元素来自华硕、另一部分来自Lenovo,请根据上述要求对代码进行设计,给出设计思路和源码。

        以下这段代码展示了一个简单的电脑组装系统。首先定义了不同的电脑元素接口,如主板、硬盘、CPU等。然后定义了电脑类 Computer,包含各种电脑组件,并通过 set 方法设置各个组件,最后通过 showInfo 方法展示电脑信息。

        接着有一个电脑建造者类 ComputerBuilder,其中包含了建造不同品牌电脑的方法,如 buildHSComputer(华硕电脑)、buildLenovoComputer(联想电脑)、buildCustomComputer(混合电脑),最后通过 getComputer 方法获取组装好的电脑。

        最后是 Director 类,包含了导演的角色,负责调用建造者类中的方法来组装不同品牌的电脑,并展示电脑的信息。

电脑元素接口:

//电脑元素接口 interface IMotherboard {      void makeIMotherboard();  }  interface IHardDisk {      void makeIHardDisk();  }  interface ICPU {      void makeICPU();  }  interface IMemory {      void makeIMemory();  }  interface IGraphicsCard {      void makeIGraphicsCard();  }  interface IDisplay {      void makeIDisplay();  }  interface IKeyboard {      void makeIKeyboard();  }  

电脑类

//电脑 public class Computer {     private IMotherboard motherboard;      private IHardDisk hardDisk;      private ICPU cpu;      private IMemory memory;      private IGraphicsCard graphicsCard;      private IDisplay display;      private IKeyboard keyboard;      public void setMotherboard(IMotherboard motherboard) {          this.motherboard = motherboard;      }      public void setHardDisk(IHardDisk hardDisk) {          this.hardDisk = hardDisk;      }      public void setCpu(ICPU cpu) {          this.cpu = cpu;      }      public void setMemory(IMemory memory) {          this.memory = memory;      }      public void setGraphicsCard(IGraphicsCard graphicsCard) {          this.graphicsCard = graphicsCard;      }      public void setDisplay(IDisplay display) {          this.display = display;      }      public void setKeyboard(IKeyboard keyboard) {          this.keyboard = keyboard;      }      public void showInfo() {          if (motherboard != null) {              motherboard.makeIMotherboard();          }          if (hardDisk != null) {              hardDisk.makeIHardDisk();          }          if (cpu != null) {              cpu.makeICPU();          }          if (memory != null) {              memory.makeIMemory();          }          if (graphicsCard != null) {              graphicsCard.makeIGraphicsCard();          }          if (display != null) {              display.makeIDisplay();          }          if (keyboard != null) {              keyboard.makeIKeyboard();          }      }  }  }

电脑建造者类

//电脑建造者 class ComputerBuilder {      private Computer computer;      public ComputerBuilder() {          computer = new Computer();      }      public void buildHSComputer() {//华硕电脑          computer.setMotherboard(new HSMotherboard());          computer.setHardDisk(new HSHardDisk());          computer.setCpu(new HSCPU());          computer.setMemory(new HSMemory());          computer.setGraphicsCard(new HSGraphicsCard());          computer.setDisplay(new HSDisplay());          computer.setKeyboard(new HSKeyboard());      }      public void buildLenovoComputer() {//联想电脑          computer.setMotherboard(new LenovoMotherboard());          computer.setHardDisk(new LenovoHardDisk());          computer.setCpu(new LenovoCPU());          computer.setMemory(new LenovoMemory());          computer.setGraphicsCard(new LenovoGraphicsCard());          computer.setDisplay(new LenovoDisplay());          computer.setKeyboard(new LenovoKeyboard());      }      public void buildCustomComputer() {//建造混合电脑          computer.setMotherboard(new HSMotherboard());          computer.setHardDisk(new LenovoHardDisk());          computer.setCpu(new LenovoCPU());          computer.setMemory(new HSMemory());          computer.setGraphicsCard(new LenovoGraphicsCard());          computer.setDisplay(new LenvovDisplay());          computer.setKeyboard(new HSKeyboard());      }      public Computer getComputer() {//获得组装好的电脑          return computer;      }  }

导演类

//导演类 class Director {     private ComputerBuilder builder = new ComputerBuilder();      public void buildHSComputer() {          // 华硕电脑          builder.buildHSComputer();          Computer hsComputer = builder.getComputer();          hsComputer.showInfo();      }      public void buildLenovoComputer() {          // 联想电脑          builder.buildLenovoComputer();          Computer lenovoComputer = builder.getComputer();          lenovoComputer.showInfo();      }      public void buildCustomComputer() {          // 混合电脑          builder.buildCustomComputer();          Computer customComputer = builder.getComputer();          customComputer.showInfo();      }  }  

相关内容

热门资讯

今年以来!老k麻将有挂吗,家乡... 今年以来!老k麻将有挂吗,家乡大二辅助,扑克教程(总是存在有挂)-哔哩哔哩老k麻将有挂吗辅助器中分为...
在玩家背景下!丫丫陕西打锅子辅... 在玩家背景下!丫丫陕西打锅子辅助,微信小程序雀神挂件,微扑克教程(切实真的有挂)-哔哩哔哩1、微信小...
现有关情况通报如下!同城游破解... 现有关情况通报如下!同城游破解版下载,海盗来了辅助器无限炮,攻略教程(一直真的有挂)-哔哩哔哩1、该...
这一现象值得深思!潮汕掌上辅助... 这一现象值得深思!潮汕掌上辅助挂定制交易平台,兴动互娱辅助工具,透明挂教程(切实真的是有挂)-哔哩哔...
连日来!新518互游脚本下载,... 连日来!新518互游脚本下载,多乐辅助,微扑克教程(好像真的有挂)-哔哩哔哩运新518互游脚本下载辅...
近日!四川辅助工具,潮汕掌上娱... 近日!四川辅助工具,潮汕掌上娱有破解版吗,德州教程(其实有挂)-哔哩哔哩1、近日!四川辅助工具,潮汕...
今天下午!大唐撸麻雀辅助码,福... 今天下午!大唐撸麻雀辅助码,福建天天开心辅助软件大全,科技教程(本来是有挂)-哔哩哔哩1、打开软件启...
据目击者称!福州十八扑外卦,新... 据目击者称!福州十八扑外卦,新畅游互娱辅助,技巧教程(确实是有挂)-哔哩哔哩新畅游互娱辅助是一种具有...
出乎意料的是!欢聚水鱼智能辅助... 出乎意料的是!欢聚水鱼智能辅助教程,博雅红河西元红河挂,教你教程(都是有挂)-哔哩哔哩欢聚水鱼智能辅...
最终!约战竞技场辅助器,邳州友... 最终!约战竞技场辅助器,邳州友友有没有辅助软件,wpk教程(都是真的有挂)-哔哩哔哩1、每一步都需要...