1 Star 0 Fork 0

栾昊/DesignPattern

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Segregation1.java 2.16 KB
一键复制 编辑 原始数据 按行查看 历史
lh112 提交于 2023-09-02 20:18 +08:00 . 没有使用接口隔离原则
package com.lh.principle.segregation;
public class Segregation1 {
public static void main(String[] args) {
A a = new A();
a.depend1(new B());
//a.depend4(new B());
}
}
interface Interface1 {
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
class B implements Interface1 {
@Override
public void operation1() {
// TODO Auto-generated method stub
System.out.println("B中实现了 operation1");
}
@Override
public void operation2() {
// TODO Auto-generated method stub
System.out.println("B中实现了 operation2");
}
@Override
public void operation3() {
// TODO Auto-generated method stub
System.out.println("B中实现了 operation3");
}
@Override
public void operation4() {
// TODO Auto-generated method stub
System.out.println("B中实现了 operation4");
}
@Override
public void operation5() {
// TODO Auto-generated method stub
System.out.println("B中实现了 operation5");
}
}
class D implements Interface1 {
@Override
public void operation1() {
// TODO Auto-generated method stub
System.out.println("D中实现了 operation1");
}
@Override
public void operation2() {
// TODO Auto-generated method stub
System.out.println("D中实现了 operation2");
}
@Override
public void operation3() {
// TODO Auto-generated method stub
System.out.println("D中实现了 operation3");
}
@Override
public void operation4() {
// TODO Auto-generated method stub
System.out.println("D中实现了 operation4");
}
@Override
public void operation5() {
// TODO Auto-generated method stub
System.out.println("D中实现了 operation5");
}
}
class A { // A类通过接口 依赖(使用B类) 但是只会用到1,2,3方法
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface1 i) {
i.operation2();
}
public void depend3(Interface1 i) {
i.operation3();
}
}
class C { // C类通过接口 依赖(使用B类) 但是只会用到1,4,5方法
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface1 i) {
i.operation5();
}
public void depend3(Interface1 i) {
i.operation5();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/luan_hao/design-pattern.git
git@gitee.com:luan_hao/design-pattern.git
luan_hao
design-pattern
DesignPattern
master

搜索帮助