Open-Closed Principle

开放封闭原则

开放封闭原则(OCP,Open Closed Principle)是所有面向对象原则的核心。软件设计本身所追求的目标就是封装变化、降低耦合,而开放封闭原则正是对这一目标的最直接体现。其他的设计原则,很多时候是为实现这一目标服务的,例如以Liskov替换原则实现最佳的、正确的继承层次,就能保证不会违反开放封闭原则。

关于开放封闭原则,其核心的思想是:

软件实体应该是可扩展,而不可修改的。也就是说,对扩展是开放的,而对修改是封闭的。 因此,开放封闭原则主要体现在两个方面:
对扩展开放,意味着有新的需求或变化时,可以对现有代码进行扩展,以适应新的情况。
对修改封闭,意味着类一旦设计完成,就可以独立完成其工作,而不要对类进行任何修改。

示例

面向抽象编程,计算一个圆柱体体积,分矩形底和圆形底。

这里写图片描述


Geometry.java

package com.ab;

public abstract class Geometry {
    public abstract double getArea();
}

Circle.java

package com.ab;
//第一个子类
public class Circle extends Geometry{
    double r;
    Circle(double r){
        this.r = r;
    }
    public double getArea(){
        return (3.14*r*r);
    }
}
//第二个子类
class Rectangle extends Geometry{
     double a;
     double b;

     public Rectangle(double a, double b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public double getArea() {
        // TODO Auto-generated method stub
        return a*b;
    }}

Pillar.java

package com.ab;

public class Pillar {
    Geometry bottom;
    double height;
    Pillar(Geometry bottom,double height){
        this.bottom =bottom;
        this.height = height;
    }
    public double getVolume(){
        return bottom.getArea()*height;
    }
}

Application.java

package com.ab;

public class Application {
    public static void main(String[] args) {
        Pillar pillar;
        Geometry bottom;
        bottom = new Rectangle(12, 22);
        pillar = new Pillar(bottom, 58);
        System.out.println("矩形底的圆柱体体积"+pillar.getVolume());

        bottom = new Circle(10);
        pillar = new Pillar(bottom, 58);
        System.out.println("圆形底的圆柱体体积"+pillar.getVolume());

    }
}

矩形底的圆柱体体积15312.0
圆形底的圆柱体体积18212.0

UML类图

这里写图片描述

该设计中Geometry和Pillar类就是系统中对修改关闭的部分,而Geometry的子类就是对扩展开放的部分,当向系统再增加任何Geometry的子类时(对扩展开放),不必修改Pillar类,就可以使用Pillar创建出具有Geometry的新子类指定的底的柱体。

-java程序设计实用教程 p105

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
3)A digital clock consists of a screen to display the time and a dial for setting in turn the year, month, day, hour and minute. Twisting the dial to the left reduces by one the value being changed but twisting it to the right increases it by one. Pushing the dial alters which value is being adjusted. At first, it is the year but after the dial is pushed once, it is the month, then after the dial is pushed again, it is the day and so on. Imagine the clock is represented by a class with attributes year, month, day etc. The following is what the code for a method rotateDialLeft() might look like. public void rotateDialLeft() { if (mode == YEAR_MODE) { year--; } else if (mode == MONTH_MODE) { month--; } else if (mode == DAY_MODE) { day--; } else if (mode == HOUR_MODE) { hour--; } else if (mode == MINUTE_MODE) { minute--; } } The code for rotateDialRight() is similar. Apply the Open-Closed Principle to explain why the above code is unsatisfactory from the design viewpoint, considering the possibility of future change to the code, giving an example of such a change. 5)Give the code required for the classes introduced in question 3), focusing on the code for a method selectState() which changes the value that is being adjusted from years to months. Make it clear in which classes the code is to be found. Assume the existence of other methods that are needed such as getMonthSetUpState(). 8)Suppose that in a multiplayer role-playing game, a class Client has a dependency to an interface Fighter with public methods attack(), defend() and escape(). The game designer now wishes for Client to use a class Wizard with three different but equivalent public methods castDestructionSpell(), shield() and portal(). Explain how it is possible to do this using an appropriate design pattern.
06-03

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值