• 开闭原则(增加新功能时、不会影响到原来的使用方式)用抽象构建框架、用实践扩展细节


    基本概念

    1、开闭原则(Open Closed Principle)是编程中最基础、最重要的设计原则

    2、一个软件实体如类,模块和函数应该对扩展开放(对提供方,工具类即被调用者),对修改关闭(对使用方即调用者)。用抽象构建框架,用实现扩展细节。

    3、当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化。

    4、编程中遵循其它原则,以及使用设计模式的目的就是遵循开闭原则。

    开闭原则要做的事

    将需要新增的功能抽象出来,通过抽象类类增加它的扩展功能,这样就不会改动使用者的代码,使用者始终都是通过抽象类的抽象方法调用来实现新功能,原先是在使用者不直接调用实现方法

    原先的功能是通过GraphicEditor类绘制矩形和圆形,如下代码:设计原理是通过构造方法时 给Shape.m_type赋值,通过该值来绘制相应图形

    public class Ocp {
     
    	public static void main(String[] args) {
    		//使用看看存在的问题
    		GraphicEditor graphicEditor = new GraphicEditor();
    		graphicEditor.drawShape(new Rectangle());
    		graphicEditor.drawShape(new Circle());
    		graphicEditor.drawShape(new Triangle());
    	}
     
    }
     
    //这是一个用于绘图的类 [使用方]
    class GraphicEditor {
    	//接收Shape对象,然后根据type,来绘制不同的图形
    	public void drawShape(Shape s) {
    		if (s.m_type == 1)
    			drawRectangle(s);
    		else if (s.m_type == 2)
    			drawCircle(s);
    	}
     
    	//绘制矩形
    	public void drawRectangle(Shape r) {
    		System.out.println(" 绘制矩形 ");
    	}
     
    	//绘制圆形
    	public void drawCircle(Shape r) {
    		System.out.println(" 绘制圆形 ");
    	}
    	
    }
     
    //Shape类,基类
    class Shape {
    	int m_type;
    }
     
    class Rectangle extends Shape {
    	Rectangle() {
    		super.m_type = 1;
    	}
    }
     
    class Circle extends Shape {
    	Circle() {
    		super.m_type = 2;
    	}
    }
    

    如果需要添加一个新功能,绘制三角形 

    首先需要新增一个类 ,与此同时需要修改(使用者)GraphicEditor类中的方法

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

    同时需要在drawShape方法中修改,增加  

    else if (s.m_type == 3)
       drawTriangle(s);
    

    并且还得新增 drawTriangle()方法来完成绘制

    可以发现新增功能时需要修改原来的功能

    改进方法

    public class Ocp {
     
    	public static void main(String[] args) {
    		//使用看看存在的问题
    		GraphicEditor graphicEditor = new GraphicEditor();
    		graphicEditor.drawShape(new Rectangle());
    		graphicEditor.drawShape(new Circle());
    		graphicEditor.drawShape(new Triangle());
    		graphicEditor.drawShape(new OtherGraphic());
    	}
     
    }
     
    //这是一个用于绘图的类 [使用方]
    class GraphicEditor {
    	//接收Shape对象,调用draw方法
    	public void drawShape(Shape s) {
    		s.draw();
    	}
     
    	
    }
     
    //Shape类,基类
    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 Triangle extends Shape {
    	Triangle() {
    		super.m_type = 3;
    	}
    	@Override
    	public void draw() {
    		// TODO Auto-generated method stub
    		System.out.println(" 绘制三角形 ");
    	}
    }
     
    //新增一个图形
    class OtherGraphic extends Shape {
    	OtherGraphic() {
    		super.m_type = 4;
    	}
     
    	@Override
    	public void draw() {
    		// TODO Auto-generated method stub
    		System.out.println(" 绘制其它图形 ");
    	}
    }
     
    

    原文链接:https://blog.csdn.net/qq_41813208/article/details/103077719 

     

     

  • 相关阅读:
    浅谈UML中类之间的五种关系及其在代码中的表现形式
    Understanding Liskov Substitution
    WPF捕获事件即使这个事件被标记为Handled
    [三层架构+WCF]三层架构+WCF实现TaskVision
    [.NET 三层架构(ADO.NET)+Web Service+多语言]WinForm三层架构重新实现TaskVision,外加WebService提供数据和多语言
    WPF拖动总结
    Understanding Single Responsibility and Interface Segregation
    [WinForm ADO.NET实现TaskVision][SQL Server 2008][winform datagridview总结][自定义Custom控件]Winform DataGridView各种现有column及自定义column+Winform自定义控件
    经尉迟方兄提点,终于明白为什么不是一个简单的26进制程序了
    一个有关windows服务的安装问题
  • 原文地址:https://www.cnblogs.com/ljstudy/p/14500072.html
Copyright © 2020-2023  润新知