1.分析项目中变化部分与不变部分

2.多用组合,少用继承;用行为类组合,而不是行为的继承

案例:

第一步,把行为抽象为接口

package top.littlepage.designPattern.Strategy;

public interface FlyBehavior {
void fly();
}

第二步,把具体行为用实体类实现

package top.littlepage.designPattern.Strategy;

public class BadFly implements FlyBehavior{

    @Override
public void fly() {
System.out.println("--bad fly--");
} }
package top.littlepage.designPattern.Strategy;

public class NotFly implements FlyBehavior{

    @Override
public void fly() {
System.out.println("--not fly--");
} }
package top.littlepage.designPattern.Strategy;

public class GoodFly implements FlyBehavior{

    @Override
public void fly() {
System.out.println("--good fly--");
} }

第三步,做出具体的环境进行实现该实体

package top.littlepage.designPattern.Strategy;

public class Bird {
private String name;
private int age;
private GoodFly goodfly;
private BadFly badfly;
private NotFly notfly; public Bird() {
name="";
age=;
goodfly=new GoodFly();
badfly=new BadFly();
notfly=new NotFly();
}
public GoodFly getGoodfly() {
return goodfly;
}
public void setGoodfly(GoodFly goodfly) {
this.goodfly = goodfly;
}
public BadFly getBadfly() {
return badfly;
}
public void setBadfly(BadFly badfly) {
this.badfly = badfly;
}
public NotFly getNotfly() {
return notfly;
}
public void setNotfly(NotFly notfly) {
this.notfly = notfly;
}
public Bird(String name, int age) {
this.name = name;
this.age = age;
goodfly=new GoodFly();
badfly=new BadFly();
notfly=new NotFly();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} }

测试:

package top.littlepage.designPattern.Strategy;

public class Test {
public static void main(String[] args) {
Bird b=new Bird("小鸟",);
System.out.print(b.getName());
b.getGoodfly().fly(); System.out.print(b.getName());
b.getBadfly().fly(); System.out.print(b.getName());
b.getNotfly().fly();
}
}

控制台截图

策略模式(strategy)利用接口进行抽象,利用实体进行实现抽象,再做出环境进行实现具体的实体

在我们面向对象后,经常利用这种设计模式进行编程,应该是比较简单的一种模式

设计模式(二)策略模式(Strategy)的更多相关文章

  1. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

  2. 二十四种设计模式:策略模式(Strategy Pattern)

    策略模式(Strategy Pattern) 介绍定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法的变化可独立于使用它的客户. 示例有一个Message实体类,对它的操作有 ...

  3. 反馈法学习设计模式(一)——策略模式Strategy Pattern

    简介(Introduction) 之前学习Java8实战时,遇到一个很好的策略模式示例.便想着借着这个示例结合反馈式的方法来,学习策略设计模式,也以便后面反复琢磨学习. 首先我们通过练习,逐步写出符合 ...

  4. JAVA设计模式之策略模式 - Strategy

    在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改.这种类型的设计模式属于行为型模式. 在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 ...

  5. 8.6 GOF设计模式四: 策略模式… Strategy Pattern

    策略模式… Strategy Pattern  在POS系统中,有时需要实行价格优惠, 该如何处理?  对普通客户或新客户报全价  对老客户统一折扣5%  对大客户统一折扣10%  注:课件 ...

  6. [设计模式] 21 策略模式 Strategy

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对策略模式是这样说的:定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.该模式使得算法可独立于使用它的客户而变化. 策略模 ...

  7. 大熊君说说JS与设计模式之------策略模式Strategy

    一,总体概要 1,笔者浅谈 策略模式,又叫算法簇模式,就是定义了不同的算法,并且之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 策略模式和工厂模式有一定的类似,策略模式相对简单容易理解,并 ...

  8. 设计模式之策略模式Strategy

    /** * 策略设计模式 * 策略模式:定义一系列的算法族,使他们之间可以相互转换,动态的改变其行为. * 问题:设计一个鸭子模拟游戏. * 现在有一群鸭子: * ①这些鸭可以有飞的行为(分为快和慢) ...

  9. 大话设计模式之策略模式(strategy)

    策略模式:它定义了算法家族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化不会影响使用算法的用户. 针对商城收银模式,打折,返现促销等的例子: 打折还是促销其实都是一些算法,可以用工厂模式来 ...

  10. 设计模式 笔记 策略模式 Strategy

    //---------------------------15/04/28---------------------------- //Strategy 策略模式----对象行为型模式 /* 1:意图 ...

随机推荐

  1. Linux(64) 下 Tomcat + java 环境搭建

    查看 linux 系统位数 getconf LONG_BIT java  JDK下载地址: http://download.oracle.com/otn-pub/java/jdk/8u181-b13/ ...

  2. oracle goldengate 远程捕获和投递

    很早之前,OGG只支持部署在数据库主机上,这叫本地化部署.而现在OGG支持远端部署,即OGG软件不安装在数据库主机上,而是安装在单独的机器上,负责数据抽取和投递. 这样做的好处: l 易于管理 - 在 ...

  3. Failed to load ApplicationContext

    java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...

  4. Git clone 报错 Unable to negotiate with xxx.xxx.xxx.xxx port 12345: no matching cipher found. Their offer: aes128-cbc,3des-cbc,blowfish-cbc

    git clone 报错 Unable to negotiate with xxx.xxx.xxx.xxx. port 12345: no matching cipher found. Their o ...

  5. css基本知识、选择器

    CSS 是指层叠样式表 (Cascading Style Sheets),基本语法规则如下 CSS 由两个主要的部分构成:选择器,以及一条或多条声明 声明以大括号{ }括起来,一个申明包括属性和值,属 ...

  6. Centos 7 安装 Supervisor 及使用

    Supervisor官网链接:http://supervisord.org/installing.html 安装与设置开机启动: http://blog.csdn.net/fenglailea/art ...

  7. default activity not found的问题

    莫名其妙的同一个project下的所有modlue全都出现了这个问题,在网上查了一些解决方法,总结一下就是在运行时把default activity改成nothing,这个把活动都搞没了肯定不行.还有 ...

  8. 5.sql2008分组与嵌套

    1.Group by基本介绍;2.Having的使用;3.分组综合应用;4.子查询基本介绍;5.In/Exists/Any/Some/All;6.子查询综合应用; 1.Group by基本介绍:依据B ...

  9. Python3基础 list list()生成空列表

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  10. sudo中的 各类授权 名称包含的操作 权限命令?

    sudo是 do something as super user: 或者说: as Super User Do something: 就是 为 "非根用户赋予根用户的权限" 使用 ...