?a???s?as?%20t??%20PowerPoint - PowerPoint PPT Presentation

About This Presentation
Title:

?a???s?as?%20t??%20PowerPoint

Description:

LSP Liskov Substitution Principle (???? ?p??at?stas?? t?? Liskov) ... assert(r- getArea() == 20); ????ta? p??s??se? st?? ???s? Rectangle t? ???d? getArea ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 61
Provided by: UserS58
Category:
Tags: 20powerpoint | 20t | assert | in | lsp

less

Transcript and Presenter's Notes

Title: ?a???s?as?%20t??%20PowerPoint


1
????? ??t??e?µe??st?ef??? S?ed?as?? Object
Oriented Design Principles
?etapt???a?? ?????aµµa Sp??d??, ?µ. ?f.
?????f??????
2
S?ed?as?
  • S?ed?as? (?p????d?p?te te?????? ?????) e??a?
  • ? ap?s???es? e??? s?st?µat?? se tµ?µata (µ???de?)

3
S?ed?as?
  • S?ed?as? (?p????d?p?te te?????? ?????) e??a?
  • ? ap?s???es? e??? s?st?µat?? se tµ?µata (µ???de?)
  • ? ?a????sµ?? t?? s??se?? µeta?? t?? tµ?µ?t??

4
S?ed?as?
  • S?ed?as? (?p????d?p?te te?????? ?????) e??a?
  • ? ap?s???es? e??? s?st?µat?? se tµ?µata (µ???de?)
  • ? ?a????sµ?? t?? s??se?? µeta?? t?? tµ?µ?t??
  • ? a???es? a?µ?d??t?t?? se ???e tµ?µa

5
S?ed?as?
  • S?ed?as? (?p????d?p?te te?????? ?????) e??a?
  • ? ap?s???es? e??? s?st?µat?? se tµ?µata (µ???de?)
  • ? ?a????sµ?? t?? s??se?? µeta?? t?? tµ?µ?t??
  • ? a???es? a?µ?d??t?t?? se ???e s??µa
  • ? ep?????s? ?t? ??a ta tµ?µata µa?? ep?t????????
    t??? s??p??? t?? s?st?µat??

6
S?µpt?µata
  • ?? s???st? "?a??" a?t??e?µe??st?ef? s?ed?as? ?
  • S?µpt?µata "?a???" s?ed?as??
  • ??s?aµ??a (Rigidity) ?? s?st?µa e??a? d?s????
    ?a t??p?p????e? d??t? ???e a??a?? ?d??e? se
    p?????a a??a??? se ???a tµ?µata t?? s?st?µat??
  • ????a?st?t?ta (Fragility) ?? a??a??? p??
    p?a?µat?p?????ta? st? ????sµ??? p???a????
    sf??µata se d??f??a s?µe?a.
  • ?????s?a (Immobility) ?p???e? d?s????a
    d?a????sµ?? t?? s?st?µat?? se s?stat??? ta ?p??a
    µp????? ?a epa?a???s?µ?p??????? se ???e?
    efa?µ????.
  • ???e??? ?e?st?t?ta? (Viscosity) ?
    p?a?µat?p???s? t??p?p???se?? µe ????? t??p? e??a?
    e?????te?? ap? t?? p?a?µat?p???s? t??? µe t??
    ???? t??p?.
  • ?e??tt? ????p????t?ta (Needless Complexity) ??
    ????sµ??? pe???aµß??e? st???e?a p?? de? e??a?
    (??te p???e?ta? ?a ??????) ???s?µa.
  • ?e??tt? ?pa?????? (Needless Repetition) ?
    s?ed?as? pe???aµß??e? epa?a?aµßa??µe?e? d?µ?? p??
    ?a µp????sa? ?a e??p??????? ?p? µ?a ?????
    afa??es?.
  • ?d?af??e?a (Opacity) ??s????a ?ata???s?? µ?a?
    µ???da? (se ep?ped? s?ed??? ? ??d??a).

7
?????
  • ????? ??t??e?µe??st?ef??? S?ed?as??
  • ???? t?? ??s?µ?t?s??
  • ???? t?? ?aµ???? S??e????
  • SRP Single Responsibility Principle (???? t??
    ???ad???? ??µ?d??t?ta?)
  • OCP Open-Closed Principle (????
    ?????t??-??e?st?? S?ed?as??)
  • LSP Liskov Substitution Principle (????
    ?p??at?stas?? t?? Liskov)
  • DIP Dependency Inversion Principle (???? t??
    ??t?st??f?? t?? ??a?t?se??)
  • ISP Interface Segregation Principle (???? t??
    ??a????sµ?? t?? ??as??d?se??)
  • ?a?aß?as? µ?a? ? pe??ss?t???? a???? ?d??e? se ??a
    ? pe??ss?te?a ap? ta s?µpt?µata
  • ?? a??t??? a???? de? efa?µ????ta? d?a???? ?a?
    ????? ????. ???? ?? ?e?ape?a se ??p??? s?µpt?µa
    p?? ??e? ?d? d?ap?st??e?. --

8
???? t?? ??s?µ?t?s??
Teµe???d?? ?a???a? ??t??e?µe??st?ef???
?????aµµat?sµ?? (Encapsulation Principle) S??????
efa?µ??eta? a??µa ?a? a? de? s??e?d?t?p??e?ta? ?
???s?µ?t?ta ???? t?? ??s?µ?t?s?? ? es?te????
?at?stas? e??? a?t??e?µ???? p??pe? ?a e??a?
t??p?p???s?µ? µ??? µ?s? t?? d?µ?s?a? d?as??des??
t??
??a?t??? efa?µ??eta? ??t??ta? t?? ??at?t?ta ????
t?? ?d??t?t?? private
9
???? t?? ??s?µ?t?s??
??e????t?µa ??at???s? t?? e?????t?ta? e???
a?t??e?µ???? ???e a?t??e?µe?? pe???aµß??e?
??p??e? a?a?????te?, s?????e? p?? p??pe? ?a e??a?
p??t?te a???e?? ?st? µ?a ???s? TimeStamp
?atas?e?? a?t??e?µ???? TimeStamp T1(23, 45, 17)
10
???? t?? ??s?µ?t?s??
?st? ?t? ??p??a ???s?-pe??t?? t?? TimeStamp
ep??e??e? ?a a???se? ?at? d?? ??e? t? ???????
st??µ? ?? e???
  • Ta p?????e? ??a µ?-?????? a?t??e?µe?? (??a
    254517 !!)
  • ??a?????t? 0 lt hour lt 23
  • ?e t?? efa?µ??? t?? a???? t?? e?s?µ?t?s?? ?
    s?ed?ast?? t?? ???s?? µp??e? ?a e?????e? t??
    e?????t?ta t?? a?t??e?µ????
  • S?ed?as? t?? ?atas?e?ast? ?ste ?a pa???e? µ???
    ?????a a?t??e?µe?a
  • S?ed?as? t?? µe??d?? ?ste ?a µ?? ?ata????? t??
    ?s?? t?? a?a?????t??

