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

静态工厂模式

ublic class FruitFactory {


	public static Fruit getFruit(String name) {
		Fruit f=null;
		switch (name)
		{
		case "APPLE":{
			f=new Apple();
		}
		case "BANANA":{
			f=new Banana();
		}
		default :{System.out.println("Unknown Fruit");}
		}
		return f;
		
	}
}

抽象工厂模式

建造者模式

public class MealBuilder {
public Meal suMeal() {
	Meal m=new Meal();
	m.additeam(new SuBuger());
	m.additeam(new bing());
	return m;
}
	public Meal rouMeal() {
		Meal m=new Meal();
		m.additeam(new bing());
		m.additeam(new RouBuger());
		m.additeam(new RouBuger());
		m.additeam(new RouBuger());
		return m;
	}
	public Meal haohua(){
		Meal m=new Meal();
		m.additeam(new bing());
		m.additeam(new RouBuger());
		m.additeam(new RouBuger());
		m.additeam(new SuBuger());
		return m;
	}
}

public class Meal {
private ArrayList<iteam> iteams=new ArrayList<iteam>();
public void additeam(iteam i) {
	iteams.add(i);
}
public double getCost() {

	return iteams.stream().mapToDouble(iteam::price).sum();
}
public void showItems() {
	iteams.stream().forEach(
		e->{
			System.out.println(e.name());
			e.packing().pack();
			System.out.println(e.price());
		}
	);
}
}

public interface iteam {
String name();
Packing packing();
double price();
} 


public class Main {
    public static void main(String[] args) {
        MealBuilder builder=new MealBuilder();
        Meal haohua=builder.haohua();
        double cost=haohua.getCost();
        System.out.println("total"+cost);
        haohua.showItems();
    }
}

public abstract class ClodDrink  implements iteam{
	@Override
	public Packing packing() {
		return new Bottle();
	}
}

public interface Packing {
 void pack();
}


public class Wapper implements Packing{
	

	@Override
	public void pack() {
		// TODO Auto-generated method stub
		System.out.println("用纸包装");
	}

}

public class bing extends ClodDrink{

	@Override
	public String name() {
		// TODO Auto-generated method stub
		return "北冰洋";
	}

	@Override
	public double price() {
		return 3.0;
	}

}

total15.5

北冰洋

用瓶子包装

3.0

肉的

用纸包装

5.0

肉的

用纸包装

5.0

素的

用纸包装

2.5

相关推荐
智慧老师12 分钟前
Spring基础分析13-Spring Security框架
java·后端·spring
lxyzcm14 分钟前
C++23新特性解析:[[assume]]属性
java·c++·spring boot·c++23
V+zmm101341 小时前
基于微信小程序的乡村政务服务系统springboot+论文源码调试讲解
java·微信小程序·小程序·毕业设计·ssm
Oneforlove_twoforjob1 小时前
【Java基础面试题025】什么是Java的Integer缓存池?
java·开发语言·缓存
xmh-sxh-13141 小时前
常用的缓存技术都有哪些
java
AiFlutter2 小时前
Flutter-底部分享弹窗(showModalBottomSheet)
java·前端·flutter
J不A秃V头A2 小时前
IntelliJ IDEA中设置激活的profile
java·intellij-idea
DARLING Zero two♡2 小时前
【优选算法】Pointer-Slice:双指针的算法切片(下)
java·数据结构·c++·算法·leetcode
小池先生2 小时前
springboot启动不了 因一个spring-boot-starter-web底下的tomcat-embed-core依赖丢失
java·spring boot·后端
CodeClimb3 小时前
【华为OD-E卷-木板 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od