读书人

对策模式(Strategy)

发布时间: 2012-08-22 09:50:35 作者: rapoo

策略模式(Strategy)

策略模式(Strategy)

我很苦恼啊,UML图没发显示,我添加到附件了

定义

策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。

?

策略模式要点:

?

UML图

???????????????

这个《java设计模式》一书中的图例。

?

Context:应用上下文类,持有一个Strategy的对象

Strategy:策略的接口

ConcreteStrategy1,2:接口的实现类(即不同的可以相互替代的算法)

?

应用

报表中要使用不同的图案表示同样的数据。

package com.dp;

?

//Strategy

public interface DiagramStrategy {

?

????? void generateDiagram();

}

?

//ConcreteStrategy

public class CuboidDiagramStrategy implements DiagramStrategy {

?

????? @Override

????? public void generateDiagram() {

??????????? // TODO Auto-generated method stub

?

??????????? System.out.println("输出立方形图形(CuboidDiagramStrategy)");

????? }

?

}

?

public class CylinderDiagramStrategy implements DiagramStrategy {

?

????? @Override

????? public void generateDiagram() {

??????????? // TODO Auto-generated method stub

?

??????????? System.out.println("输出圆柱形图形(CylinderDiagram)");

????? }

?

}

?

//Context应用场景

?

public class ReportForm {

?

????? //生成报表的策略

????? private DiagramStrategy strategy;

?????

????? public ReportForm(DiagramStrategy strategy) {

??????????? super();

??????????? this.strategy = strategy;

????? }

?

????? //产生报表中的图形

????? public void generateDiagram(){

??????????? strategy.generateDiagram();

????? }

?????

?????

????? public DiagramStrategy getStrategy() {

??????????? return strategy;

????? }

????? //设置产生图形的策略

????? public void setStrategy(DiagramStrategy strategy) {

??????????? this.strategy = strategy;

????? }

?????

}

?

?

在java的api中Comparable和Comparator是Strategy的一个应用。

Package java.uitl;

public interface Comparator<T> {

??? int compare(T o1, T o2);

??? boolean equals(Object obj);

}

?

?

package java.lang;

?

public interface Comparable<T> {

??? public int compareTo(T o);

}

?

Comparator与Comparable的简单应用

?

?

UML图

?

?

即设计两个接口,Comparable和Comparator,有一个类Cat,它有两个属性Height,Weight,我们可以按照Height或者Weight给他们分辨大小,因此我们可以有两种算法。这样就可以使用Strategy类了。

?

/**

?* 按体重比较

?* @author Cody_408516

?*

?*/

public class CatWeightComparator implements Comparator<Cat> {

?

????? @Override

????? public int compare(Cat o1, Cat o2) {

??????????? if(o1.getWeight() > o2.getWeight()) return 1;

??????????? else if(o1.getWeight() < o2.getWeight()) return -1;

??????????? return 0;

????? }

?

}

?

import com.model.Cat;

/**

?* 按身高比较

?* @author Cody_408516

?*

?*/

public class CatHeightComparator implements Comparator<Cat> {

?

????? @Override

????? public int compare(Cat o1, Cat o2) {

??????????? if(o1.getHeight()> o2.getHeight()) return 1;

??????????? else if(o1.getHeight() < o2.getHeight()) return -1;

??????????? return 0;

????? }

?

}

?

?

public class Cat implements Comparable<Cat> {

?

????? //比较器

????? private Comparator<Cat> comparator;

????? //构造器

????? public Cat(float height, float weight) {

??????????? super();

//????????? this.comparator = comparator;

??????????? this.height = height;

??????????? this.weight = weight;

????? }

?

????? //猫的属性

????? private float height;

????? private float weight;

?????

????? @Override

????? public int compareTo(Cat o) {

??????????? //返回比较的值

??????????? return comparator.compare(this, o);

????? }

?

????? public float getHeight() {

??????????? return height;

????? }

?

????? public void setHeight(float height) {

??????????? this.height = height;

????? }

?

????? public float getWeight() {

??????????? return weight;

????? }

?

????? public void setWeight(float

读书人网 >软件架构设计

热点推荐