11
???? t?? ??s?µ?t?s??
?fa?µ???
12
???? t?? ??s?µ?t?s??
  • S?µpe?asµat???
  • ? pa?aß?as? t?? a???? µp??e? ?a p???a??se?
    ???sµ??a ap? ta s?µa?t???te?a p??ß??µata
  • ?p?t??p??ta? t?? t??p?p???s? t?? t?µ?? t??
    ?d??t?t?? ?ata??e?ta? ? ?µad?p???s? ded?µ???? ?a?
    s?µpe??f????
  • St? s????e?a ?a e??a? d?s???? ?a e?t?p?st???
    p??a tµ?µata ??d??a ep??e????? p??a ded?µ??a

13
???? t?? ?aµ???? S??e????
  • ????de? ?at? t? s?ed?as? ??e???t?ta s?stat???
    ????sµ????
  • µe saf?? ?a????sµ??? ?e?t???????t?ta
  • µe saf?? ?a????sµ??? s????? e?s?d?? ?a? e??d??
  • ??? s?µa?t???? ?????e? st?? ?e???????a ????sµ????
    e??a?
  • ? s??e??? (coupling). ??af??eta? st? ßa?µ?
    e???t?s?? µeta?? d?? s?stat????
  • ? s??e?t???t?ta (cohesion). ??af??eta? st? ßa?µ?
    es?te????? ?e?t???????? s???fe?a? µeta?? t??
    tµ?µ?t?? e??? s?stat????
  • ????? µ???µa ?at? t? d?ad??as?a s?ed?as?? p??pe?
    ?a e??a?
  • ???? t?? ?aµ???? S??e???? Se ??a s??d??
    ????sµ???? p??pe? ?a ep?d???eta? ? ep?te??? t??
    µ????te??? d??at?? s??e???? µeta?? t?? s?stat????
    t??

14
???? t?? ?aµ???? S??e????
??e??e?t?µata ??????te?? ???p???s?, ??e???? ?a?
s??t???s? ?a??de??µa ?a??? s?ed?as?? (?????
s??e???)
  • ?? t??p?p????e? ? ?????µet?? apa?te?ta?
    (t??????st??) ? ??e???? ?a? (e?de??µ????) ?
    µeta???tt?s? t?? s?s?et???µe??? ???se??
  • G?a ?a epa?a???s?µ?p????e? ? ?????µet??
    apa?te?ta? µetaf??? t?? a?af???? t?? p??? t??
    s?s?et???µe?e? ???se??

15
???? t?? ?aµ???? S??e????
  • ? µe??s? t?? s??e???? e??a? d?s???? ?p??es?.
    ???a? ?µ?? e????? ?a µet??ta?
  • ?et???? CBO (Coupling between Objects
    Chidamber Kemerer, 1994)
  • ? t?µ? t?? µet????? ??a µ?a ???s? C ?s??ta? µe
    t?? a???µ? t?? ????? ???se?? µe t?? ?p??e?
    ?p???e? s??e???. O? s??e??? ?e???sa?
  • ???s? µe??d??
  • ?pe??e?a? p??sßas? se µ??? ded?µ???? (a?
    ?p???e?)
  • ??a??e?ta? ? ???????µ???t?ta
  • ?p?s?? ??e? p??ta?e? ?a ?e??e?ta? ?? s??e???
  • ? ???s? t?p?? µ?a? ???s?? ?? pa?aµ?t??? se
    µe??d???
  • ? d?µ??????a a?t??e?µ????
  • ? ?pa??? f?????? ???se??
  • ? ap?st??? µ???µ?t?? at runtime ??p ??p

16
SRP Single Responsibility Principle
  • Tom De Marco (1979) S??e?t???t?ta (Cohesion)
  • ???? t?? ???ad???? ??µ?d??t?ta? ??a ???s? p??pe?
    ?a ??e? µ??? ??a ???? ?a a????e?
  • ???e ???s? de? p??pe? ?a ??e? pe??ss?te?e? ap?
    µ?a a?µ?d??t?te? (axis of change)
  • pe??ss?te??? ap? ??a? ????? a??a???
  • s??e??? µeta?? a?µ?d??t?t??
  • ?st? ? a??????? s?ed?as?

