面向可维护性的设计模式

创建型模式

工厂方法模式/Factory Method pattern
*适用:*当client不知道要创建哪个具体类的时候,或者不想在Client中指明要具体创建的实例的时候。
*方法:*定义一个用于创建对象的接口,让其子类来决定实例化哪一个类,从而使类的实例化延迟到子类
实例:
第一部分:
public interface Trace {
// turn on and off debugging
public void setDebug( boolean debug );
// write out a debug message
public void debug( String message );
// write out an error message
public void error( String message );
}
product1
public class FileTrace implements Trace {
private PrintWriter pw;
private boolean debug;
public FileTrace() throws IOException {
pw = new PrintWriter( new FileWriter( “t.log” ) );
}
public void setDebug( boolean debug ) {
this.debug = debug;
}
public void debug( String message ) {
if( debug ) {
pw.println( "DEBUG: " + message );
pw.flush();
}
}
public void error( String message ) {
pw.println( "ERROR: " + message );
pw.flush();
}
}

product2
public class SystemTrace implements Trace {
private boolean debug;
public void setDebug( boolean debug ) {
this.debug = debug;
}
public void debug( String message ) {
if( debug )
System.out.println( "DEBUG: " + message );
}
public void error( String message ) {
System.out.println( "ERROR: " + message );
}
}

factory接口
interface TraceFactory {
public Trace getTrace();
public Trace getTrace(String type);
void otherOperation(){};
}
Fctory1
public class Factory1 implements TraceFactory {
public Trace getTrace() {
return new SystemTrace();
}
}
Factory2
public class Factory2 implements TraceFactory {
public getTrace(String type) {
if(type.equals(“file”)
return new FileTrace();
else if (type.equals(“system”)
return new SystemTrace();
}
}

使用创建对象时候
Trace log1 = new Factory1().getTrace();
log1.setDebug(true);
log1.debug( “entering log” );
Trace log2 = new Factory2().getTrace(“system”);
log2.setDebug(false);
log2.debug("…");
可以看到,不需要将某个类放到应用程序的代码之中代码只需要使用接口就可以。相当于把中间那部分影藏了起来。
常规模式下都是:Product p=new Product2();
这样一调整就可以变为
Product p=new Concrete2().makeObject();
本来要创建某一个对象,现在我把对象的对应的接口以及对象的实现都写出来了。以后我要添加什么新的对象类型,就在那里面加入。
现在我只需要创建一个接口来引用那些构造方法,也就是factory接口,然后他的若干个实现类,分别实现引用所需要的对象类的构造方法

适配器模式/Adapter

增加一个接口,让client面向接口,隐藏具体的那个子类
实例:
class LegacyRectangle {
void display(int x1, int y1, int w, int h) {… }
}
class Client {
public display() {
new LegacyRectangle().display(x1, y1, x2, y2);
}
}
在这里Client直接调用了示例

Adapter
interface Shape {
void display(int x1, int y1, int x2, int y2);
}
class Rectangle implements Shape {
void display(int x1, int y1, int x2, int y2) {
new LegacyRectangle().display(x1, y1, x2-x1, y2-y1);
}
}
class LegacyRectangle {
void display(int x1, int y1, int w, int h) {…}
}
class Client {
Shape shape = new Rectangle();
public display() {
shape.display(x1, y1, x2, y2);
}
}
这样一来,便是通过对应的接口来使用方法。
接口用来面向Client,接口的实现,有一个类的委托,通过这个委托,调用类的具体方法,以此让Client可以使用到这个方法,也让Client无法接触到类

Decorator

特点:大量的重复。通过构建子类给对象增加不同的特性。那如果需要几个特性一起,就连续的继承。对每一个特性构造子类,通过委派的方法增加到对象上

Strategy/策略模式

解决的问题:许多算法具有相似性,使用if…else十分复杂
使用情况:一个系统有许多类,区分它们的知识他们的直接行为
解决方式:将算法封装成一个个类,要用到哪个就替换
关键:实现一个统一的接口

每次使用的都是直接的这些子类。这些子类都是实际的使用方法。

模板模式

适用情况:一些方法都是通用的,但是在每个子类中都重写了
解决方法:将这个方法抽取出来,放在抽象类实现
思路:定义一个骨架,将步骤延迟到子类中。让子类可以不改变结构就重定义某些步骤

实现:
创建一个抽象类,模板方法定义为final保证不会被重写,留下一部分抽象类,随便子类进行改写。
或者说,结构不变,比如一个房子有窗户有房顶,我们留下这些要求,也就是必须有这些,但是窗户什么样,屋顶怎么样,随便我们去改写。final的就是有房子又屋顶的这个模板。

迭代器模式

适用于顺序访问集合的元素,不需要知道集合对象的底层实现
适用情况:遍历一个聚合的对象
关键:定义hasNext等接口

访问者模式

*使数据结构与数据操作分开
对一个结构中的对象要做许多的操作,而为了避免这些操作,影响到这些对象的类
解决办法:在被访问类中添加一个对外提供接待的接口
在数据基础类中有一个方法接受访问者,并将自身引用
*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值