设计模式-行为型模式总结实例(二)(一)

2014-11-24 03:22:13 · 作者: · 浏览: 0

命令模式

将一个请求封装为一个对象,从而使你可以用不同的请求对客户进行参数化,对请求排队和记录请求日志,以及支持可撤销的操作。

package com.ruishenh.designPatter.action.command;
 
public class CommandClient {
   public static void main(String[] args) {
      Lead lead=new Lead();
      Staff staff=new Staff();
      ConcreteCommand command =new ConcreteCommand(staff);
      lead.setCommand(command);
      lead.action();
      //这一块来看的话,比如一些细节操作可能还需要变动暂时无法定下来,可以留着接口的回调函数最后来写。
      lead.setCommand(new ICommand() {
        @Override
        public void execute() {
        System.out.println("员工-有意见了,决定不干了。");
        }
      });
      lead.action();
     
   }
}
interface ICommand{
   public void execute(); 
}
class ConcreteCommand implements ICommand{
  
   Staff staff;
  
    public ConcreteCommand(Staff staff){ 
           this.staff = staff; 
       } 
   @Override
   public void execute() {
      staff.doSomething();
   }
  
}
//命令发起者
class Lead{
  
   private ICommand command; 
  
    public void setCommand(ICommand command) { 
        this.command = command; 
    } 
    public void action(){
        this.command.execute(); 
    }
}
//命令操作者
class Staff {
   public void doSomething() {
      System.out.println("员工-干活");
   }
}


命令模式的核心任务把执行命令封装成一个对象传送给发送命令者,命令者直接执行操作。某种意义上解决了行为的操作和发起之间的解耦。

在编码上边有时候感觉就是把一些命令或者是未知的命令留着开放下来,然后在后续去完成。

状态模式

允许对象在其内部状态改变时改变他的行为。对象看起来似乎改变了他的类。

package com.ruishenh.designPatter.action.state;
 
 
public class StateClient {
   public static void main(String[] args) {
      Washer washer =new Washer();
     
      washer.setState(new PowerWashState());
      washer.action(null);
     
      //切换状态
      washer.setState(new StartingWashState());
      washer.action(null);
   }
 
}
interface WashState{
   void Handler(String clothes);
}
class PowerWashState implements WashState{
   public void Handler(String clothes) {
      System.out.println("洗衣机电源打开");
   }
}
class StartingWashState implements WashState{
   public void Handler(String clothes) {
      System.out.println("洗衣机启动洗衣服");
   }
}
class WaterInWashState implements WashState{
   public void Handler(String clothes) {
      System.out.println("洗衣机注水中...");
   }
}
class WashingWashState implements WashState{
   public void Handler(String clothes) {
      System.out.println("洗衣机洗衣中...");
   }
}
class WaterOutWashState implements WashState{
   public void Handler(String clothes) {
      System.out.println("洗衣机放水中...");
   }
}
class DehydrationWashState implements WashState{
   public void Handler(String clothes) {
      System.out.println("洗衣机脱水中...");
   }
}
class ClosedWashState implements WashState{
   public void Handler(String clothes) {
      System.out.println("洗衣机洗衣完毕..提示.");
   }
}
//洗衣机
class Washer{
  
   WashState state;
  
   public void setState(WashState state) {
      this.state = state;
   }
   void action(String p1){
      state.Handler(p1);
   }
}


状态模式的核心就是通过一个状态的改变,而随时改变对应的状态行为。个人理解对于扩展较多且每个状态处理的逻辑可能比较复杂的时候可以选择使用此模式。否则,可能会产生很对的状态子类。维护性变的复杂

策略模式

定义一系列的算法,把他们一个个封装起来,并使他们可以互相替换,本模式使得算法可以独立于使用它们的客户。

package com.ruishenh.designPatter.action.strategy;
 
import java.math.BigDecimal;
 
public class StrategyClient {
 
   public static void main(String[] args) {
      DiscountContext context =new DiscountContext(null);
      System.out.println("小明买了一件衣服原价:100元");
      System.out.println("现价:"+context.getDiscountPrice(100));
     
      System.out.println("由于小明是会员打九五折,所以");
      context.setStrategy(new VipDiscount(0.95));
      System.out.println("现价:"+context.getDiscountPrice(100));
     
      System.out.println("由于小明是又买了一件衣服不能用会员,但是却参加了满100-20的活动所以");
      context.setStrateg