17
SRP Single Responsibility Principle
? efa?µ??? t?? a???? SRP ep?ß???e? t? d?a????sµ?
t?? d?? a?µ?d??t?t?? se d?? te?e??? d?af??et????
???se??
18
SRP Single Responsibility Principle
??s?t???p???s? t?? ßa?µ?? s??e?t???t?ta? µ?a?
???s?? ?et???? LCOM (Lack of Cohesion between
Methods), Chidamber Kemerer, 1994
?at? s???pe?a, ?s? µe?a??te?? ? s??e?t???t?ta,
t?s? µ????te?? ? t?µ? t?? LCOM
19
SRP Single Responsibility Principle
?? e??a? µ?a a?µ?d??t?ta ? Sta p?a?s?a t??
a??t??? a???? µ?a a?µ?d??t?ta ????eta? ?? µ?a
"a?t?a a??a???".
interface Modem public void dial(String
pno) public void hangup() public void
send(char c) public char recv()
?? a?µ?d??t?te? p?? ?p?????? ed? e??a? d??. Ta
p??pe? a?t?? ?a d?a????st??? ? ? ap??t?s?
e?a?t?ta? ap? t?? t??p? µe t?? ?p??? a????e? ?
efa?µ???.
20
SRP Single Responsibility Principle
?? ? efa?µ??? a????e? s???? ?at? t?t??? t??p?
?ste ?a t??p?p??e?ta? ? ?p???af? t?? µe??d??
s??des??, t?te eµfa???eta? d?s?aµ??a ?a??? ??
???se?? p?? ?a???? t?? µe??d??? ep????????a? send
?a? recv ?a p??pe? ???e f??? ?a
epa?aµeta???tt????ta?. Se a?t? t?? pe??pt?s? ??
d?? a?µ?d??t?te? ?a p??pe? ?a d?a????st???. ?e
a?t? t?? t??p?, ?? efa?µ???? p?? ???s?µ?p?????
e?te t? d?as??des? Data Channel, e?te t?
d?as??des? Connection ap?s??d???ta? µeta?? t???.
21
SRP Single Responsibility Principle
?p? t?? ????, a? ? efa?µ??? de? a????e? ?ts? ?ste
?a t??p?p?????ta? ?? a?µ?d??t?te? a?e???t?ta,
t?te de? ?p???e? ????? d?a????sµ?? t???. Se a?t?
t?? pe??pt?s?, ? d?a????sµ?? ?a p???a???se
pe??tt? p???p????t?ta. ?at? s???pe?a, ??a?
????a? a??a??? e??a? ????a? a??a??? µ??? a? ??
a??a??? s?µßa????? p???µat?. S?µpe?asµat??? ?
a??? t?? ???ad???? ??µ?d??t?ta? e??a? e?de??µ????
? ap???ste?? a??? a?t??e?µe??st?ef??? s?ed?as??
a??? e??a? ?a? µ?a ap? t?? d?s????te?e? ?a
efa?µ?ste?. ? s???e?asµ?? a?µ?d??t?t?? e??a?
??t? p?? ?????µe ?at? f?s??? t??p?. ? e?t?p?sµ??
?a? ? d?a????sµ?? a?t?? t?? a?µ?d??t?t?? ap?te?e?
?at? p?????? ??a ??s?ast??? ??µµ?t? t?? s?ed?as??
?a? a??pt???? ????sµ????. ?? pe??ss?te?e? ap?
t?? a???? ?a? t??? ?a???e? p?? ?a s???t????? st?
s????e?a s?et????ta? µe t?? ??a ? ???? t??p? µe
t?? a??? SRP.
22
OCP Open-Closed Principle
"??a ta s?st?µata ????sµ???? a??????? ?at? t?
d????e?a ???? t??? (Only three truths about
life Death, Taxes and Requirement Changes
!!) ???? t?? ?????t??-??e?st?? S?ed?as?? ??
??t?t?te? ????sµ???? (???se??, µ???de?,
s??a?t?se?? ??p) ?a p??pe? ?a e??a? a????t?? ??a
ep??tas?, a??? ??e?st?? ??a t??p?p???s? ?? ? a???
OCP efa?µ?ste? ????, t?te ? ???p???s? pe?a?t???
a??a??? t?? ?d??? t?p?? ep?t?????eta? µe t??
p??s???? ???? ??d??a, ??? µe t?? t??p?p???s?
?p?????t?? ??d??a p?? ?d? ?e?t????e?. ?????t??
??a ep??tas? ? s?µpe??f??? t?? µ???da? µp??e? ?a
epe?ta?e? ??e?st?? ??a t??p?p???s? ? ep??tas?
t?? s?µpe??f???? de? ?d??e? se a??a??? t??
p??a??? ? a?t??e?µe??? ??d??a t?? µ???da?. ???
µp????µe ?a ??a??p???s??µe t?? a?t??????µe?e?
a?t?? ?d??t?te? ?
23
OCP Open-Closed Principle
Abstraction is the key Se ?p??ad?p?te OOPL e??a?
d??at?? ?a ?atas?e?ast??? afa???se?? ?? ?p??e?
e??a? sta?e??? ?a? ?a????sµ??e? a?apa??st??? de
??a ape?????st? p????? s?µpe??f????
??a µ???da e??a? d??at? ?a ?e????eta? µ?a afa??es?
??a µ???da p?? ?e????eta? µ?a afa??es? e??a?
??e?st? ??a t??p?p???se?? ?a??? e?a?t?ta? ap? t??
afa??es? p?? e??a? sta?e??. Ost?s?, ? s?µpe??f???
t?? µ???da? µp??e? ?a epe?ta?e? d?µ???????ta?
??e? ?p????se?? t?? afa??es??.
24
OCP Open-Closed Principle
?a?aß?as? OCP
S?µµ??f?s? µe OCP (STRATEGY Design Pattern)
25
OCP Open-Closed Principle
??a??a?t??? d?µ? (TEMPLATE METHOD Design Pattern)
?a d?? a?t? p??t?pa e??a? ?? s?????ste??? t??p??
s?µµ??f?s?? µe t?? a??? OCP.
26
OCP Open-Closed Principle
  • Shape Application ??a efa?µ??? Painter p??pe? ?a
    µp??e? ?a s?ed???e? ??????? ?a? tet?????a se GUI.
    ??p??? ???? p????aµµa (p.?. µ?a ???s?
    ShapeApplication) d?µ?????e? µ?a ??sta ap?
    ??????? ?a? tet?????a ?a? ? efa?µ??? p??pe? ?a
    d?at???e? t? ??sta ?a? ?a s?ed??se? ???e s??µa
  • ???p???s? se Java (download ??d??a ap? se??da)
    ?at? t? d????e?a t?? e?t??es?? e?et??eta? ? t?p??
    ???e a?t??e?µ????
  • S?µµ??f??eta? ? ???s? Painter µe t?? a??? OCP ?
    (???a? t?? ????? p??ß??µ?t?? ? ???s? Painter
    a?a?aµß??e? ???e? a?µ?d??t?te?)
  • St?? p???? apa?te?ta? ? e?t?p?sµ?? ???? t??
    s?et???? d?µ?? switch ?a? if/else (µeta????s?
    s??µ?t??, resizing, d?a??af?, a??a?? ???µ?t??)
  • ?? e?t???? switch s?????? eµp?????? ????????
    te?est??, ? s??d?????ta? (?????? e????e?e? ??a
    ???sµ??a s??µata)

