7.2 what
一個軟件實體如果使用的是一個基類的話,那么一定適用于其子類,而且它根本不能覺察出基類對象和子類對象的區別。
反之不成立。
7.3 在設計模式中的體現
策略模式
合成模式
代理模式
7.5
長方形和正方形
Rectangle類的源代碼:
Square類的源代碼:
正方形不可以作為長方形的子類
新的Square類的源代碼:
SmartTest類的源代碼:
代碼的重構
Quadrangle類的源代碼:
Rectangle類的源代碼:
Square類的源代碼:?
一個軟件實體如果使用的是一個基類的話,那么一定適用于其子類,而且它根本不能覺察出基類對象和子類對象的區別。
反之不成立。
7.3 在設計模式中的體現
策略模式
合成模式
代理模式
7.5
長方形和正方形
Rectangle類的源代碼:
?1
package?com.javapatterns.liskov.version1;
?2
?3
public?class?Rectangle
?4

{
?5
????private?long?width;
?6
????private?long?height;
?7
????
?8
????public?void?setWidth(long?width)
?9
????
{
10
????????this.width?=?width;
11
????}
12
????public?long?getWidth()
13
????
{
14
????????return?this.width;
15
????}
16
????public?void?setHeight(long?height)
17
????
{
18
????????this.height?=?height;
19
????}
20
????public?long?getHeight()
21
????
{
22
????????return?this.height;
23
????}
24
}

?2

?3

?4



?5

?6

?7

?8

?9



10

11

12

13



14

15

16

17



18

19

20

21



22

23

24

Square類的源代碼:
?1
package?com.javapatterns.liskov.version1;
?2
?3
public?class?Square
?4

{
?5
????private?long?side;
?6
????
?7
????public?void?setSide(long?side)
?8
????
{
?9
????????this.side?=?side;
10
????}
11
12
????public?long?getSide()
13
????
{
14
????????return?side;
15
????}
16
}

?2

?3

?4



?5

?6

?7

?8



?9

10

11

12

13



14

15

16

正方形不可以作為長方形的子類
新的Square類的源代碼:
?1
package?com.javapatterns.liskov.version2;
?2
?3
public?class?Square?extends?Rectangle
?4

{
?5
????private?long?side;
?6
?7
????public?void?setWidth(long?width)
?8
????
{
?9
????????setSide(width);
10
????}
11
12
????public?long?getWidth()
13
????
{
14
????????return?getSide();
15
????}
16
17
????public?void?setHeight(long?height)
18
????
{
19
????????setSide(height);
20
????}
21
22
????public?long?getHeight()
23
????
{
24
????????return?getSide();
25
????}
26
27
????public?long?getSide()
28
????
{
29
????????return?side;
30
????}
31
32
????public?void?setSide(long?side)
33
????
{
34
????????this.side?=?side;
35
????}
36
}

?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

SmartTest類的源代碼:
?1
package?com.javapatterns.liskov.version2;
?2
?3
public?class?SmartTest
?4

{
?5
????public?void?resize(Rectangle?r)
?6
????
{
?7
????????while?(r.getHeight()?<=?r.getWidth()?)
?8
????????
{
?9
????????????r.setWidth(r.getWidth()?+?1);
10
????????}
11
????}
12
13
????/**?*//**?@link?dependency?*/
14
????/**//*#Rectangle?lnkRectangle;*/
15
}

?2

?3

?4



?5

?6



?7

?8



?9

10

11

12

13


14


15

代碼的重構
Quadrangle類的源代碼:
1
package?com.javapatterns.liskov.version3;
2
3
public?interface?Quadrangle
4

{
5
????public?long?getWidth();
6
7
????public?long?getHeight();
8
}

2

3

4



5

6

7

8

Rectangle類的源代碼:
?1
package?com.javapatterns.liskov.version3;
?2
?3
public?class?Rectangle?implements?Quadrangle?
?4

{
?5
????private?long?width;
?6
????private?long?height;
?7
????
?8
????public?void?setWidth(long?width)
?9
????
{
10
????????this.width?=?width;
11
????}
12
????public?long?getWidth()
13
????
{
14
????????return?this.width;
15
????}
16
????public?void?setHeight(long?height)
17
????
{
18
????????this.height?=?height;
19
????}
20
????public?long?getHeight()
21
????
{
22
????????return?this.height;
23
????}
24
}

?2

?3

?4



?5

?6

?7

?8

?9



10

11

12

13



14

15

16

17



18

19

20

21



22

23

24

Square類的源代碼:?
?1
package?com.javapatterns.liskov.version3;
?2
?3
public?class?Square?implements?Quadrangle?
?4

{
?5
????private?long?side;
?6
?7
????public?void?setSide(long?side)
?8
????
{
?9
????????this.side?=?side;
10
????}
11
12
????public?long?getSide()
13
????
{
14
????????return?side;
15
????}
16
17
????public?long?getWidth()
18
????
{
19
????????return?getSide();
20
????}
21
22
????public?long?getHeight()
23
????
{
24
????????return?getSide();
25
????}
26
}

?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

|
|
歡迎大家訪問我的個人網站 萌萌的IT人