适配模式
1 一个接口有好多方法,但是一个类要实现他,但是只要实现其中的一两个方法就能搞定
那么就定义一个抽象类来实现它
然后继承这个抽象类,来重写里面的两个方法,就搞定了
interface Window {// 定义Window窗口接口,表示窗口操作
public void open();// 窗口打开
public void close();// 窗口关闭
public void iconified();// 窗口最小化
public void deiconified();// 窗口恢复
public void activated();// 窗口活动
}
// 定义抽象类实现接口,在此类中覆写方法,但是所有的方法体为空
abstract class WindowAdapter implements Window {
public void open() {
};// 窗口打开
public void close() {
};// 窗口关闭
public void iconified() {
};// 窗口最小化
public void deiconified() {
};// 窗口恢复
public void activated() {
};// 窗口活动
}
// 子类继承WindowAdapter抽象类,选择性实现需要的方法
class WindowImpl extends WindowAdapter {
public void open() {
System.out.println("窗口打开");// 实现open()方法
}
public void close() {
System.out.println("窗口关闭");// 实现close()方法
}
}
2 策略模式Strategy模式)
有一个文件,我们需要读取后,希望替代其中相应的变量,然后输出。
关于替代其中变量的方法可能有多种方法,这取决于用户的要求,所以我们要准备几套变量字符替代方案。
public abstract class RepTempRule{
protected String oldString="";
public void setOldString(String oldString){
this.oldString=oldString;
}
protected String newString="";
public String getNewString(){
return newString;
}
public abstract void replace() throws Exception;
}
在RepTempRule中 有一个抽象方法abstract需要继承明确,这个replace里其实是替代的具体方法
我们现在有两个字符替代方案:
1将文本中aaa替代成bbb;
2将文本中aaa替代成ccc。
这时我们对应的类分别是RepTempRuleOne和RepTempRuleTwo:
public class RepTempRuleOne extends RepTempRule{
public void replace() throws Exception{
//replaceFirst是jdk1.4新特性
newString=oldString.replaceFirst("aaa", "bbbb")
System.out.println("this is replace one");
}
public class RepTempRuleTwo extends RepTempRule{
public void replace() throws Exception{
newString=oldString.replaceFirst("aaa", "ccc")
System.out.println("this is replace Two");
}
}
第二步:我们要建立一个算法解决类,用来提供客户端可以自由选择算法。
public class RepTempRuleSolve {
private RepTempRule strategy;
public RepTempRuleSolve(RepTempRule rule){
this.strategy=rule;
}
public String getNewContext(Site site,String oldString) {
return strategy.replace(site,oldString);
}
public void changeAlgorithm(RepTempRule newAlgorithm) {
strategy = newAlgorithm;
}
}
public class test{
......
public void testReplace(){
//使用第一套替代方案
RepTempRuleSolve solver=new RepTempRuleSolve(new RepTempRuleSimple());
solver.getNewContext(site,context);
//使用第二套
solver=new RepTempRuleSolve(new RepTempRuleTwo());
solver.getNewContext(site,context);
}
.....
}
装饰模式(Decorator模式)
我们通常可以使用继承来实现功能的拓展,如果这些需要拓展的功能的种类很繁多,那么势必生成很多子类,
同时,使用继承实现功能拓展,我们必须可预见这些拓展功能,这些功能是编译时就确定了,是静态的。
这些功能需要由用户动态决定加入的方式和时机。Decorator提供了"即插即用"的方法,在运行期间决定何时增加何种
举Adapter中的打桩示例,在Adapter中有两种类:方形桩 圆形桩,Adapter模式展示如何综合使用这两个类,
在Decorator模式中,我们是要在打桩时增加一些额外功能,比如,挖坑 在桩上钉木板等,不关心如何使用两个不相关的类。
public interface Work{
public void insert();
}
public class SquarePeg implements Work{
public void insert(){
System.out.println("方形桩插入");
}
}
public class Decorator implements Work{
private Work work;
//额外增加的功能被打包在这个List中
private ArrayList others = new ArrayList();
//在构造器中使用组合new方式,引入Work对象;
public Decorator(Work work){
this.work=work;
others.add("挖坑");
others.add("钉木板");
}
public void insert(){
newMethod();
}
//在新方法中,我们在insert之前增加其他方法,这里次序先后是用户灵活指定的
public void newMethod(){
otherMethod();
work.insert();
}
public void otherMethod(){
ListIterator listIterator = others.listIterator();
while (listIterator.hasNext()){
System.out.println(((String)(listIterator.next())) + " 正在进行");
}
}
}
Work squarePeg = new SquarePeg();
Work decorator = new Decorator(squarePeg);
decorator.insert();
实际上Java 的I/O API就是使用Decorator实现的,I/O变种很多,如果都采取继承方法,将会产生很多子类,显然相当繁琐。
1 一个接口有好多方法,但是一个类要实现他,但是只要实现其中的一两个方法就能搞定
那么就定义一个抽象类来实现它
然后继承这个抽象类,来重写里面的两个方法,就搞定了
interface Window {// 定义Window窗口接口,表示窗口操作
public void open();// 窗口打开
public void close();// 窗口关闭
public void iconified();// 窗口最小化
public void deiconified();// 窗口恢复
public void activated();// 窗口活动
}
// 定义抽象类实现接口,在此类中覆写方法,但是所有的方法体为空
abstract class WindowAdapter implements Window {
public void open() {
};// 窗口打开
public void close() {
};// 窗口关闭
public void iconified() {
};// 窗口最小化
public void deiconified() {
};// 窗口恢复
public void activated() {
};// 窗口活动
}
// 子类继承WindowAdapter抽象类,选择性实现需要的方法
class WindowImpl extends WindowAdapter {
public void open() {
System.out.println("窗口打开");// 实现open()方法
}
public void close() {
System.out.println("窗口关闭");// 实现close()方法
}
}
2 策略模式Strategy模式)
有一个文件,我们需要读取后,希望替代其中相应的变量,然后输出。
关于替代其中变量的方法可能有多种方法,这取决于用户的要求,所以我们要准备几套变量字符替代方案。
public abstract class RepTempRule{
protected String oldString="";
public void setOldString(String oldString){
this.oldString=oldString;
}
protected String newString="";
public String getNewString(){
return newString;
}
public abstract void replace() throws Exception;
}
在RepTempRule中 有一个抽象方法abstract需要继承明确,这个replace里其实是替代的具体方法
我们现在有两个字符替代方案:
1将文本中aaa替代成bbb;
2将文本中aaa替代成ccc。
这时我们对应的类分别是RepTempRuleOne和RepTempRuleTwo:
public class RepTempRuleOne extends RepTempRule{
public void replace() throws Exception{
//replaceFirst是jdk1.4新特性
newString=oldString.replaceFirst("aaa", "bbbb")
System.out.println("this is replace one");
}
public class RepTempRuleTwo extends RepTempRule{
public void replace() throws Exception{
newString=oldString.replaceFirst("aaa", "ccc")
System.out.println("this is replace Two");
}
}
第二步:我们要建立一个算法解决类,用来提供客户端可以自由选择算法。
public class RepTempRuleSolve {
private RepTempRule strategy;
public RepTempRuleSolve(RepTempRule rule){
this.strategy=rule;
}
public String getNewContext(Site site,String oldString) {
return strategy.replace(site,oldString);
}
public void changeAlgorithm(RepTempRule newAlgorithm) {
strategy = newAlgorithm;
}
}
public class test{
......
public void testReplace(){
//使用第一套替代方案
RepTempRuleSolve solver=new RepTempRuleSolve(new RepTempRuleSimple());
solver.getNewContext(site,context);
//使用第二套
solver=new RepTempRuleSolve(new RepTempRuleTwo());
solver.getNewContext(site,context);
}
.....
}
装饰模式(Decorator模式)
我们通常可以使用继承来实现功能的拓展,如果这些需要拓展的功能的种类很繁多,那么势必生成很多子类,
同时,使用继承实现功能拓展,我们必须可预见这些拓展功能,这些功能是编译时就确定了,是静态的。
这些功能需要由用户动态决定加入的方式和时机。Decorator提供了"即插即用"的方法,在运行期间决定何时增加何种
举Adapter中的打桩示例,在Adapter中有两种类:方形桩 圆形桩,Adapter模式展示如何综合使用这两个类,
在Decorator模式中,我们是要在打桩时增加一些额外功能,比如,挖坑 在桩上钉木板等,不关心如何使用两个不相关的类。
public interface Work{
public void insert();
}
public class SquarePeg implements Work{
public void insert(){
System.out.println("方形桩插入");
}
}
public class Decorator implements Work{
private Work work;
//额外增加的功能被打包在这个List中
private ArrayList others = new ArrayList();
//在构造器中使用组合new方式,引入Work对象;
public Decorator(Work work){
this.work=work;
others.add("挖坑");
others.add("钉木板");
}
public void insert(){
newMethod();
}
//在新方法中,我们在insert之前增加其他方法,这里次序先后是用户灵活指定的
public void newMethod(){
otherMethod();
work.insert();
}
public void otherMethod(){
ListIterator listIterator = others.listIterator();
while (listIterator.hasNext()){
System.out.println(((String)(listIterator.next())) + " 正在进行");
}
}
}
Work squarePeg = new SquarePeg();
Work decorator = new Decorator(squarePeg);
decorator.insert();
实际上Java 的I/O API就是使用Decorator实现的,I/O变种很多,如果都采取继承方法,将会产生很多子类,显然相当繁琐。