设计模式中的几个原则
原文链接:https://blog.csdn.net/qq_39355828/article/details/112201341 (opens new window)
# 1.单一职责模式

单一职责模式-每个类实现一个指责
package com.principle.singleresponsibiliity;
public class SingleResponsibility1 {
public static void main(String[] args)
{
Teacher tt=new Teacher();
tt.eat("王老师");
Student ss=new Student();
ss.eat("李同学");
}
}
class Teacher
{
public void eat(String teacher)
{
System.out.println(teacher+" 在教师餐厅");
}
}
class Student
{
public void eat(String student)
{
System.out.println(student+" 在学生餐厅");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
单一指责模式-每个方法实现一个指责
package com.principle.singleresponsibiliity;
public class SingleResponsibility
{
public static void main(String[] args)
{
People pp=new People();
pp.teacherdo("王老师");
pp.studentdo("李同学");
}
}
class People
{
public void teacherdo(String name)
{
System.out.println(name+"在教师餐厅吃");
}
public void studentdo(String name)
{
System.out.println(name+"在学生餐厅吃");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 2.接口隔离原则
客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上
将接口 Interface1 拆分为独立的几个接口**(这里我们拆分成 3 个接口)**,类 A 和类 C 分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则

package com.principle.Segregation;
public class Segregation1
{
public static void main(String[] args)
{
A aa=new A();
C cc=new C();
B bb=new B();
D dd=new D();
aa.depend1(bb);
aa.depend2(bb);
aa.depend3(bb);
cc.depend1(dd);
cc.depend2(dd);
cc.depend3(dd);
}
}
interface Interface
{
public void run1();
public void run2();
public void run3();
public void run4();
public void run5();
}
class B implements Interface
{
public void run1()
{
System.out.println("B run1 is running");
}
public void run2()
{
System.out.println("B run2 is running");
}
public void run3()
{
System.out.println("B run3 is running");
}
public void run4()
{
System.out.println("B run4 is running");
}
public void run5()
{
System.out.println("B run5 is running");
}
}
class D implements Interface
{
public void run1()
{
System.out.println("D run1 is running");
}
public void run2()
{
System.out.println("D run2 is running");
}
public void run3()
{
System.out.println("D run3 is running");
}
public void run4()
{
System.out.println("D run4 is running");
}
public void run5()
{
System.out.println("D run5 is running");
}
}
class A //A通过Interface依赖D
{
public void depend1(Interface i)
{
i.run1();
}
public void depend2(Interface i)
{
i.run2();
}
public void depend3(Interface i)
{
i.run3();
}
}
class C //A通过Interface依赖D
{
public void depend1(Interface i)
{
i.run1();
}
public void depend2(Interface i)
{
i.run4();
}
public void depend3(Interface i)
{
i.run5();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
改进后 将Interface 分成三个接口 从而减少了对不需要接口进行实现

package com.principle.Segregation;
public class Segregation1
{
public static void main(String[] args)
{
A aa=new A();
C cc=new C();
aa.depend1(new B());
aa.depend2(new B());
aa.depend3(new B());
cc.depend1(new D());
cc.depend2(new D());
cc.depend3(new D());
}
}
interface Interface1
{
public void run1();
}
interface Interface2
{
public void run2();
public void run3();
}
interface Interface3
{
public void run4();
public void run5();
}
class B implements Interface1,Interface2
{
public void run1()
{
System.out.println("B run1 is running");
}
public void run2()
{
System.out.println("B run2 is running");
}
public void run3()
{
System.out.println("B run3 is running");
}
}
class D implements Interface1,Interface3
{
public void run1()
{
System.out.println("D run1 is running");
}
public void run4()
{
System.out.println("D run4 is running");
}
public void run5()
{
System.out.println("D run5 is running");
}
}
class A //A通过Interface依赖D
{
public void depend1(Interface1 i)
{
i.run1();
}
public void depend2(Interface2 i)
{
i.run2();
}
public void depend3(Interface2 i)
{
i.run3();
}
}
class C //A通过Interface依赖D
{
public void depend1(Interface1 i)
{
i.run1();
}
public void depend2(Interface3 i)
{
i.run4();
}
public void depend3(Interface3 i)
{
i.run5();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 3.依赖倒转原则
依赖倒转原则(Dependence Inversion Principle)是指:
- 高层模块不应该依赖低层模块,二者都应该依赖其抽象
- 抽象不应该依赖细节,细节应该依赖抽象
- 依赖倒转(倒置)的中心思想是面向接口编程
- 依赖倒转原则是基于这样的设计理念:相对于细节的多变性,抽象的东西要稳定的多。以抽象为基础搭建的架构比以细节为基础的架构要稳定的多。在 java 中,抽象指的是接口或抽象类,细节就是具体的实现类
- 使用接口或抽象类的目的是制定好规范,而不涉及任何具体的操作,把展现细节的任务交给他们的实现类去完成
package com.principle.inversion.com.principle;
public class inversion
{
public static void main(String[] args)
{
Person pp=new Person();
pp.receive(new Email());
pp.receive(new Whechat());
}
}
//
interface IReceiver
{
public String getInfo();
}
class Email implements IReceiver
{
public String getInfo()
{
return "hello";
}
}
class Whechat implements IReceiver
{
public String getInfo()
{
return "hello";
}
}
class Person
{
public void receive(IReceiver re)
{
System.out.println(re.getInfo());
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
- 接口传递
- 构造方法传递
- setter 方式传递
# 4.里氏原则
(1) 如果对每个类型为 T1 的对象 o1,都有类型为 T2 的对象 o2,使得以 T1 定义的所有程序 P 在所有的对象 o1 都代换成 o2 时,程序 P 的行为没有发生变化,那么类型 T2 是类型 T1 的子类型。换句话说,所有引用基类的地方必须能透明地使用其子类的对象。(2) 在使用继承时,遵循里氏替换原则,在子类中尽量不要重写父类的方法(3) 里氏替换原则告诉我们,继承实际上让两个类耦合性增强了,在适当的情况下,可以通过聚合,组合,依赖 来解决问题。.
package com.principle.liskov;
public class lisov
{
public static void main(String[] args)
{
A a=new A();
System.out.println("11-3= "+a.func1(11,3));
System.out.println("1-8= "+a.func1(1,8));
System.out.println("-------------");
B b=new B();
System.out.println("11-3= "+b.func1(11,3));
System.out.println("11-3= "+b.func1(1,8));
System.out.println("11+3+9= "+b.func1(11,3));
}
}
class A
{
int func1(int a,int b)
{
return a-b;
}
}
class B extends A{
int func1(int a,int b)
{
return a+b;
}
int func2(int a,int b)
{
return func1(a,b)+9;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
改进后
package com.principle.liskov;
public class lisovimprove
{
public static void main(String[] args)
{
AA aa=new AA();
System.out.println("11-3="+aa.func1(11,3));
BB bb=new BB();
System.out.println("11-3="+bb.func1(11,3));
}
}
class Base{
}
class AA extends Base
{
public int func1(int a,int b)
{
return a-b;
}
}
class BB extends Base
{
public int func1(int a,int b)
{
return a+b;
}
public int func2(int a,int b)
{
return a-b;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 5.开闭原则
- 一个软件实体如类,模块和函数应该对扩展开放**(对提供方),对修改关闭(对使用方)**。用抽象构建框架,用实现扩展细节。
- 当软件需要变化时,尽量通过扩展软件实体的行为来实现变化,而不是通过修改已有的代码来实现变化。
- 编程中遵循其它原则,以及使用设计模式的目的就是遵循开闭原则。

package com.principle.ocp;
public class OCP
{
public static void main(String[] args)
{
GraphicEditor gg=new GraphicEditor();
gg.drawRectangle(new Rectangle());
gg.drawCircle(new Circle());
gg.drawTrirangle(new Tritangle());
}
}
class GraphicEditor
{
public void drawShape(Shape s)
{
if(s.myShape==1)
drawRectangle(s);
if(s.myShape==2)
drawCircle(s);
if(s.myShape==3)
drawTrirangle(s);
}
public void drawRectangle(Shape r)
{
System.out.println("绘制矩形");
}
public void drawCircle(Shape r)
{
System.out.println("绘制圆形");
}
public void drawTrirangle(Shape r)
{
System.out.println("绘制三角形");
}
}
class Shape
{
int myShape;
}
class Rectangle extends Shape
{
Rectangle()
{
super.myShape=1;
}
}
class Circle extends Shape
{
Circle()
{
super.myShape=2;
}
}
class Tritangle extends Shape
{
Tritangle()
{
super.myShape=3;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
添加绘制图形 需要修改三处,不方便修改 违反了设计模式的 ocp 原则,即对扩展开放(提供方),对修改关闭(使用方)。即当我们给类增加新功能的时候,尽量不修改代码,或者尽可能少修改代码.
把创建 Shape 类做成抽象类,并提供一个抽象的 draw 方法,让子类去实现即可,这样我们有新的图形种类时,只需要让新的图形类继承 Shape,并实现 draw 方法即可,使用方的代码就不需要改 , 满足了开闭原则
package com.principle.ocp;
public class OCP_improve
{
public static void main(String[] args)
{
GraphicEditor1 ggg=new GraphicEditor1();
ggg.drawShape(new Rectangle1());
ggg.drawShape(new Circle1());
}}
class GraphicEditor1
{
public void drawShape(Shape1 s)
{
s.draw();
}
}
abstract class Shape1
{
int myShape;
public abstract void draw();//抽象方法
}
class Rectangle1 extends Shape1
{
Rectangle1()
{
super.myShape=1;
}
public void draw()
{
System.out.println("绘制矩形");
}
}
class Circle1 extends Shape1
{
Circle1()
{
super.myShape=2;
}
public void draw()
{
System.out.println("绘制圆形");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46