Java设计模式
依赖倒转原则
— 注意的细节
- 低层模块尽量都要有抽象类或接口,或者两者都有,程序稳定性更好。
- 变量的声明类型尽量是抽象类或接口,这样我们的变量引用和实际对象间,就存在一个缓冲层,利于程序的扩展。
- 继承时遵循里氏替换原则
package DependenctInversion;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
/**
* @Author:wmy
* @Date:2019/12/15
* @Description:
**/
public class Instance1 {
public static void main(String[] args) {
Changhong changhong = new Changhong();
OpenandClose openandClose = new OpenandClose();
openandClose.setOpen(changhong);
openandClose.open();
}
}
//方法1 通过接口传递实现依赖
/*interface IOpenandClose{
public void open(ITV itv);
}
interface ITV{
public void play();
}
class OpenandClose implements IOpenandClose{
@Override
public void open(ITV itv) {
itv.play();
}
}*/
class Changhong implements ITV{
@Override
public void play() {
System.out.println("tv open");
}
}
//方式2 通过构造器实现传递依赖
/*interface IOpenandClose{
public void open();
}
interface ITV{
public void play();
}
class OpenandClose implements IOpenandClose{
public ITV itv;
public OpenandClose(ITV itv){
this.itv = itv;
}
@Override
public void open() {
this.itv.play();
}
}*/
//方式3 通过setter方法进行传递
interface IOpenandClose{
public void open();
public void setOpen(ITV itv);
}
interface ITV{
public void play();
}
class OpenandClose implements IOpenandClose{
public ITV itv;
@Override
public void setOpen(ITV itv) {
this.itv = itv;
}
@Override
public void open() {
this.itv.play();
}
}
里氏替换原则:
- 子类的所有方法必须在父类中声明,或子类必须实现父类中声明的所有方法。根据里氏代换原则,为了保证系统的扩展性,在程序中通常使用父类来进行定义,如果一个方法只存在子类中,在父类中不提供相应的声明,则无法在以父类定义的对象中使用该方法。
- 我们在运用里氏代换原则时,尽量把父类设计为抽象类或者接口,让子类继承父类或实现父接口,并实现在父类中声明的方法,运行时,子类实例替换父类实例,我们可以很方便地扩展系统的功能,同时无须修改原有子类的代码,增加新的功能可以通过增加一个新的子类来实现。里氏代换原则是开闭原则的具体实现手段之一。
package LiReplacePrinciple;
/**
* @Author:wmy
* @Date:2019/12/16
* @Description:
**/
public class Instance {
public static void main(String[] args) {
A a = new A();
System.out.println(a.func1(10, 3));
B b = new B();
System.out.println(b.func3(9, 2));
}
}
class Base {
}
class A extends Base {
public int func1(int num1, int num2) {
return num1 - num2;
}
}
class B extends Base {
private A a = new A();
public int func1(int num1, int num2) {
return num1 + num2;
}
public int func2(int num1, int num2) {
return (num1 + num2) + 1;
}
public int func3(int a, int b) {
return this.a.func1(a, b);
}
}
开闭原则
基本介绍
- 开闭原则是编程中,最重要的,最基础的设计原则
- 一个软件实体如类,模块和函数应该== 对扩展开放(提供方),对修改关闭(对使用房)==。用抽象构建架构,用实现扩展细节。
- 当软件发生变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化。
- 编程中遵循其他原则,以及使用设计模式的目的就是遵循开闭原则。
更改之前:
package ocp;
import java.awt.*;
/**
* @Author:wmy
* @Date:2019/12/16
* @Description:
**/
public class InstanceofOCP {
public static void main(String[] args) {
GraphicEditor graphicEditor =new GraphicEditor();
graphicEditor.judgeGraphic(new Rectangle());
graphicEditor.judgeGraphic(new Circle());
}
}
class GraphicEditor{
public void judgeGraphic(Base base){
if(base.type==1){
draw1();
}else if(base.type==2){
draw2();
}
}
public void draw1(){
System.out.println("绘制矩形");
}
public void draw2(){
System.out.println("绘制圆形");
}
}
class Base{
int type;
}
class Rectangle extends Base{
Rectangle(){
super.type=1;
}
}
class Circle extends Base{
Circle(){
super.type=2;
}
}
更改之后:
package ocp;
import java.awt.*;
/**
* @Author:wmy
* @Date:2019/12/16
* @Description:
**/
public class InstanceofOCP {
public static void main(String[] args) {
GraphicEditor graphicEditor =new GraphicEditor();
graphicEditor.judgeGraphic(new Rectangle());
graphicEditor.judgeGraphic(new Circle());
}
}
class GraphicEditor{
public void judgeGraphic(Base base){
base.draw();
}
}
abstract class Base{
int type;
public abstract void draw();
}
class Rectangle extends Base{
Rectangle(){
super.type=1;
}
@Override
public void draw() {
System.out.println("绘制矩形");
}
}
class Circle extends Base{
Circle(){
super.type=2;
}
@Override
public void draw() {
System.out.println("绘制圆形");
}
}