策略模式
2015-08-12 00:00
309 查看
如果在一个系统里面有许多类,它们之间的区别仅在于它们的行为,那么使用策略模式可以动态的让一个对象在许多行为中选择一种行为。如果系统需要动态地在几种算法中选择一种。那么这些算法可以包装到一个个的具体算法类里面,而这些算法类都是一个抽象算法类的子类。
下面例子采用策略模式实现加减乘除四种运算:
public interface Operation {
publicvoid op(double a,double b);
}
public class Add
3ff1
implements Operation {
public void op(double a, double b) {
double result = a + b;
System.out.println(result);
}
}
public class Sub implements Operation {
public void op(double a, double b) {
double result = a - b;
System.out.println(result);
}
}
public class Multi implements Operation {
public void op(double a, double b) {
double result = a * b;
System.out.println(result);
}
}
publicclass Div implements Operation {
publicvoid op(double a, double b) {
if(b != 0)
{
dou
3ff0
ble result = a/b;
System.out.println(result);
}
else
{
System.out.println("除0了!");
}
}
}
publicclass Calc {
public Add add = new Add();
public Sub sub = new Sub();
public Div div = new Div();
public Multi multi = new Multi();
}
publicclass test1 {
publicstaticvoid main(String[] args) {
Calc c = new Calc();
c.add.op(11,22);
c.sub.op(22,11);
c.div.op(33, 11);
c.multi.op(33, 33);
}
}
也可以改写成下面形式:
publicclass Calc {
Operation operation;
public Calc(Operation operation){
this.operation = operation;
}
publicvoid op(double a,double b){
operation.op(a, b);
}
}
publicclass test1 {
publicstaticvoid main(String[] args) {
Calc add = new Calc(new Add());
add.op(11,22);
Calc sub = new Calc(new Sub());
sub.op(22,11);
Calc div = new Calc(new Div());
div.op(33, 11);
Calc multi = new Calc(new Multi());
multi.op(33, 33);
}
}
上面代码在调用的时候需要用到new来创建一个实例,可以考虑与“工厂模式”结合来优化(单纯用工厂模式有个缺点:调用者要知道接口类和工厂类这两个类,看笔记工厂模式):
publicclass Param {
publicfinalstaticintadd = 1;
publicfinalstaticintsub = 2;
publicfinalstaticintmulti = 3;
publicfinalstaticintdiv = 4;
}
publicclass Calc {
Operation operation;
public Calc(int type){
switch(type){
case Param.add:
operation = new Add();
break;
case Param.sub:
operation = new Sub();
break;
case Param.multi:
operation = new Multi();
break;
case Param.div:
operation = new Div();
break;
&
7fe0
nbsp; }
}
publicvoid op(double a,double b){
operation.op(a, b);
}
}
publicclass test1 {
publicstaticvoid main(String[] args) {
Calc add = new Calc(Param.add);
add.op(11,22);
Calc sub = new Calc(Param.sub);
sub.op(22,11);
Calc div = new Calc(Param.div);
div.op(33, 11);
Calc multi = new Calc(Param.multi);
multi.op(33, 33);
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
下面例子采用策略模式实现加减乘除四种运算:
public interface Operation {
publicvoid op(double a,double b);
}
public class Add
3ff1
implements Operation {
public void op(double a, double b) {
double result = a + b;
System.out.println(result);
}
}
public class Sub implements Operation {
public void op(double a, double b) {
double result = a - b;
System.out.println(result);
}
}
public class Multi implements Operation {
public void op(double a, double b) {
double result = a * b;
System.out.println(result);
}
}
publicclass Div implements Operation {
publicvoid op(double a, double b) {
if(b != 0)
{
dou
3ff0
ble result = a/b;
System.out.println(result);
}
else
{
System.out.println("除0了!");
}
}
}
publicclass Calc {
public Add add = new Add();
public Sub sub = new Sub();
public Div div = new Div();
public Multi multi = new Multi();
}
publicclass test1 {
publicstaticvoid main(String[] args) {
Calc c = new Calc();
c.add.op(11,22);
c.sub.op(22,11);
c.div.op(33, 11);
c.multi.op(33, 33);
}
}
也可以改写成下面形式:
publicclass Calc {
Operation operation;
public Calc(Operation operation){
this.operation = operation;
}
publicvoid op(double a,double b){
operation.op(a, b);
}
}
publicclass test1 {
publicstaticvoid main(String[] args) {
Calc add = new Calc(new Add());
add.op(11,22);
Calc sub = new Calc(new Sub());
sub.op(22,11);
Calc div = new Calc(new Div());
div.op(33, 11);
Calc multi = new Calc(new Multi());
multi.op(33, 33);
}
}
上面代码在调用的时候需要用到new来创建一个实例,可以考虑与“工厂模式”结合来优化(单纯用工厂模式有个缺点:调用者要知道接口类和工厂类这两个类,看笔记工厂模式):
publicclass Param {
publicfinalstaticintadd = 1;
publicfinalstaticintsub = 2;
publicfinalstaticintmulti = 3;
publicfinalstaticintdiv = 4;
}
publicclass Calc {
Operation operation;
public Calc(int type){
switch(type){
case Param.add:
operation = new Add();
break;
case Param.sub:
operation = new Sub();
break;
case Param.multi:
operation = new Multi();
break;
case Param.div:
operation = new Div();
break;
&
7fe0
nbsp; }
}
publicvoid op(double a,double b){
operation.op(a, b);
}
}
publicclass test1 {
publicstaticvoid main(String[] args) {
Calc add = new Calc(Param.add);
add.op(11,22);
Calc sub = new Calc(Param.sub);
sub.op(22,11);
Calc div = new Calc(Param.div);
div.op(33, 11);
Calc multi = new Calc(Param.multi);
multi.op(33, 33);
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关文章推荐