Java设计模式之策略模式定义与用法详解

2025-05-29 0 54

本文实例讲述了Java策略模式定义与用法。分享给大家供大家参考,具体如下:

一. 定义:

定义一系列算法,把他们一个一个封装起来,并且使他们可以相互替换.

二. 优点:

(1)上下文(Context)和具体策略(ConcreteStrategy)是松耦合关系,因此上下文只需要知道他要使用某一个实现 Strategy接口类的实例,但不需要知道是哪个类.
(2)策略模式满足开闭原则,当增加新的具体类时,不需要修改上下文类的代码,上下文即可以引用新的具体策略的实例.

三. 实例:

下面就通过一个问题来详细解释一下策略模式.

实验要求:

小丁是阿里巴巴公司程序员,这天项目经理给他一个类定义

?

1

2

3

4

5
public class Worker {

int id;

String name;

int age;

}

小丁一看,这不是一个工人的定义吗,包括编号id,姓名,年龄什么的。

经理说,我需要你写一个功能,能找出一批工人中年龄最大的一位。方法声明是(或者类似)这样的:

?

1
Worker searchWorker(List<Worker> workers);

参数List<Worker> workers是这批工人数据。如果查到了年龄最大的工人,则返回这个Worker对象。如workers没有节点时,返回null。
答案:

1. 策略

策略模式中,这个接口被命名为WorkStrategy (在具体问题中,这个名字可以根据具体问题命名),代码如下:

WorkStrategy .java

?

1

2

3

4
import java.util.List;

public interface WorkStrategy {

public Worker AgeAverage(List<Worker> list);

}

2. 上下文

上下文面向策略,既是面向接口的类;代码如下:

MaxAge.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
import java.util.List;

public class MaxAge {

WorkStrategy workstrategy;

public void SetWorkStrategy(WorkStrategy workstrategy){

this.workstrategy=workstrategy;

}

public Worker getFindMax(List<Worker> list){

if(workstrategy!=null)

return workstrategy.AgeAverage(list);

else{

return null;

}

}

}

3. 具体策略

具体策略是实现WorkStrategy接口的类,即必须重写接口中的abstract Worker
AgeAverage(List<Worker> list)方法

代码如下:

StrategyMaxAge.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
import java.util.List;

public class StrategyMaxAge implements WorkStrategy{

@Override

public Worker AgeAverage(List<Worker> list) {

int fs[] = new int[list.size()];

int maxage = 0;

for (int j = 0; j < list.size(); j++) {

fs[j]=list.get(j).getAge();

if (fs[maxage] <= fs[j]) {

maxage = j;

}

}

return list.get(maxage);

}

}

4. 策略模式的使用

Application.java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48
import java.util.ArrayList;

import java.util.List;

public class Application {

public static void main(String[] args) {

List<Worker> list = new ArrayList<>();

list.add(new Worker(1, "张三", 30));

list.add(new Worker(2, "李四", 40));

list.add(new Worker(3, "王五", 33));

MaxAge findage = new MaxAge();

findage.SetWorkStrategy(new StrategyMaxAge());

Worker findw = findage.getFindMax(list);

System.out.println("快网idc测试结果:");

System.out.println("年龄最大的工人:"+findw.getId()+" "+findw.getName()+" "+findw.getAge());

}

}

class Worker{

int id;

String name;

int age;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

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;

}

public Worker(int id, String name, int age) {

super();

this.id = id;

this.name = name;

this.age = age;

}

public Worker() {

super();

// TODO Auto-generated constructor stub

}

}

5. 运行结果:

Java设计模式之策略模式定义与用法详解

希望本文所述对大家java程序设计有所帮助。

原文链接:http://blog.csdn.net/yang_tang/article/details/78240531

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java设计模式之策略模式定义与用法详解 https://www.kuaiidc.com/112584.html

相关文章

猜你喜欢
发表评论
暂无评论