【徹底參考】http://www.unitymanual.com/thread-22531-1-2.htmlhtml
編程衆所周知,它是屬於腳本化,腳本沒有一個具體的概念跟架構,
致使在項目過程當中,常常出現哪裏須要實現什麼功能,就隨便添加腳本,
結果,就形成了一片混亂,很差管理。
更有甚者,本身的寫的代碼閒置一段時間後,再去想找某個功能的實現,都要在視圖中翻來覆去找半天。
哎!請允許我在此感嘆一聲,這仍是你寫的東西麼?
所以,一個好的設計模式是多麼的重要啊,
那麼,咱們在使用unity3d開發東西的時候,腳本架構到底應該如何來寫?
呵呵...
其實,我也給不了大傢俱體答案,由於每一個人的開發習慣,每一個團隊的開發模式也各有千秋,
so,在此我只作幾種設計模式的總結,
主要參考書籍有《設計模式》《設計模式之禪》《大話設計模式》以及網上一些零散的文章,
但主要內容仍是我本人的一些經驗以及感悟。
寫出來的目的一方面是系統地整理一下,一方面也與廣大的網友分享,
至於大家到底如何使用,
望君斟酌啊!
由於設計模式對編程人員來講,的確很是重要。
固然,若是你們的理解跟我有所不一樣,歡迎留言,你們共同探討。
設計模式六大原則(1):單一職責原則
說到單一職責原則,不少人都會不屑一顧。
由於它太簡單了,稍有經驗的程序員即便歷來沒有讀過設計模式、歷來沒有據說過單一職責原則,在設計軟件時也會自覺的遵照這一重要原則,由於這是常識。
在軟件編程中,誰也不但願由於修改了一個功能致使其餘的功能發生故障。
而避免出現這一問題的方法即是遵循單一職責原則。
雖然單一職責原則如此簡單,而且被認爲是常識,可是即使是經驗豐富的程序員寫出的程序,也會有違背這一原則的代碼存在。
爲何會出現這種現象呢?由於有職責擴散。所謂職責擴散,就是由於某種緣由,職責被分化成了更細的職責。
如:用一個類描述動物呼吸這個場景。
程序員
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
class
Animal
{
public
void
breathe(
string
animal)
{
Debug.Log(animal+
"呼吸空氣"
);
}
}
public
class
Client
{
Animal animal =
new
Animal();
void
Start()
{
animal.breathe(
"牛"
);
animal.breathe(
"羊"
);
animal.breathe(
"豬"
);
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
class
Terrestrial
{
public
void
breathe(String animal){
Debug.Log(animal +
"呼吸空氣"
);
}
}
class
Aquatic
{
public
void
breathe(String animal){
Debug.Log(animal +
"呼吸水"
);
}
}
public
class
Client
{
public
static
void
main(String[] args)
{
Terrestrial terrestrial =
new
Terrestrial();
Debug.Log(terrestrial.breathe(
"牛"
));
Debug.Log(terrestrial.breathe(
"羊"
));
Debug.Log(terrestrial.breathe(
"豬"
));
Aquatic aquatic =
new
Aquatic();
Debug.Log( aquatic.breathe(
"魚"
));
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
class
Animal
{
public
void
breathe(String animal)
{
if
(
"魚"
.equals(animal))
{
Debug.Log((animal+
"呼吸水"
));
}
else
{
Debug.Log((animal+
"呼吸空氣"
));
}
}
}
public
class
Client
{
public
static
void
main(String[] args)
{
Animal animal =
new
Animal();
Debug.Log(animal.breathe(
"牛"
));
Debug.Log(animal.breathe(
"羊"
));
Debug.Log(animal.breathe(
"豬"
));
Debug.Log(animal.breathe(
"魚"
));
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class
Animal
{
public
void
breathe(String animal){
Debug.Log(animal+
"呼吸空氣"
);
}
public
void
breathe2(String animal){
Debug.Log(animal+
"呼吸水"
);
}
}
public
class
Client
{
public
static
void
main(String[] args)
{
Animal animal =
new
Animal();
Debug.Log(animal.breathe(
"牛"
));
Debug.Log(animal.breathe(
"羊"
));
Debug.Log(animal.breathe(
"豬"
));
Debug.Log(animal.breathe2(
"魚"
));
}
}
|
須要說明的一點是單一職責原則不僅是面向對象編程思想所特有的,只要是模塊化的程序設計,都適用單一職責原則。編程
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
[/size][/font][/color]
[color=#000][font=宋體][size=13px]
class
A
{
public
int
func1(
int
a,
int
b)
{
return
a - b;
}
}
public
class
Client
{
void
Start()
{
A a =
new
A();
Debug.Log((
"100-50="
+a.func1(100, 50));
Debug.Log((
"100-80="
+a.func1(100, 80)));
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
|
[/size][/font][/color]
[color=#000][font=宋體][size=2]
class
B:A{
public
int
func1(
int
a,
int
b){
return
a+b;
}
public
int
func2(
int
a,
int
b){
return
func1(a,b)+100;
}
}
public
class
Client{
void
Start()
{
B b =
new
B();
Debug.Log(
"100-50="
+b.func1(100, 50));
Debug.Log(
"100-80="
+b.func1(100, 80));
Debug.Log(
"100+20+100="
+b.func2(100, 20));
}
|