【彻底参考】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));
}
|