1.the bridge pattern(结构模式)
1)利用聚合,将抽象化(abstraction-superclass)与实现化(implementation-subclass)脱耦,使得两者可以独立变化
2)问题解决
A.解除接口和实现件的耦合
B.抽象化和实现化的层次结构可以独立扩展
3)解决
A.抽象化定义抽象接口
B.抽象化保存一个队实现化对象的引用
C.实现化给出实现化接口
D.具体实现化给出实现化接口的具体实现
interface DBManager {
public boolean saveObject(Object o);
public Object queryObject(String sql);
}
public class DBManagerNoSQL implements DBManager{
@Override
public boolean saveObject(Object o) {
System.out.println("nosql save object");
return true;
}
@Override
public Object queryObject(String sql) {
Object object = null;
System.out.println("nosql query object");
return object;
}
}
public class DBManagerSQL implements DBManager{
SQLImplementor impl;
@Override
public boolean saveObject(Object o) {
impl.saveRecord(o);
return true;
}
@Override
public Object queryObject(String sql) {
Object object = impl.getRecord(sql);
return object;
}
public void setImpl(SQLImplementor impl){
this.impl = impl;
}
}
interface SQLImplementor {
public boolean saveRecord(Object o);
public Object getRecord(String sql);
}
public class MySQLImplementer implements SQLImplementor {
@Override
public boolean saveRecord(Object o) {
System.out.println("mysql save object");
return true;
}
@Override
public Object getRecord(String sql) {
System.out.println("mysql query object");
return null;
}
}
public class SQListImplementor implements SQLImplementor{
@Override
public boolean saveRecord(Object o) {
System.out.println("sqlist save object");
return false;
}
@Override
public Object getRecord(String sql) {
System.out.println("sqlist query object");
return null;
}
}
public class Client {
public static void main(String []args){
DBManager dbManager;
dbManager = new DBManagerNoSQL();
dbManager.saveObject(null);
dbManager.queryObject(null);
dbManager = new DBManagerSQL();
SQLImplementor impl;
impl = new MySQLImplementer();
((DBManagerSQL)dbManager).setImpl(impl);
dbManager.saveObject(null);
dbManager.queryObject(null);
impl = new SQListImplementor();
((DBManagerSQL)dbManager).setImpl(impl);
dbManager.saveObject(null);
dbManager.queryObject(null);
}
}
nosql save object
nosql query object
mysql save object
mysql query object
sqlist save object
sqlist query object
2.the command pattern (行为模型)
1)把一个请求或操作封装到一个对象中
2)问题解决
A.将一个请求或操作封装到一个对象中
B.允许系统使用不同的请求把客户端参数化
C. 对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。
3)解决
A.命令声明一个给所有具体命令类的抽象接口,用于execute行为
B.具体命令继承命名,并实现execute方法
C.客户端创建一个具体命令对象
public abstract class SQLCMD {
Object result;
public abstract void execute();
}
public class SaveRecord extends SQLCMD{
public SaveRecord(Object o) {}
@Override
public void execute() {
System.out.println("save execute");
}
}
public class GetRecord extends SQLCMD{
public GetRecord (String sql) {}
@Override
public void execute() {
System.out.println("get execute");
}
}
public class UpdateRecord extends SQLCMD{
public UpdateRecord(Object o) {}
@Override
public void execute() {
System.out.println("update execute");
}
}
public class ConnectDB extends SQLCMD{
@Override
public void execute() {
System.out.println("connect db");
}
}
public class MySQLImplementor {
private SQLCMD cmd;
public void setCmd(SQLCMD cmd){
this.cmd =cmd;
}
public boolean SaveRecord(Object o){
setCmd(new SaveRecord(o));
cmd.execute();
return true;
}
public Object getRecord(String sql){
setCmd(new GetRecord(sql));
cmd.execute();
return null;
}
public boolean updateRecord(Object o){
setCmd(new UpdateRecord(o));
cmd.execute();
return true;
}
public void connectDB() {
setCmd(new ConnectDB());
cmd.execute();
}
}
public class Client {
public static void main(String []args){
MySQLImplementor implementor = new MySQLImplementor();
implementor.SaveRecord(null);
implementor.getRecord(null);
implementor.updateRecord(null);
implementor.connectDB();
}
}
save execute
get execute
update execute
connect db