最近学习+复习各种常用设计模式,看到一道题目http://www.iteye.com/topic/243309和http://topic.csdn.net/u/20091125/14/99c028d1-6cf0-4f82-b852-b94b84c4fb45.html,很有意思,不看不知道,面向对象真奇妙!
其他人怎么说,不如自己练,于是用了单例模式,策略模式和命令模式写了一个初版的微波炉,考虑了微波炉操作的时候不能开门。策略模式用于烹饪鱼和鸡等一套【高级】微波炉功能,命令模式用于“煮”,“煎”,“高火”,“中火”,“低火”等【基础】功能。
测试类CookFishTest:
package com.hui.test;
import java.util.ArrayList;
import java.util.List;
import com.hui.Machine;
import com.hui.food.Fish;
import com.hui.food.Food;
import com.hui.strategy.CookFishStrategy;
public class CookFishTest {
public static void main(String[] args){
CookFishTest cft = new CookFishTest();
System.out.println("正常测试开始……");
cft.regular();
System.out.println("正常测试结束……");
}
private void regular(){
Machine machine = Machine.getInstance();
machine.openDoor();
Fish fishA = new Fish();
List<Food> fishList = new ArrayList<Food>();
fishList.add(fishA);
machine.putFood(fishList);
machine.closeDoor();
machine.setTime(10);
machine.setCookStrategy(new CookFishStrategy(fishList));
machine.operate();
}
}
各种命令,以”煮“为例:
package com.hui.command;
public class BoilCommand implements Command {
public void execute() {
System.out.println("煮……");
}
}
各种策略,以烹饪鱼为例:
package com.hui.strategy;
import java.util.ArrayList;
import java.util.List;
import com.hui.command.BoilCommand;
import com.hui.command.Command;
import com.hui.command.FryCommand;
import com.hui.command.HighCommand;
import com.hui.command.LowCommand;
import com.hui.command.MiddleCommand;
import com.hui.food.Fish;
public class CookFishStrategy implements CookStrategy{
//这里使用List<Food>会出错,原因大抵是"苹果可以是水果的子类
//,但装苹果的袋子不是装水果的袋子的子类"
private List<?> fishList = new ArrayList<Fish>();
public CookFishStrategy(List<?> fishList){
this.fishList = fishList;
}
public void cook(){
Command boil = new BoilCommand();
boil.execute();
Command high = new HighCommand();
high.execute();
Command middle = new MiddleCommand();
middle.execute();
Command low = new LowCommand();
low.execute();
Command fry = new FryCommand();
fry.execute();
System.out.println("烹饪结束!可以取出食物!");
}
}
策略类的接口:
package com.hui.strategy;
public interface CookStrategy {
public void cook();
}
微波炉主类:
package com.hui;
import java.util.List;
import com.hui.food.Food;
import com.hui.strategy.CookStrategy;
public class Machine {
private CookStrategy cookStrategy;
private List<Food> foodList;
private static Machine instance;
private int minutes;
private byte[] lock = new byte[0];
private Machine(){
System.out.println("这有一个微波炉A……");
}
public static synchronized Machine getInstance(){
if(instance == null){
instance = new Machine();
}
return instance;
}
public void openDoor(){
synchronized(lock){
System.out.println("开门……");
}
}
public void closeDoor(){
System.out.println("关门……");
}
public void setTime(int minutes){
System.out.println("设置时间" + minutes + "分钟!");
this.minutes = minutes;
}
public void putFood(List<Food> foodList){
System.out.println("放食物……");
this.foodList = foodList;
}
public void setCookStrategy(CookStrategy cookStrategy) {
this.cookStrategy = cookStrategy;
}
public void operate(){
synchronized(lock){
cookStrategy.cook();
}
}
}