• 静态工厂模式,抽象工厂模式,建造者模式


    静态工厂模式

    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
     

  • 相关阅读:
    从Matlab实例学习遗传算法
    二十六、【颜色调整】
    UE5 GAS 学习笔记 10.3 LyraStarter案例解析(中)
    c sharp基于随笔画的加解密系统
    雷尼绍探头编程 9724
    STM32实战总结:HAL之触摸屏
    人在国企,年薪30w,每天工作1小时,觉得没意思,要不要走?
    【Linux】Make/Makefile
    啊哈算法--堆排序 (python)
    StableSwarmUI:功能强大且易于使用的Stable Diffusion WebUI
  • 原文地址:https://blog.csdn.net/weixin_53150299/article/details/132783577