设计模式之行为型

行为型模式

行为模式的四种分类

1.策略模式
策略模式定义了一系列算法,并将每个算法封装起来,使他们可以相互替换,且算法的变化不会影响到使用算法的客户,算法写好,客户只需要调用

1
2
3
public interface Calculator{
public double calculate(double left,double right);
}
1
2
3
4
5
public class Add implements Calculator{
public double calculate(double left,double right){
return left+right;
}
}
1
2
3
4
5
public class Mul implements Calculator{
public double calculate(double left,double right){
return left*right;
}
}
1
2
3
4
5
6
public static void main(String[] args){
Calculator c = new Add();
double result1 = c.calculate(1,2);
c = new Mul();
double result2 = c.calculate(1,2);
}

2.模板方法模式
一个抽象类中,有一个主方法,再定义1…n个方法,可以是抽象的,也可以是实际的方法,定义一个类,继承该抽象类,重写抽象方法,通过调用抽象类,实现对子类的调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public abstract class Calculator{
public double calculate(double left,double right,String mode){
double result;
if("+".equals(mode)){
result = Add(left,right);
}else if(...){
...
}else if(...){
...
}else if(...){
...
}
return result;
}
publiv abstract double Add(double left,double right);
publiv abstract double Sub(double left,double right);
...
...
}

1
2
3
4
5
6
7
8
9
10
public class MyCalculator extends Calculator{
publiv double Add(double left,double right){
return left+right;
}
publiv double Sub(double left,double right){
return left-right;
}
...
...
}
1
2
3
4
5
6
public static void main(String[] args){
Calculator c = new MyCalculator();
//调用抽象类的方法,抽象类方法中再调用实现类的方法
double result1 = c.calculate(1,2,"+");
double result2 = c.calculate(1,2,"-");
}

3.观察者模式
当一个对象变化时,其它依赖该对象的对象都会收到通知,并且随着变化

1
2
3
4
5
public class Person implements Observer{
public void getMilk(){
System.out.print("receive milk");
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class MilkStore{
private List<Person> customers = new ArratList<Person>();
public void addCustom(Person person){
customers.add(person);
}
public void deleteCustom(Person person){
customers.remove(person);
}
public void makeMilk(){
//挤牛奶
}
public void sendMilk(){
for(int i =0;i<customers.length;i++){
Person customer = customers.get(i);
customer.getMilk();
}
}
}
1
2
3
4
5
6
7
public static void main(String[] args){
MilkStore m = new MilkStore();
m.add(new Person("张三"));
m.add(new Person("李四"))
m.makeMilk();
m.sendMilk();
}

4.迭代子模式
顺序访问聚集中的对象,如List集合

1
2
3
4
5
6
7
8
9
10
11
12
public class School implements Collection{
public Student students[] = new Student[20];
public Iterator iterator() {
return new StudentID(this);
}
public Student get(int i) {
return students[i];
}
public int size() {
return students.length;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class StudentID implements Iterator{
private School s;
private int pos = -1;
public StudentID(School s){
this.s = s ;
}
public String getPreviousStudentID(){
pos--;
if(0=<pos&&pos<s.size())
return s.get(pos).getStudentID();
return null;
}
public String getNextStudentID(){
pos++;
if(0=<pos&&pos<s.size())
return s.get(pos).getStudentID();
return null;
}
}

5.责任链模式
有多个对象,每个对象持有对下一个对象的引用,这样就会形成一条链,请求在这条链上传递,直到某一对象决定处理该请求(发出者不清楚最终那个对象会处理)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Handler{
private Handler nextHandler;
private String name;
public Handler(String name){
this.name = name;
}
public void setNextHandler(Handler handler){
this.handle r= handler;
}
public Handler getNextHandler(){
return handler;
}
public void solve(){
System.out.println(name+"is solving!");
if(getNextHandler!==null){
handler.solve());
}
}
}

1
2
3
4
5
6
7
8
public static void main(String[] args){
Handler h1 = new Handler();
Handler h2 = new Handler();
Handler h3 = new Handler();
h1.setNextHandler(h2);
h2.setNextHandler(h3);
h1.solve(); //h1 h2 h3依次处理
}

6.命令模式
实现命令的发出者和执行者之间解耦,实现请求和执行分开

1
2
3
4
5
6
7
8
9
public class Command{
private Soilder s;
public Command(Soilder s){
this.s = s;
}
public void execute(String message){
s.receive(message);
}
}

1
2
3
4
5
6
7
8
9
10
public class Commander{
private Command command;
public Commander(Command command) {
this.command = command;
}
public void makeCommand(String message){
command.execute(message);
}
}
1
2
3
4
5
public class Soilder{
public void receive(String message){
System.out.print("receive message:"+message);
}
}
1
2
3
4
5
6
public static void main(String[] args){
Soilder s = new Soilder("王二");
Command c = new Command(s);
Commander com = new Commander(c);
com.makeCommand("Attack!");
}

7.备忘录模式
保存一个对象的某个状态,以便在适当的时候恢复对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Blackboard{
private String message;
private void setMessage(String message){
this.message = message;
}
private String getMessage(){
return message;
}
public Record createRecord(){
return new Record(message);
}
publiv void restoreRecord(Record record){
this.message = record.getMessage();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Record{
private String message;
public Record(String message){
this.message = message;
}
private void setMessage(String message){
this.message = message;
}
private String getMessage(){
return message;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
public class Storage{
private Record record;
public Record(Record record){
this.record = record;
}
private void setRecord(Record record){
this.record = record;
}
private String getRecorde(){
return record;
}
}
1
2
3
4
5
6
7
public static void main(String[] args){
Blackboard b = new Blackboard("Today's homework is ......");
Storage s = new Storage(b.createRecord());
b.setMessage("No homework");
//恢复
b.restoreRecord(s.getRecord());
}

8.状态模式
当对象的状态改变时,同时改变其行为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class State{
private String value;
public void setValue(String value){
this.value = value;
}
public void getValue(){
return value;
}
public void method1){
}
public void method2(){
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Context{
private State state;
public Context(State state){
this.state = state;
}
public void method(){
if(state.equals("1")){
sate.method1();
}else if(state.equals(2)){
state.method2();
}
}
}
1
2
3
4
5
6
7
public static void main(String[] args){
State s = new State("1");
Context c = new Context(s);
c.method();
s.setValue("2");
c.method();
}

9.访问者模式

1
2
3
4
5
6
7
8
9
10
public class Visitor{
//持有被访问的对象
private School s;
public Visitor(School s){
this.s =s ;
}
public void visit(){
System.out.print("Visit:"+s.getName());
}
}

1
2
3
4
5
6
7
8
9
public class School{
private String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
}

10.中介者模式
用一个中介对象来封装一系列的对象交互,使各对象不需要显式地相互引用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(String name){
this.name = name;
}
public void sendMessage(String message){
ChatRoom.showMessage(this,message);
}
}

1
2
3
4
5
public class ChatRoom{
public static void showMessage(User sender,String message){
System.out.print(sender.getName()+":"+message;)
}
}

11.解释器模式
正则表达式

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
public class Context {
private int num1;
private int num2;
public Context(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}
public int getNum1() {
return num1;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public int getNum2() {
return num2;
}
public void setNum2(int num2) {
this.num2 = num2;
}
}

1
2
3
4
5
public class Minus{
public int interpret(Context context) {
return context.getNum1()-context.getNum2();
}
}
1
2
3
4
public static void main(String[] args){
Context c = new Context(1,2);
int result = new Minus().interpret(c);
}