27
OCP Open-Closed Principle
Bad Design ??s?aµ??a ??e? ?? µ???de? p??
e?a?t??ta? ap? t?? Painter p??pe? ?a
µeta???tt?st??? e? ???? (C) se pe??pt?s?
a??a??? (p.?. p??s???? e??? Triangle). ???s?d?t?
µeta???tt????ta? e? ???? ??a ta s?et???µe?a
a??e?a (e? ???? e??at?stas? ß?ß????????, DLL
??p) ????a?st?t?ta ? p??s???? apa?te? t??
e?t?p?sµ?, ?ata???s? ?a? t??p?p???s? ???? t??
s?et???? e?t???? switch ?a? if/else ?????s?a G?a
µetaf??? t?? Painter apa?te?ta? ?a? ? µetaf???
t?? a??e??? p?? af????? Square ?a? Circle
28
OCP Open-Closed Principle
Improved Design (conformance to OCP) St? s?st?µa
p??st??eta? µ?a ??a af???µ??? ???s? Shape ?a?
e?s?µat??e? ?t?d?p?te µp??e? ?a µetaß???e? H
p??s???? ???? s??µat?? de? ??e? ap???t?? ?aµ?a
ep?d?as? se ?aµ?a ap? t?? a??t???
µ???de? ??s?aµ??a ??a?e?f???e d??t? de?
apa?te?ta? ?a µeta???tt?st??? ???a a??e?a
a?t??e?µe??? ??d??a p?? e??a? ?d? se
?e?t?????a ????a?st?t?ta ??a?e?f???e d??t? de?
apa?te?ta? e?t?p?sµ?? s?µe??? ??d??a if/else ?a?
switch ?????s?a ??a?e?f???e d??t? ? Painter
µp??e? ?a epa?a???s?µ?p????e? ????? ?a p??pe? ?a
µetafe????? ?? ???se?? Circle ?a? Square ??
ße?t??µ??? p????aµµa s?µµ??f??eta? µe t?? a???
OCP. ???p?p??e?ta? p??s??t??ta? ??? ??d??a ?a?
??? a??????ta? ?p?????ta ??d??a
29
LSP Liskov Substitution Principle
?????te??? ???a??sµ?? OOPL ?fa??es?,
????µ??f?sµ?? Se statically typed languages
µ??a??sµ?? ?p?st?????? e??a? ? ???????µ???t?ta ??
s???st? "?a?? ?e?a???a ???se??" ? ????
?p??at?stas?? t?? Liskov ?? pa??????? t?p??
p??pe? ?a µp????? ?a ?p??a??st??? t??? ßas?????
t??? t?p???. Barbara Liskov (MIT, 1988, MIT's
magnificent seven Seven MIT women faculty are
among the 60 top scientists cited in the November
issues of Popular Science and Discover
magazine) ??t? p?? e??a? ep???µ?t? ed? e??a?
??t? sa? t?? a??????? a??? ?p??at?stas?? ?? ??a
???e a?t??e?µe?? o1 t?? t?p?? S ?p???e? ??a
a?t??e?µe?? o2 t?? t?p?? T t?t??? ?ste ??a ??a ta
p?????µµata P p?? ??????ta? ?p? ????? t?? T, ?
s?µpe??f??? t?? P pa?aµ??e? a?a?????t? ?ta? t? ?1
?p??atast?se? t? o2 t?te ? S e??a? pa???????
t?p?? (?p??at?????a) t?? T. p.?. ??a s????? (set)
de? µp??e? ?a ap?te??se? pa?????? t?p? µ?a?
??sta? (list)
30
LSP Liskov Substitution Principle
?st? µ?a s????t?s? f (B ref), if f (D ref)
s?µpe??f??eta? ????? , D subclass of B t?te ? D
pa?aß???e? t?? a??? LSP. ??s? ??e???? µ?sa st??
f a? pe??? ?? ???sµa a?t??e?µe?? t?p?? D ?
???????S? t?? a???? OCP ?a??de??µa (??d??a? µe
???s? te?????? RTTI) --
31
LSP Liskov Substitution Principle
??? "?ept??" pa?aß??se??
class Rectangle public void
setWidth(double w) itsWidth w void
setHeight(double h) itsHeight h double
getArea() const return itsHeight
itsWidth private double itsWidth double
itsHeight
32
LSP Liskov Substitution Principle
??t?µa ??a a??a?? ???at?t?ta s?ed?as?? ?a?
tet?a????? p??a? t?? ?????????? ???eta? s????,
?t? ? ???????µ???t?ta e??a? µ?a s??s? t?p??
"???a?" (IS-A). ?e ???a ????a, a? ??a ??? e?d??
a?t??e?µ???? µp??e? ?a e?p??e? ?t? ??a??p??e? t?
s??s? "???a?" ?? p??? ??a pa?a?? e?d??
a?t??e?µ????, t?te ? ???s? t?? ???? a?t??e?µ????
p??pe? ?a ???????µe? t?? ???s? t?? pa?a???
a?t??e?µ????. G?a ??e? t?? ??????? ???se?? ?a?
s??p???, ??a tet?????? e??a? ??a ?????????
33
LSP Liskov Substitution Principle
1? ??de??? ??a Square de? ??e???eta? ?a? t? ????
?a? t? p??t?? ?? ?d??t?te? (spat??? µ??µ??) 2?
??de??? ???????µ?s? t?? µe??d?? setWidth ?a?
setHeight. ??s?
void SquaresetWidth(double w)
RectanglesetWidth(w) RectanglesetHeight(w)
  void SquaresetHeight(double h)
RectanglesetHeight(h) RectanglesetWidth(h)

