设计模式——命令模式
1- 命令模式的定义
命令模式(Command Pattern)是一种数据驱动的设计模式,它属于行为型模式。请求以命令的形式包裹在对象中,并传给调用对象。调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该对象执行命令。
No BB,Show Code!
2- 命令模式实现代码
创建Command接口:
package designPattern.test.command;
/**
* 命令接口
*/
public interface Command<T> {
/**
* 命令执行方法
*/
void execute(T t);
}
定义命令Command操作的业务bean——Student,代码如下:
package designPattern.test.command;
import java.util.Date;
/**
* 学生类
*/
public class Student {
//学号
private Long id;
//姓名
private String name;
//性别:1-男,0-女
private Byte gender;
//出生日期
private Date birthday;
public Student(Long id, String name, Byte gender, Date birthday) {
this.id = id;
this.name = name;
this.gender = gender;
this.birthday = birthday;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Byte getGender() {
return gender;
}
public void setGender(Byte gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (id != null ? !id.equals(student.id) : student.id != null) return false;
if (name != null ? !name.equals(student.name) : student.name != null) return false;
if (gender != null ? !gender.equals(student.gender) : student.gender != null) return false;
return birthday != null ? birthday.equals(student.birthday) : student.birthday == null;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (gender != null ? gender.hashCode() : 0);
result = 31 * result + (birthday != null ? birthday.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", gender=" + gender +
", birthday=" + birthday +
'}';
}
}
创建接口的实现类,表示实际的命令:
InsertCommand:增加一条记录命令
package designPattern.test.command;
import com.google.common.base.Preconditions;
import java.util.List;
/**
* 表示绘制一个点的命令
*/
public class InsertCommand implements Command<Student> {
private final List<Student> list;
public InsertCommand(List<Student> list) {
this.list = list;
}
@Override
public void execute(Student student) {
Preconditions.checkNotNull(student);
Preconditions.checkNotNull(student.getId());
Preconditions.checkNotNull(student.getName());
Preconditions.checkNotNull(student.getGender());
Preconditions.checkNotNull(student.getBirthday());
System.out.println("模拟写入一条数据到student表中:" + student);
list.add(student);
}
}
DeleteCommand:根据Id删除一条记录命令
package designPattern.test.command;
import com.google.common.base.Preconditions;
import java.util.List;
/**
* 删除命令
*/
public class DeleteCommand implements Command<Student> {
private final List<Student> list;
public DeleteCommand(List<Student> list) {
this.list = list;
}
@Override
public void execute(Student student) {
Preconditions.checkNotNull(student);
Preconditions.checkNotNull(student.getId());
System.out.println("模拟删除student" + student);
for (Student stu : list) {
if (stu.getId().equals(student.getId())) {
list.remove(stu);
return;
}
}
}
}
UpdateCommand:根据id更新一条记录
package designPattern.test.command;
import com.google.common.base.Preconditions;
import org.apache.commons.collections4.CollectionUtils;
import java.util.List;
/**
* 修改命令:只能修改一条,根据id修改
*/
public class UpdateCommand implements Command<Student> {
private final List<Student> list;
public UpdateCommand(List<Student> list) {
this.list = list;
}
@Override
public void execute(Student student) {
Preconditions.checkNotNull(student);
Preconditions.checkNotNull(student.getId());
System.out.println("模拟修改student" + student);
if (CollectionUtils.isEmpty(list)) {
System.out.println("list为空");
return;
}
System.out.println("模拟修改操作" + student);
for (Student stu : list) {
if (stu.getId().equals(student.getId())) {
stu.setName(student.getName());
stu.setGender(student.getGender());
stu.setBirthday(student.getBirthday());
break;
}
}
}
}
SelectCommand:根据id查询记录命令
package designPattern.test.command;
import com.google.common.base.Preconditions;
import org.apache.commons.collections4.CollectionUtils;
import java.util.List;
/**
* 查询命令
*/
public class SelectCommand implements Command<Student> {
private final List<Student> list;
public SelectCommand(List<Student> list) {
this.list = list;
}
@Override
public void execute(Student student) {
Preconditions.checkNotNull(student);
Preconditions.checkNotNull(student.getId());
//只能根据学号查询
if (CollectionUtils.isEmpty(list)) {
System.out.println("List为空");
return;
}
for (Student stu : list) {
if (stu.getId().equals(student.getId())) {
System.out.println("查询出student:" + stu);
return;
}
}
System.out.println("未查询到" + student.getId() + "对应的student");
}
}
3- 测试
测试代码如下:
package designPattern.test.command;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 命令模式测试
*/
public class CommandTest {
@Test
public void testCommand() {
List<Student> studentList = new ArrayList<>();
new InsertCommand(studentList).execute(new Student(1001l, "aaa", (byte) 1, new Date()));
new InsertCommand(studentList).execute(new Student(1002l, "bbb", (byte) 0, new Date()));
new InsertCommand(studentList).execute(new Student(1003l, "ccc", (byte) 1, new Date()));
new InsertCommand(studentList).execute(new Student(1004l, "ddd", (byte) 0, new Date()));
new DeleteCommand(studentList).execute(new Student(1001l, null, null, null));
new UpdateCommand(studentList).execute(new Student(1002l, "eee", (byte) 0, new Date()));
new SelectCommand(studentList).execute(new Student(1003l, null, null, null));
System.out.println();
}
}
执行结果:
模拟写入一条数据到student表中:Student{id=1001, name='aaa', gender=1, birthday=Sat Mar 31 23:15:21 CST 2018}
模拟写入一条数据到student表中:Student{id=1002, name='bbb', gender=0, birthday=Sat Mar 31 23:15:21 CST 2018}
模拟写入一条数据到student表中:Student{id=1003, name='ccc', gender=1, birthday=Sat Mar 31 23:15:21 CST 2018}
模拟写入一条数据到student表中:Student{id=1004, name='ddd', gender=0, birthday=Sat Mar 31 23:15:21 CST 2018}
模拟删除studentStudent{id=1001, name='null', gender=null, birthday=null}
模拟修改studentStudent{id=1002, name='eee', gender=0, birthday=Sat Mar 31 23:15:24 CST 2018}
模拟修改操作Student{id=1002, name='eee', gender=0, birthday=Sat Mar 31 23:15:24 CST 2018}
查询出student:Student{id=1003, name='ccc', gender=1, birthday=Sat Mar 31 23:15:21 CST 2018}
命令模式的优点: 1、降低了系统耦合度。 2、新的命令可以很容易添加到系统中去