interface接口解耦合

耦合的概念

耦合是一个宽泛的概念。两个程序模块有关联就叫做耦合

比如某些模块必然要关联起来才能工作,这是由业务逻辑决定的,不能否认。所以解耦并不是字面意义上的把关联拆掉,而是把模块之间的关联放松到必要的程度。

  • 模块只对外暴露最小限度的接口,形成最低的依赖关系。
  • 只要对外接口不便,板块内部的修改,就是不得影响期的的模块。
  • 删除一个模块,应当值影响有依赖关系的其他模块,而不应该影响其他无关部分
public static void main(String[] args) {
		// Mouse mouse = new Mmouse();
		// Mouse mouse = new Omouse();
		// Display display = new LED();
		Mouse mouse = new TouchScreen();
		Display display = (Display) mouse;
		Computer c = new Computer(mouse, display);
		c.startup();
	}

}

interface Mouse {
	void input();
}

interface Display {
	void output();
}

class Mmouse implements Mouse {
	public void input() {
		System.out.println("mmouse input");
	}
}

class Omouse implements Mouse {
	public void input() {
		System.out.println("omouse input");
	}
}

class CRT implements Display {
	public void output() {
		System.out.println("CRT output");
	}
}

class LED implements Display {
	public void output() {
		System.out.println("LED output");
	}
}

class TouchScreen implements Mouse, Display {
	public void input() {
		System.out.println("touchscreen input");
	}

	public void output() {
		System.out.println("touchscreen output");
	}
}

class Computer {
	private Mouse mouse;
	private Display display;

	public Computer(Mouse mouse, Display display) {
		this.mouse = mouse;
		this.display = display;
	}

	public void startup() {
		this.mouse.input();
		this.display.output();
	}



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值