?et? t?? ???s? ?p??asd?p?te µe??d?? t?
a?t??e?µe?? Square pa?aµ??e? ?e?µet????? ????
tet?????? (invariants ????te?)
34
LSP Liskov Substitution Principle
?st? ?t? se ??p??a efa?µ???
void f(Rectangle r) r-gtsetWidth(32) //?a?e?
ta? ? RectanglesetWidth
?a?aß?as? a???? LSP ? s????t?s? f de? ?e?t????e?
??a pa?????e? ???se?? t?? pa?aµ?t??? t?? (?a
????e? ? setWidth t?? Rectangle ?a? ??? t??
Square) G?a t? d?????s? t?? p??ß??µat?? p??pe? ?a
t??p?p???s??µe t? ßas??? ???s? Rectangle.
?a?aß?as? a???? OCP
35
class Rectangle public virtual void
setWidth(double w) itsWidth w virtual
void setHeight(double h) itsHeight h
double getHeight() const return
itsHeight double getWidth() const
return itsWidth   private Point
itsTopLeft double itsWidth double
itsHeight   class Square public Rectangle
public virtual void setWidth(double w)
virtual void setHeight(double h)   void
SquaresetWidth(double w) RectanglesetWidth
(w) RectanglesetHeight(w)   void
SquaresetHeight(double h)
RectanglesetHeight(h) RectanglesetWidth(h)

36
LSP Liskov Substitution Principle
The Real Problem ? s?ed?as? fa??eta? s??ep?? ??
p??? t?? ea?t? t??. O? p??? t??? ?????? ? ?st? ?
s????t?s?
void g(Rectangle r) r-gtsetWidth(5)
r-gtsetHeight(4) assert(r-gtgetArea()
20) ????ta? p??s??se? st?? ???s? Rectangle t?
µ???d? getArea
? s????t?s? ?e?t????e? t??e?a ??a a?t??e?µe?a
Rectangle. ????a?e? assertion error a? pe??s??µe
a?t??e?µe?? Square. ? s????af?a? t?? g ?e???se
?t? a??????ta? t? p??t?? e??? a?t??e?µ????
Rectangle, t? ???? t?? pa?aµ??e? a??paf?
37
LSP Liskov Substitution Principle
???a??? e??a? t? sf??µa ? "?? p??ß??µa ???e?ta?
st?? g" ? s????af?a? t?? de? e??e t? d??a??µa
?a ?p???se? ?t? t? p??t?? ?a? t? ???? e??a?
a?e???t?ta. Ost?s?, ?p?????? a?a?????te? ??a ??a
?????????, ?a? µ?a ap? a?t?? e??a? ?t? ?? d??
d?ast?se?? e??a? a?e???t?te? ? s????af?a? t??
Square pa?aß?ase a?t? t?? a?a?????t?, ?????ta?
?t? t? tet?????? "e??a?" ??a ????????? ??t? p??
??e? e?d?af???? e??a? ?t? ? s????af?a? t?? Square
de? pa?aß?ase µ?a a?a?????t? t?? Square.
???????µ??ta? t?? Rectangle, ? s????af?a? t??
Square pa?aß?ase µ?a a?a?????t? t?? Rectangle !
??a µ??t??? p?? e?et??eta? µeµ???µ??a de? µp??e?
?a ep??????e? p?????. ?? s???ße?? ?e? e??a?
te???? ??a tet?????? ?a? ??a ????????? ? ?s??
af??? t? s?µpe??f??? ???. ? a??? LSP ep?ß??e? ?t?
se µ?a s??s? ???????µ???t?ta? ? s?µpe??f??? t??
pa??????? ???se?? p??pe? ?a µp??e? ?a
?p??atast?se? t? s?µpe??f??? t?? ßas???? ???se??
38
LSP Liskov Substitution Principle
Design by Contract ? s????af?a? ???e ???s??
d?at?p??e? ??t? t?? s?µß?se?? ?p? t?? ?p??e?
?e?t????e? s?st? ? ???s?. ?p???sd?p?te ???fe?
p????aµµa pe??t? a?t?? t?? ???s??, µp??e? ?a
ßas?ste? se a?t?? t?? s?µß?se??.
p??s?????e? µ???d?? µetas?????e?
?s?d??aµe? µe "e?? ?s????? ?? p??s?????e? p???
ap? t?? e?t??es? t?? µe??d??, µet? t? p??a? t??
e?t??es?? t?? µe??d??, ?? µetas?????e? ?a p??pe?
?a ?s?????" G?a t? pa??de??µa t?? µe??d??
setWidth e??a?
old.itsHeight itsHeight //? µetaß??t?
old.itsHeight p??pe?
//?a ??e? t?? t?µ? t?? ?????
RectanglesetWidth(double w) (itsWidth w)
(itsHeight old.itsHeight)
39
LSP Liskov Substitution Principle
G?a t?? ???????µ???t?ta ?s??e? ? ep??????? µ?a?
µe??d?? (se µ?a ?p????s?) µp??e? µ??? ?a
a?t??atast?se? t?? a????? p??s?????? µe µ?a ?d?a
? as?e??ste?? ?a? t?? a????? µetas?????? µe µ?a
?d?a ? ?s????te?? ?ta? ??a? pe??t?? ???s?µ?p??e?
??a a?t??e?µe?? µ?s? t?? d?as??des?? t?? ßas????
???s??, ? pe??t?? ??????e? µ??? t?? p??s?????e?
?a? µetas?????e? ??a t?? ßas??? ???s?. ?at?
s???pe?a, ?? pa?????e? ???se?? de? ?a p??pe? ?a
?p???t??? ?t? ?? pe??te? ?a ??a??p???s???
p??s?????e? ?? ?p??e? e??a? ?s????te?e? ap? a?t??
p?? apa?t???ta? ap? t? ßas??? ???s?. ?p?p????,
p??pe? ?a ??a??p????? ??e? t?? µetas?????e? t??
ßas???? ???s?? (? a??µa ?s????te?e?).
? µetas?????? ??a t?? setWidth t?? ???s?? Square
e??a? as?e??ste?? none //OK, ?
p??s?????? e??a? as?e??ste?? SquaresetWidth(do
uble w) itsWidth w //???ß??µa, ?
µetas?????? e??a? as?e??ste?? !!
40
LSP Liskov Substitution Principle
  • S?µpe?asµat???
  • Ta p??pe? ?a e?asfa???eta? (µ?s? e?te???
    e??????) ?t? ? ???s? pa??????? ???se?? se ??a ta
    s?µe?a ?p?? ???s?µ?p?????ta? ?? ßas???? ???se??,
    de? a??????e? t? ?e?t???????t?ta t?? s?st?µat??
  • ? a??? t?? ?p??at?stas?? pa?aß???eta? a? µ?a
    s????t?s? ? p????aµµa pe??t?? ?e?t????e? ??a µ?a
    ßas??? ???s? a??? de? ?e?t????e? ???? ??a
    pa?????e? ???se?? a?t??.

