Professional Documents
Culture Documents
!"#$#%$&''()%*$+,-./
'0)12)'(%3$"1$+1)'%0$4/
5'&06$78
!"#$%&'()*+#%, -%))%.
/,&'012'0303'·'4'5"6'&+,*
!"#$%&'()*$+,-'.&.,.&)/$01&/2&34#5
6#1&*#7$248''#'$9,'.$-#
',-'.&.,.8-4#$:)1$."#&1$-8'#$248''#';
What does “substitutable” in this case mean?
Does it mean that the application behavior
should remain the same? Absolutely not,
because that is the base of polymorphism. You
want to be able to change the behavior of code
just by injecting instances with different types
into classes.
+)<$="8.$=),47$&.$9#8/$.)$-1#8($."&'
31&/2&34#>
To understand how we can end up breaking
this principle, let’s take one more Sniper 3D
example.
?/.#1:82#'$@$8$'9844$7#.),1
Before talking about the next two principles,
let’s make a detour to talk about to something
that we don’t see much on Unity tutorials —
interfaces.
!"#$6#3#/7#/2A$?/*#1'&)/$01&/2&34#5
6#3#/7$)/$8-'.182.&)/'<$/).$)/
2)/21#.&)/';
I’ll invert things a bit and start talking about
the Dependency Inversion Principle before the
Interface Segregation one.
double progressPercentage,
double maxProgress)
{
#if UNITY_IOS || UNITY_ANDROID
UnityEngine.Social.ReportProgress(i
d,
progressPercentage,
b => { });
#else
SteamWorks.IndicateAchievementProgr
ess(id,
(uint)progressPercentage,
(uint)maxProgress);
#endif
}
}
public void
UnlockAchievement(string id,
double progressPercentage,
double maxProgress)
{
platform.ReportProgress(id,
progressPercentage, maxProgress);
}
}
public interface
IAchievementPlatform
{
void ReportProgress(string id,
double progressPercentage,
double maxProgress);
}
SteamWorks.IndicateAchievementProgr
ess(id,
(uint)progressPercentage,
(uint)maxProgress);
}
}
public class
UnityAchievementPlatform :
IAchievementPlatform
{
public void ReportProgress(string
id,
double progressPercentage,
double maxProgress)
{
UnityEngine.Social.ReportProgress(i
d,
progressPercentage,
(b) => { });
}
}
!"#$?/.#1:82#$+#B1#B8.&)/$01&/2&34#5
C8(#$:&/#$B18&/#7$&/.#1:82#'$."8.$81#
24&#/.D'3#2&:&2;
Let’s say hypothetically we want to support
checking a friend’s progress, and only Steam
supports that. The most straightforward way to
do that is, of course, to include a new method
in the IAchievementPlatform interface
public interface
IAchievementPlatform
{
void ReportProgress(string id,
double progressPercentage,
double maxProgress);
double GetFriendProgress(string
userId, string achievementId);
}
E)/24,'&)/
This is a very complex and vital subject. Each of
the principles was created after years of
research and experience. I hope this series of
articles could give you a taste of it and motivate
you to search more about it. You can find
excellent resources in Uncle Bob’s blog and in
his book Clean Architecture.
>,5+'?+@+)%<5+6$ A6B"6++&"6B
14 0
8CDEEAF'GH
F&2.)1$G47#2)8 -%))%.
H&474&:#$+.,7&)'$!#2"$I4)B -%))%.
8")*)"9+'7$:*"%;'";'I:")*"6B'6+J$KB+6+&,$"%6'5%I")+
B,5+;2',6*'"$'$,L+;',')%$'%9'*,$,2'"66%@,$"%62',6*
L6%.)+*B+M'N:&'$+#O'<+%<)+',&+'O+&+'$%';O,&+'O%.
$O+P',&+'I:")*"6B'$O+'I+;$'"6'#),;;'$+#O6%)%BP'$%
"5<&%@+'<+%<)+Q;')"9+'."$O'9:6',6*'"66%@,$"%6M
C)1#$J1)9$C#7&,9
G+0;/#.$5$H#-:)19'$F+$CFE
!"<"6'RO+&"P,6@++$")'"6'$"6P.,@+
K$0#'$):$G7*&2#$B&*#'$E)93,.#1
+2&#/2#$J1#'"98/
8AFKS7DF>'ST(F>'"6'N%<;UV3V
L8/,81A$MN$OPMQ$8.$PNRSNGC
S,5&,;'5%O,5+*
T)=$.)$3,.$?/:);34&'.$&/.)$8$A#44)=
:)47#1
F"#L'FB:P+6
U048AB1),/7V$GW,1#$EX?$4)BY'),12#
2)7#$8/84A'&'
7O:,6BW:
+.8.&)/$E8'&/)'$%8'$F#B8'$E4)'#'.
!)$T817$Z)2($E8'&/)