七大设计原则之开闭原则
介绍
对扩展(开发者)开放,对修改(使用者)关闭,用抽象构建框架,用实现扩展细节
案例
public class OpenClosePrincipleDemo1 {
public static void main(String[] args) {
Shape rectangle = new Rectangle();
System.out.println(rectangle.m_type);
GraphicEditer graphicEditer = new GraphicEditer();
graphicEditer.drawShape(rectangle);
}
}
class GraphicEditer{
public void drawShape(Shape shape){
shape.draw();
}
}
abstract class Shape{
int m_type;
public abstract void draw();
}
class Rectangle extends Shape{
Rectangle(){
super.m_type = 1;
}
@Override
public void draw() {
System.out.println("绘制矩形===");
}
}
class Circle extends Shape{
Circle(){
super.m_type = 2;
}
@Override
public void draw() {
System.out.println("绘制圆型===");
}
}
class Triangle extends Shape{
Triangle(){
super.m_type = 3;
}
@Override
public void draw() {
System.out.println("绘制三角形===");
}
}