41
DIP Dependency Inversion Principle
???? ??t?st??f?? t?? ??a?t?se?? a. ?? µ???de?
?????? ep?p?d?? de? ?a p??pe? ?a e?a?t??ta? ap?
µ???de? ?aµ???? ep?p?d??. ß. ?? afa???se?? de?
?a p??pe? ?a e?a?t??ta? ap? ?ept?µ??e?e?. ??
?ept?µ??e?e? ?a p??pe? ?a e?a?t??ta? ap?
afa???se??. Se s?µßat???? µe??d?????e? a??pt????
????sµ????, ?p?? ? ??µ?µ??? ?????s? ?a? S?ed?as?
(SADT) d?µ????????ta? d?µ?? ?p?? ?? ?????te?e?
µ???de? st?? ?e?a???a ?a???? (?a? ??a e?a?t??ta?)
t?? µ???de? ?aµ???te??? ep?p?d??. ?? ap?t??esµa
e??a? ? ?e???? st?at????? t?? s?st?µat?? ?a
e?a?t?ta? ap? t?? ?ept?µ??e?e? ???p???s?? t??
µ???d?? ?aµ???? ep?p?d??. (??????s? Roberts ?ef.
11 e?sa????) ??a t?t??a d?µ? e??a? p??fa???
pa?????? ?? µ???de? sta ?????te?a ep?peda
eµpe??????? t??? ?e?????? ?a???e? t?? efa?µ????
?a? t?? ped??? t?? p??ß??µat?? (business rules)
?a? s??ep?? p??pe? ?a ????? p??te?a??t?ta ?a? ?a
e??a? a?e???t?te? ap? t?? ?ept?µ??e?e?
???p???s??. ?p?p????, a?t?? ?? ?a???e? e??a?
s?????? ??a st???e?? p?? ?????µe ?a µp??e? ?a
epa?a???s?µ?p????e?.
42
DIP Dependency Inversion Principle
??ast??µ?t?s? (Layering) Booch µ?a ?a??
a???te?t????? pe????e? saf?? ???sµ??a ep?peda,
?a???a e? t?? ?p???? pa???e? ??a s??ep?? ?a?
s?µpa??? s????? ?p??es??? d?a µ?s?? µ?a? ??t?
d?at?p?µ???? d?as??des??. ?fe??? ?et?f?as?
43
DIP Dependency Inversion Principle
?e?t??µ??? ??ast??µ?t?s? (Inversion)
Hollywood principle "Don't call us, we'll call
you" You implement the interfaces, you get
registered. You get called when the time is
right.
44
DIP Dependency Inversion Principle
  • "?a e?a?t?ste ap? afa???se??"
  • ? ?a???a? p??te??e? ??a t?? ?atas?e?ast? ???e
    ???s?? ?a µ?? e?a?t?ta? ap? s???e???µ??e? ???se??
    ?t? d??ad? ??e? ?? s??se?? se ??a p????aµµa ?a
    p??pe? ?a ?ata?????? se µ?a af???µ??? ???s? ? se
    µ?a d?as??des?. S?µf??a µe a?t?? t?? ?a???a
  • ?aµ?a µetaß??t? de? ?a p??pe? ?a d?at??e? ??a?
    de??t? ? µ?a a?af??? p??? ??p??a s???e???µ???
    ???s?
  • ?aµ?a ???s? de? ?a p??pe? ?a ???????µe? µ?a
    s???e???µ??? ???s?
  • ?aµ?a µ???d?? de? ?a p??pe? ?a ep??a??pte?
    ???p???µ??? µ???d? ?p??asd?p?te ap? t?? ???????
    t?? ???se??
  • ?? a??t??? ?a???e? ?a pa?aß?ast??? t??????st??
    µ?a f???, st? s?µe?? p?? ?atas?e?????µe
    st??µ??t?pa
  • ?p?p????, de? ?p???e? ????? ?a t??? a???????s??µe
    a? ????????µe ?t? µ?a s???e???µ??? ???s? de? ?a
    a????e?

45
DIP Dependency Inversion Principle
?a??de??µa ??a? e?e??t?? (Controller)
a?t??aµß??eta? ??p??a a??a?? st? e??te????
pe??ß????? ?a? ap?st???e? µ???µa
e?e???p???s??/ape?e???p???s?? st? s?et???µe??
a?t??e?µe?? s??a?e?µ?? (LampAlarm)
  • ? ???s? Controller e????e? a?t??e?µe?a
    VisibleAlarm ?a? µ???? a?t?
  • ?? a????e? ? p??a??? ??d??a? t?? LampAlarm (p.?.
    p??s??? f??) ?a p??pe? ?a epa?aµeta???tt?ste?
    (C) ?a? ? ??d??a? t?? Controller
  • ?? ?????µe ?a s??d?s??µe t?? e?e??t? µe ????
    µ??a??sµ? (p.?. ??a? ????t??a), p??pe? ?a
    t??p?p???s??µe t?? e?e??t?
  • ? p???t??? ?????? ep?p?d?? de? ??e? d?a????ste?
    ap? t?? ???p???s?

46
DIP Dependency Inversion Principle
??t?p?sµ?? t?? ?p??e?µe??? ?fa??es?? ?a?
??t?st??f? t?? ????t?s??
  • ? ?p??e?µe?? afa??es? e??a? ? a????e?s? ??p??a?
    a??a??? ?a? ? ap?st??? µ???µat??
    e?e???p???s??/ape?e???p???s?? se ??p????
    pa?a??pt?
  • ????? e??a? ? ???a??sµ?? ?????e?s?? ad??f???
    (?ept?µ??e?a)
  • ???? e??a? t? a?t??e?µe?? pa?a??pt?? ad??f???
    (?ept?µ??e?a)
  • ?fa?µ??? t?? ????? DIP ??t?st??f? t?? e???t?s??
    t?? e?e??t? ap? t? a?t??e?µe?? VisibleAlarm

