定义:
一个软件实体如类,模块和函数应该对扩展开放,对修改关闭。即一个软件实体应该通过扩展来实现变化,而不是通过修改已有的代码来实现变化。
软件实体应该包括以下几个部分:
1、项目或软件产品中按照一定的逻辑规则划分的模块
2、抽象和类
3、方法
使用:
1.抽象约束
通过接口或抽象类约束扩展,对扩展进行边界限定,不允许出现在接口或抽象类中不存在的public方法
参数类型、引用对象尽量使用接口或抽象类,而不是实现类
抽象层尽量保持稳定,一旦确定不允许修改接口或抽象类一旦定义,应立即执行,不能有修改接口的想法,除非是彻底的大返工
2.元数据控制模块行为
元数据: 用来描述环境和数据的数据,通俗的说就是配置参数.
通过扩展一个子类,修改配置文件,完成了业务的变化,也是框架的好处
3.制定项目章程
4.封装变化
对变化的封装包含两层含义:
1、将相同的变化封装到一个接口或抽象类中
2、将不同的变化封装到不同的接口或抽象类中
不能有两个不同的变化出现在同一个接口或抽象类中.封装变化,找出预计的变化或不稳定的点,为这些变化点创建稳定的接口,准确的讲是封装可能发生的变化。
举例:
利用委派编写代码以及使用装饰器模式其实是很好的实现了开闭原则;
我们用一个装饰器的例子进行说明:
public interface Shoes {
public void makeShoes();
}
public class MakeShoes implements Shoes {
@Override
public void makeShoes() {
System.out.println("制作一双鞋");
}
}
此时我们已经实现了基本功能,但是我们想要扩展功能,修改原码方法?不,我们编写新的接口与实现:
public class Decorator implements Shoes {
private Shoes shoes;
public Decorator(Shoes _shoes) {
this.shoes = _shoes;
}
@Override
public void makeShoes() {
shoes.makeShoes(); //基本功能委派给其他类实现
}
}
public class Embroidery extends Decorator implements Shoes {
public Embroidery(Shoes _shoes) {
super(_shoes);
}
public void embroidery() {
System.out.println("给鞋补充图案");
}
public void makeShoes() {
super.makeShoes();
//拓展方式一:直接在子类的基本功能的实现方法中更新
System.out.println("在鞋子上绘制一个logo");
//拓展方式二:在子类中新加入一个方法,在基本功能的实现方法中调用
this.embroidery();
}
}
public class Hollow extends Decorator {
public Hollow(Shoes _shoes) {
super(_shoes);
}
public void hollow() {
System.out.println("关键位置挖层处理");
}
public void makeShoes() {
super.makeShoes();
this.hollow();
}
}
这样,我们就实现了在不修改原代码方法的基础上进行功能的拓展,实现了对扩展开发,对修改封闭;
public class TestDecorator {
public static void main(String[] args) {
Shoes shoes = new MakeShoes();
shoes = new Embroidery(shoes);
shoes = new Hollow(shoes);
shoes.makeShoes();
//当然我们也可以这么实现:
Shoes shoes = new Hollow(new Embroidery(new MakeShoes()));
shoes.makeShoes();
System.out.println("鞋子制作完成!");
}
}
总结:
开闭原则要求对扩展开放,对修改关闭,其实也就是我们要在不修改原代码的基础上扩展新功能,而委派与装饰器很好的实现了这一点!