码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 静态工厂模式,抽象工厂模式,建造者模式


    静态工厂模式

    1. ublic class FruitFactory {
    2. public static Fruit getFruit(String name) {
    3. Fruit f=null;
    4. switch (name)
    5. {
    6. case "APPLE":{
    7. f=new Apple();
    8. }
    9. case "BANANA":{
    10. f=new Banana();
    11. }
    12. default :{System.out.println("Unknown Fruit");}
    13. }
    14. return f;
    15. }
    16. }

    抽象工厂模式

     建造者模式

    1. public class MealBuilder {
    2. public Meal suMeal() {
    3. Meal m=new Meal();
    4. m.additeam(new SuBuger());
    5. m.additeam(new bing());
    6. return m;
    7. }
    8. public Meal rouMeal() {
    9. Meal m=new Meal();
    10. m.additeam(new bing());
    11. m.additeam(new RouBuger());
    12. m.additeam(new RouBuger());
    13. m.additeam(new RouBuger());
    14. return m;
    15. }
    16. public Meal haohua(){
    17. Meal m=new Meal();
    18. m.additeam(new bing());
    19. m.additeam(new RouBuger());
    20. m.additeam(new RouBuger());
    21. m.additeam(new SuBuger());
    22. return m;
    23. }
    24. }
    1. public class Meal {
    2. private ArrayList iteams=new ArrayList();
    3. public void additeam(iteam i) {
    4. iteams.add(i);
    5. }
    6. public double getCost() {
    7. return iteams.stream().mapToDouble(iteam::price).sum();
    8. }
    9. public void showItems() {
    10. iteams.stream().forEach(
    11. e->{
    12. System.out.println(e.name());
    13. e.packing().pack();
    14. System.out.println(e.price());
    15. }
    16. );
    17. }
    18. }
    1. public interface iteam {
    2. String name();
    3. Packing packing();
    4. double price();
    5. }
    1. public class Main {
    2. public static void main(String[] args) {
    3. MealBuilder builder=new MealBuilder();
    4. Meal haohua=builder.haohua();
    5. double cost=haohua.getCost();
    6. System.out.println("total"+cost);
    7. haohua.showItems();
    8. }
    9. }
    1. public abstract class ClodDrink implements iteam{
    2. @Override
    3. public Packing packing() {
    4. return new Bottle();
    5. }
    6. }
    1. public interface Packing {
    2. void pack();
    3. }
    1. public class Wapper implements Packing{
    2. @Override
    3. public void pack() {
    4. // TODO Auto-generated method stub
    5. System.out.println("用纸包装");
    6. }
    7. }
    1. public class bing extends ClodDrink{
    2. @Override
    3. public String name() {
    4. // TODO Auto-generated method stub
    5. return "北冰洋";
    6. }
    7. @Override
    8. public double price() {
    9. return 3.0;
    10. }
    11. }

    total15.5
    北冰洋
    用瓶子包装
    3.0
    肉的
    用纸包装
    5.0
    肉的
    用纸包装
    5.0
    素的
    用纸包装
    2.5
     

  • 相关阅读:
    迅为3568开发板Ubuntu 系统交叉编译 Qt 工程
    作战仿真试验理论体系研究
    java 版本企业招标投标管理系统源码+多个行业+tbms+及时准确+全程电子化
    Qt中显示摄像头数据(V4L2三)
    【茗创科技】如何看待心理学实验中的数据缺失?
    【Verilator】 1 简明教程
    Similarity and Matching of Neural Network Representations 论文阅读笔记
    为什么使用ray_tune对自己改进的yolov8模型调优时
    医疗产品设计的四个主要因素
    gabse 8a常见问题
  • 原文地址:https://blog.csdn.net/weixin_53150299/article/details/132783577
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号