47
DIP Dependency Inversion Principle
??t?p?sµ?? t?? ?p??e?µe??? ?fa??es?? ?a?
??t?st??f? t?? ????t?s??
??e????a ?a a?t??e?µe?a Controller µp????? ?a
e??????? ?t?d?p?te s?µµ??f??eta? µe t? d?as??des?
ControllerServer. ???µa ?a? a?t??e?µe?a p?? de?
????? a??µ? ep?????e?.
48
DIP Dependency Inversion Principle
  • S?µpe?asµat???
  • ??ad??as?a??? p????aµµat?sµ?? ??µ?? ?p?? ??
    µ???de? ?????? ep?p?d?? e?a?t??ta? ap? t??
    µ???de? ?aµ???? ep?p?d??
  • ?t???? ?p????? ? p???t??? t?? s?st?µat?? e??a?
    e????t? se a??a??? t?? ???p???s??
  • ??t??e?µe??st?ef?? ?????aµµat?sµ?? ??µ??
    a?test?aµµ??e?
  • ?????? ?e????? t? d?af??? a?t? ?? e?d?p???.

49
ISP Interface-Segregation Principle
St? s????e?a, ?? d?as??des? ?e??e?ta? t? s?????
t?? ?e?t??????? µ?a? ??t?t?ta? ? s?ed?as? µe
d?as??d?se?? e??a? ?a?? ta?t???. Ost?s?, ?a
p??pe? ?a e?et??eta? p??se?t??? ? ?d?a ? s?ed?as?
t?? d?as??d?se?? ? a??? ISP d?ap?a?µate?eta? ta
µe???e?t?µata t?? ????d?? d?as??d?se?? ("fat"
interfaces). ?? ???se?? p?? ????? µe???? a???µ?
d?µ?s??? µe??d?? e??a? ???se?? t?? ?p???? ?
d?as??des? de? e??a? s??e?t??? ?a? ?? µ???d??
µp????? ?a d?a????st??? se ??a s?????
d?as??d?se??. ???e s????? e??p??ete? µ?a
d?af??et??? ?µ?da ap? pe??te?. ? a??? ISP
a?a??????e? ?t? ?p?????? a?t??e?µe?a p??
??e???eta? ?a ????? µ? s??e?t???? d?as??d?se??.
Ost?s?, p??te??e? ?t? ?? pe??te? de? ?a p??pe? ?a
????????? a?t? ta a?t??e?µe?a µ?s? µ?a? µ??ad????
???s??. ??t???t??, ?? pe??te? ?a p??pe? ?a
????????? ??a p????? af???µ??e? ???se?? ß?s?? µe
s??e?t???? d?as??d?se??. ???? ??a????sµ?? t??
??as??d?se?? (? ???sµ??) ?????? e?e?d??e?µ??e?
d?as??d?se?? e??a? p??t?µ?te?e? ap? µ?a ?e????
d?as??des?
50
ISP Interface-Segregation Principle
Te????µe ??a s?st?µa asfa?e?a?. St? s?st?µa
?p?????? p??te? (a?t??e?µe?a Door) p?? µp????? ?a
??e?d????? ?a? ?a ?e??e?d?????.
class Door public virtual void lock()
0 virtual void unlock() 0
? ???s? e??a? af???µ??? ?ts? ?ste ?? pe??te? ?a
µp????? ?a ???s?µ?p????? a?t??e?µe?a p??
s?µµ??f????ta? µe t? d?as??des? Door ????? ?a
p??pe? ?a e?a?t??ta? ap? s???e???µ??e?
???p???se?? t?? Door (ß??pe DIP).
51
ISP Interface-Segregation Principle
Te????µe t??a µ?a t?t??a ???p???s? asfa????
p??ta? (ProtectedDoor) ? ?p??a e?e???p??e? ??a?
s??a?e?µ? a? pa?aß?aste?. G?a t? s??p? a?t? ???e
a?t??e?µe?? ProtectedDoor ep???????e? µe ??a ????
a?t??e?µe?? t?? ???s?? PasswordProtector.
class PasswordProtector public void
Register(int code, PasswordClient client) void
check(int code) private int safeNumber Passwo
rdClient myClient
52
ISP Interface-Segregation Principle
???e p??ta (a?t??e?µe?? Door) p?? ep???µe? ?a
asfa??ste? ?a?e? t? µ???d? Register t??
a?t??e?µ???? PasswordProtector ?ata???????ta? t??
ea?t? t??. ?a ???sµata t?? µe??d?? e??a? ??a?
??d???? ??a t?? ??e??? ?a? ??a? de??t?? p??? ??a
a?t??e?µe?? PasswordClient t?? ?p???? t?
?e??e?d?µa ?a e????eta? µe ??d???. ?? ? ??d????
?at? t? ?????µa t?? p??ta? de? e??a? ?d??? µe
a?t?? p?? ?ata???????e, ?a ?a?e?ta? ? µ???d??
alarm() t?? a?t??e?µ???? PasswordClient. G?a
?????? a????t??-??e?st?? s?ed?as??, ?
PasswordClient e??a? µ?a d?as??des? (af???µ???
???s? ß?s??) t?? ?p??a p??pe? ?a ???p??e?
?p???d?p?te a?t??e?µe?? ep???µe? ?a asfa??ste?.
??? µp??e? ep?µ???? ? ???s? PasswordProtector ?a
ep???????e? µe a?t??e?µe?a ProtectedDoor
?p?????? p????? e?a??a?t???? ?s?? af??? t?
s?ed?as? t?? s?st?µat??.
53
ISP Interface-Segregation Principle
?fe??? ???s????s?
54
ISP Interface-Segregation Principle
  • ? a??t??? p?a?t??? e??a? s?????sµ???, d?µ?????e?
    ?µ?? p??ß??µata
  • ? ???s? Door ???p??e? p???? t? d?as??des?
    PasswordClient. ?e? ??e?????ta? ??e? ?? p??te?
    asf??e?a µ?s? ??d????.
  • ?? apa?t???ta? pa??????? t?? Door ?????
    ?e?t???????t?ta ??d????, a?t?? ?? pa???????
    ???se?? ?a p??pe? ?a pa??s???? e?f???sµ??e?
    ???p???se?? ??a t? µ???d? alarm() µ?a
    e?de??µe?? pa?aß?as? t?? a???? LSP. ?p?p????, ??
    efa?µ???? ?? ?p??e? ?a ???s?µ?p???s??? a?t?? t??
    pa?????e? ???se?? ?a p??pe? ?a e?s????? ?a? t??
    ???sµ? t?? d?as??des?? PasswordClient, pa???? p??
    a?t? de? ???s?µ?p??e?ta?. ?d? eµfa?????ta? ta
    s?µpt?µata t?? pe??tt?? p???p????t?ta? ?a? t??
    pe??tt?? epa???????.
  • ? d?as??des? Door ??e? "??pa??e?" µe µ?a µ???d?
    t?? ?p??a de? ??e???eta?. ??a?a???st??e ?a
    s?µpe????ße? a?t? t? µ???d? µ??? p??? ?fe???
    ??p???? ap? t?? ?p????se?? t??. ?? ? p?a?t???
    a?t? epe?ta?e?, t?te ???e f??? p?? µ?a pa???????
    ??e???eta? µ?a ??a µ???d?, ? µ???d?? a?t? ?a
    p??st??eta? st? ßas??? ???s?.

