java-策略模式

     策略模式,又叫算法簇模式,就是定义了不同的算法族,并且之间可以互相替换,此模式让算法的变化独立于使用算法的客户。 

 

     策略模式的好处在于你可以动态的改变对象的行为。

 
     设计原则是把一个类中经常改变或者将来可能改变的部分提取出来,作为一个接口(c++z中可以用虚类),然后在类中包含这个对象的实例,这样类的实例在运行时就可以随意调用实现了这个接口的类的行为。下面是一个例子。 


      策略模式属于对象行为型模式,主要针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。通常,策略模式适用于当一个应用程序需要实现一种特定的服务或者功能,而且该程序有多种实现方式时使用。


    

     (策略模式静态图)

 

   策略模式中有三个对象:
(1)       环境对象:该类中实现了对抽象策略中定义的接口或者抽象类的引用。
(2)       抽象策略对象:它可由接口或抽象类来实现。
(3)       具体策略对象:它封装了实现同不功能的不同算法。
利用策略模式构建应用程序,可以根据用户配置等内容,选择不同有算法来实现应用程序的功能。具体的选择有环境对象来完成。采用这种方式可以避免由于使用条件语句而带来的代码混乱,提高应用程序的灵活性与条理性。


/* 
这是一个表现僧人和道士的程序,僧人光头,使用棍子做武器,道士长小胡子,使用拂尘作武器 
*/ 

public interface DisplayInte {
    public void display();

}

public interface WeaponInte {
    public void wuqi();
}

public class daoshiDisplayImpl implements DisplayInte {

    public void display() {
        // TODO Auto-generated method stub
        System.out.println("道士是有头发的!!!");
    }

}
public class heshangDisplayImpl implements DisplayInte {

    public void display() {
        // TODO Auto-generated method stub
        System.out.println("和尚没有头发的");
    }

}
public class daoshiWeaponImpl implements WeaponInte {

    public void wuqi() {
        // TODO Auto-generated method stub
            System.out.println("道士使用的武器是拂尘!!!");
    }

}
public class heshangWeaponImpl implements WeaponInte {

    public void wuqi() {
        // TODO Auto-generated method stub
        System.out.println("和尚拿的武器是法仗!!!");
    }

}
public class Daoshi extends Role {

    public Daoshi() {
        this.display=new daoshiDisplayImpl();
        this.weapon=new daoshiWeaponImpl();
        // TODO Auto-generated constructor stub
    }

}

public class Heshang extends Role {

    public Heshang() {
        this.display=new heshangDisplayImpl();
        this.weapon=new heshangWeaponImpl();
        // TODO Auto-generated constructor stub
    }

}


public class Role {
    public String name;
    public int age;
    public DisplayInte display;
    public WeaponInte weapon;
    public void display()
    {       
        display.display();
    }
    public void weapon()
    {
        weapon.wuqi();
    }
    public void changeWeapon()
    {
        if(this instanceof Daoshi)
        {
            weapon=new heshangWeaponImpl();
        }
        else
        {
            weapon=new daoshiWeaponImpl();
        }
    }
    
    
}

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Role oDaoshi=new Daoshi();
        Role oHeshang=new Heshang();
        oDaoshi.display();
        oDaoshi.weapon();
        oHeshang.display();
        oHeshang.weapon();
        oDaoshi.changeWeapon();
        oDaoshi.display();
        oHeshang.changeWeapon();
        oHeshang.display();
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值