设计模式(2)-设计模式的七大原则(二)

4.里氏替换原则

基本介绍

  1. 里氏替换原则(Liskov Substitution Principle)在1988年,由麻省理工学院的以为姓里
    的女士提出的。
  2. 如果对每个类型为T1的对象o1,都有类型为T2的对象o2,使得以T1定义的所有程序
    P在所有的对象o1都代换成o2时,程序P的行为没有发生变化,那么类型T2是类型T1
    的子类型。换句话说,所有引用基类的地方必须能透明地使用其子类的对象。
  1. 在使用继承时,遵循里氏替换原则,在子类中尽量不要重写父类的方法
  2. 里氏替换原则告诉我们,继承实际上让两个类耦合性增强了,在适当的情况下,可以通过聚合,组合,依赖 来解决问题。.

解决方法

  1. 我们发现原来运行正常的相减功能发生了错误。原因就是类B无意中重写了父类的
    方法,造成原有功能出现错误。在实际编程中,我们常常会通过重写父类的方法完
    成新的功能,这样写起来虽然简单,但整个继承体系的复用性会比较差。特别是运
    行多态比较频繁的时候
  2. 通用的做法是:原来的父类和子类都继承一个更通俗的基类,原有的继承关系去掉,
    采用依赖,聚合,组合等关系代替.

原先代码:

package liskov;

public class Liskov {

	public static void main(String[] args) {
		
		A a = new A();
		System.out.println("11-3=" + a.func1(11, 3));
		System.out.println("1-8=" + a.func1(1, 8));
		System.out.println("-----------------------");
		B b = new B();
		System.out.println("11-3=" + b.func1(11, 3));
		System.out.println("1-8=" + b.func1(1, 8));
		System.out.println("11+3+9=" + b.func2(11, 3));

	}

}

//A类
class A {
	//返回两数之差
	public int func1(int num1, int num2) {
		return num1 - num2;
	}
}

//B类继承了A类
class B extends A {
	//增加了一个新的功能,完成两数相加,然后和9求和
	public int func1(int a, int b) {
		return a + b;
	}

	public int func2(int a, int b) {
		return func1(a, b) + 9;
	}
}

遵从里氏替换原则代码

package liskov1;

public class Liskov {

	public static void main(String[] args) {
		
		A a = new A();
		System.out.println("11-3=" + a.func1(11, 3));
		System.out.println("1-8=" + a.func1(1, 8));
		System.out.println("-----------------------");
		B b = new B();
		//因为B类不再继承A类,因此调用者不会再认为func1是求剪发
		System.out.println("11+3=" + b.func1(11, 3));
		System.out.println("1+8=" + b.func1(1, 8));
		System.out.println("11+3+9=" + b.func2(11, 3));
		
		//使用组合任然可以使用A的相关方法
		System.out.println("11-3=" + b.func3(11, 3));

	}

}
//创建一个更加基础的基类
class Base{
	//把更加基础的类和成员写到base类中
	
}

//A类
class A extends Base{
	//返回两数之差
	public int func1(int num1, int num2) {
		return num1 - num2;
	}
}

//B类继承了A类
class B  extends Base{
	//如果B类需要A类的方法,使用组合的关系
	private A a = new A();
	
	//增加了一个新的功能,完成两数相加,然后和9求和
	public int func1(int a, int b) {
		return a + b;
	}

	public int func2(int a, int b) {
		return func1(a, b) + 9;
	}
	
	//加入我们任然想使用A的方法
	public int func3(int a,int b) {
		return this.a.func1(a,b);
	}
}

5.开闭原则

基本介绍

  1. 开闭原则(Open Closed Principle)是编程中最基础、最重要的设计原则
  2. 一个软件实体如类,模块和函数应该对扩展开放(对提供方),对修改关闭(对使用
    方)。用抽象构建框架,用实现扩展细节。
  3. 当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已
    有的代码来实现变化。
  4. 编程中遵循其它原则,以及使用设计模式的目的就是遵循开闭原则。

不存从开闭原则代码

package ocp;

public class ocp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//使用看看存在的问题
		GraphicEditor graphicEditor = new GraphicEditor();
		graphicEditor.drawShape(new Rectangle());
		graphicEditor.drawShape(new Circle());
		graphicEditor.drawShape(new Tringle());

	}

}
//这是一个用于绘图的类
class GraphicEditor {
	//接收Shape对象,然后根据类型绘制不同的图形
	public void drawShape(Shape s) {
		if (s.m_type == 1)
			drawRectangle(s);
		else if (s.m_type == 2)
			drawCircle(s);
		else if(s.m_type == 3)
			drawTringle(s);
	}

	public void drawRectangle(Shape r) {
		System.out.println("绘制矩形");
	}

	public void drawCircle(Shape r) {
		System.out.println("绘制圆形");
	}
	
	public void drawTringle(Shape r) {
		System.out.println("绘制三角形");
	}
}

//基类
class Shape {
	int m_type;
}

class Rectangle extends Shape {
	Rectangle() {
		super.m_type = 1;
	}
}

class Circle extends Shape {
	Circle() {
		super.m_type = 2;
	}
}

//新增画三角形的类
class Tringle extends Shape {
	Tringle() {
		super.m_type = 3;
	}
}

方式1的优缺点

  1. 优点是比较好理解,简单易操作。
  2. 缺点是违反了设计模式的ocp原则,即对扩展开放(提供方),对修改关闭(使用方)。
    即当我们给类增加新功能的时候,尽量不修改代码,或者尽可能少修改代码.
  3. 比如我们这时要新增加一个图形种类 三角形,我们需要做如下修改,修改的地方
    较多

改进的思路分析
思路:把创建Shape类做成抽象类,并提供一个抽象的draw方法,让子类去实现即可,
这样我们有新的图形种类时,只需要让新的图形类继承Shape,并实现draw方法即可,
使用方的代码就不需要修 -> 满足了开闭原则
使用开闭原则代码:

package ocp1;

public class ocp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//使用看看存在的问题
		GraphicEditor graphicEditor = new GraphicEditor();
		graphicEditor.drawShape(new Rectangle());
		graphicEditor.drawShape(new Circle());
		graphicEditor.drawShape(new Tringle());
		graphicEditor.drawShape(new Other());

	}

}
//这是一个用于绘图的类
class GraphicEditor {
	//接收Shape对象,然后根据类型绘制不同的图形
	public void drawShape(Shape s) {
		s.draw();
	}

}

//基类
abstract class Shape {
	int m_type;
	public abstract void draw();//抽象方法
}

class Rectangle extends Shape {
	Rectangle() {
		super.m_type = 1;
	}

	@Override
	public void draw() {
		// TODO Auto-generated method stub
		System.out.println("绘制矩形");
		
	}
}

class Circle extends Shape {
	Circle() {
		super.m_type = 2;
	}
	
	@Override
	public void draw() {
		// TODO Auto-generated method stub
		System.out.println("绘制圆形");
		
	}
}

//新增画三角形的类
class Tringle extends Shape {
	
	Tringle() {
		super.m_type = 3;
	}
	
	@Override
	public void draw() {
		// TODO Auto-generated method stub
		System.out.println("绘制三角形");
		
	}
}

//新增一个图形
class Other extends Shape{
	Other() {
		super.m_type = 4;
	}
	
	@Override
	public void draw() {
		// TODO Auto-generated method stub
		System.out.println("其他图形");
		
	}
	
}
原创文章 153 获赞 71 访问量 4万+

猜你喜欢

转载自blog.csdn.net/mzc_love/article/details/105143189
今日推荐