55
ISP Interface-Segregation Principle
  • ?? Door ?a? PasswordClient a?t?p??s?pe????
    d?as??d?se?? p?? ???s?µ?p?????ta? ap? te?e???
    d?af??et????? pe??te?. ?a??? ?? pe??te? e??a?
    ?e????st??, ?? d?as??d?se?? ?a p??pe? ?a
    pa?aµe????? ?e????st?? ep?s??. ? ????? e??a? ?t?
    ?? pe??te? as???? ep????? st?? d?as??d?se?? p??
    ???s?µ?p?????.
  • S??????, ?p?te ?e????µe d???µe?? p?? p???a????
    a??a??? st? ????sµ???, s?ept?µaste ??a p?? ??
    d?as??d?se?? ep?ß????? t??p?p???se?? st???
    ???ste? t???. Ost?s?, ?p???e? ?a? µ?a d??aµ? p??
    ?e?t????e? p??? t?? a?t??et? ?ate????s?.
  • ?? ?p???s??µe ??a pa??de??µa ?t? ? e?e???p???s?
    t?? s??a?e?µ?? ap? t? a?t??e?µe??
    PasswordProtector µp??e? ?a ???e? se d?? ep?peda
    ??tas?? (a?????a µe t? a? ? ?d???t?t?? ß??s?eta?
    µ?sa st? sp?t? ? ???), t?te ? af???µ??? µ???d??
    alarm t?? d?as??des?? PasswordClient p??pe? ?a
    t??p?p????e?. ? a??a?? e??a? s?et??? ap??
    (p??s???? µ?a? pa?aµ?t??? int st? ??sta t??
    pa?aµ?t??? t?? alarm) ?a? t?? ap?de??µaste ?a???
    p???e?ta? ??a µ?a ?????? apa?t?s?.

56
ISP Interface-Segregation Principle
  • ??a t?t??a a??a?? e??a? a?aµe??µe?? ?t? ?a
    ep??e?se? ????? t??? ???ste? t?? PasswordClient.
    Ost?s?, µe t? s?ed?as? t?? s??µat?? 5.1 ?a p??pe?
    ?a t??p?p????e? ?a? ? ???s? Door ?a??? ?a? ????
    ?? pe??te? t?? Door!
  • ??t?st???a ?a p??pe? ?a epa?aµeta???tt?st??? ?a?
    ??e? ?? efa?µ???? p?? t?? ???s?µ?p?????).
  • G?a p??? ???? µ?a µ???? ße?t??s? p?? af??? t??
    e?e???p???s? s??a?e?µ?? ap? ????? ??d??? ?a ??e?
    ?p??ad?p?te ep?d?as? se pe??te? pa??????? ???se??
    t?? Door ?? ?p??e? de? ????? ap???t?? ?aµ?a
    a????? ap? ??e??? ??d???? ?d? µ?a a??a?? se ??a
    tµ?µa t?? s?st?µat?? ep??e??e? ???a, te?e???
    ?s?eta tµ?µata, a??????ta? t? ??st??, t? d?s????a
    ?a? t? ??s?? p?a?µat?p???s?? a??a??? d?aµat???.
  • ???? ??a????sµ?? t?? ??as??d?se?? (? ???sµ??)
  • ?? pe??te? de? ?a p??pe? ?a e?a?a??????ta? se
    e???t?s? ap? µe??d??? p?? de? ???s?µ?p?????.

57
ISP Interface-Segregation Principle
  • ???µa ?µ?? ?a? a? d?a????st??? ?? d?as??d?se??,
    t? te???? ??t??µe?? e??a? ??a a?t??e?µe?? p?? ?a
    e?s?µat??e? ?a? ta d?? e?d? ?e?t???????t?ta?

2 ??se??
58
ISP Interface-Segregation Principle
??a????sµ?? µ?s? ?p?st???? ????µ?t?? (Delegation)
59
ISP Interface-Segregation Principle
??a????sµ?? µ?s? ????ap??? ???????µ???t?ta?
60
ISP Interface-Segregation Principle
S?µpe?asµat??? Se µ?a ?a?? s?ed?as?, ?? pe??te?
?a p??pe? ?a e?a?t??ta? µ??? ap? t?? µe??d??? t??
?p??e? ?a???? ??t? ep?t?????eta? d?a???????ta? t?
d?as??des? se p????? d?as??d?se??, µ?a ??a ???e
?at?????a pe?at??
Write a Comment
User Comments (0)
About PowerShow.com