设计模式之雇工模式
- 23中模式之外的新模式
简介
雇工模式也叫做仆人模式:雇工模式是行为模式的一种,它为一组类提供通用的功能,而不需要类实现这些功能,他是命令模式的一种扩展。
类似于厨师、裁缝、园丁等都是一组类,具有清洁的能力,但是我们并没有实现,使用雇工模式,就是简化版的命令模式。让被服务对象实现具体的方法,使用雇工来干活
类图
代码
具有一组能力的对象,以及对应对象的实现
Iserviced1
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
49
50package com.bj.hz.hire;
/**
* Created with hzz
* Description:
*
* @author: huangzhe
* @date: 2018-07-19
* @time: 9:08 PM
*/
public interface Iserviced {
public void cleaned();
}
package com.bj.hz.hire;
/**
* Created with hzz
* Description:
*
* @author: huangzhe
* @date: 2018-07-19
* @time: 9:15 PM
*/
public class Garden implements Iserviced {
@Override
public void cleaned() {
System.out.println("花园被打扫了");
}
}
package com.bj.hz.hire;
/**
* Created with hzz
* Description:
*
* @author: huangzhe
* @date: 2018-07-19
* @time: 9:17 PM
*/
public class Kitchen implements Iserviced {
@Override
public void cleaned() {
System.out.println("厨房被打扫了");
}
}
雇工 Servant1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package com.bj.hz.hire;
/**
* Created with hzz
* Description:
*
* @author: huangzhe
* @date: 2018-07-19
* @time: 9:14 PM
*/
public class ServantHire {
public void clean(Iserviced serviced){
serviced.cleaned();
}
}
场景类
Cient1
2
3
4
5
6
7
8
9
10
11
12
13
14
15package com.bj.hz.hire;
/**
* Created with hzz
* Description:
*
* @author: huangzhe
* @date: 2018-07-19
* @time: 9:15 PM
*/
public class Client {
public static void main(String[] args) {
}
}