Create Mode -- Builder mode
程序员文章站
2022-05-19 13:38:19
...
1 建造者模式
2
interface Packing{
public String pack();
}
class Wrapper implements Packing{
@Override
public String pack() {
return "Wrapper";
}
}
class Bottle implements Packing{
@Override
public String pack() {
return "Bottle";
}
}
interface Item{
public String name();
public Packing pack();
public float price();
}
abstract class Hambur implements Item{
@Override
public Packing pack() {
return new Wrapper();
}
@Override
public abstract float price();
}
abstract class Colddrink implements Item{
@Override
public Packing pack() {
return new Bottle();
}
@Override
public abstract float price();
}
class Mcdonald extends Hambur{
@Override
public float price() {
return 1000.0f;
}
@Override
public String name() {
return "Mcdonald";
}
}
class KFC extends Hambur{
@Override
public float price() {
return 2000.0f;
}
@Override
public String name() {
return "KFC";
}
}
class Coca extends Colddrink{
@Override
public float price() {
return 100.0f;
}
@Override
public String name() {
return "Coca";
}
}
class Pepsi extends Colddrink{
@Override
public float price() {
return 200.0f;
}
@Override
public String name() {
return "Pepsi";
}
}
class Meal{
private List<Item> items = new ArrayList<Item>();
public void addItem(Item item){
items.add(item);
}
public float getPrice(){
float cost = 0.0f;
for (Item item : items){
cost += item.price();
}
return cost;
}
public void showItem(){
for (Item item : items){
System.out.println(item.name() + " " + item.price() + " ");
}
System.out.println(getPrice());
}
}
class MealBuilder{
public Meal prepareKFC(){
Meal meal = new Meal();
meal.addItem(new KFC());
meal.addItem(new Coca());
return meal;
}
public Meal preparePepsi(){
Meal meal = new Meal();
meal.addItem(new Mcdonald());
meal.addItem(new Pepsi());
return meal;
}
}
public class Test {
public static void main(String[] args){
MealBuilder mealBuilder = new MealBuilder();
Meal meal = mealBuilder.prepareKFC();
meal.showItem();
}
}
上一篇: 网鼎杯2020 青龙组 -web
下一篇: Yarn的两种deploy-mode模式
推荐阅读
-
mysql sql_mode="" 的作用说明
-
MySQL Installer is running in Community mode 的解决办法
-
Linux mkdir函数mode权限设置方法
-
在JavaScript中使用严格模式(Strict Mode)
-
关于MySQL innodb_autoinc_lock_mode介绍
-
Mysql-sql_mode
-
浅谈iOS开发如何适配暗黑模式(Dark Mode)
-
浅谈innodb_autoinc_lock_mode的表现形式和选值参考方法
-
Linux crontab报错:BAD FILE MODE的解决方法
-
python中如何正确使用正则表达式的详细模式(Verbose mode expression)