第3章

這天大B問小A:“怎樣設計可複用的麵向對象軟件?”

小A:“師兄你這是考我麽?”

大B:“啥啊?我這是想看你在學校是不是真學到了東西。”

小A:“得得得,那我就說說吧!設計模式是一套被反複使用、多數人知曉的、經過分類編目的、代碼設計經驗的總結。使用設計模式是為了可重用代碼、讓代碼更容易被他人理解、保證代碼可靠性。設計模式於己於他人於係統都是多贏的,設計模式使代碼編製真正工程化,設計模式是軟件工程的基石,如同大廈的一塊塊磚石一樣。”

大B:“我再考考你,用C++、Java、C#或VB.NET任意一種麵向對象語言實現一個簡單程序。”

小A不到幾分鍾就給大B一個程序。

/*

*@(#)Blah.java1.8299/03/18

*Copyright(c)1994-1999SunMicrosystems,Inc.

*901SanAntonioRoad,PaloAlto,California,94303,U.S.A.

*Allrightsreserved.

*ThissoftwareistheconfidentialandproprietaryinformationofSun

*Microsystems,Inc.(“ConfidentialInformation”).Youshallnot

*disclosesuchConfidentialInformationandshalluseitonlyin

*accordancewiththetermsofthelicenseagreementyouenteredinto

*withSun.

*/

packagejava.blah;

importjava.blah.blahdy.BlahBlah;

/**

*Classdescriptiongoeshere.

*

*@version1.82

*@authorFirstnameLastname

*/

publicclassBlahextendsSomeClass{

/*Aclassimplementationmentcangohere.*/

*

*classVar2documentationmentthathappenstobe

*morethanonelinelong

*/

privatestaticObjectclassVar2;

/**instanceVar1documentationment*/

publicObjectinstanceVar1;

/**instanceVar2documentationment*/

protectedintinstanceVar2;

/**instanceVar3documentationment*/

privateObject[]instanceVar3;

/**

publicclassBlahextendsSomeClass{

/*Aclassimplementationmentcangohere.*/

*

*classVar2documentationmentthathappenstobe

*morethanonelinelong

*/

privatestaticObjectclassVar2;

/**instanceVar1documentationment*/

publicObjectinstanceVar1;

/**instanceVar2documentationment*/

protectedintinstanceVar2;

/**instanceVar3documentationment*/

privateObject[]instanceVar3;

/**

*……constructorBlahdocumentationment……

*/

publicBlah(){

//……implementationgoeshere……

}

/**

*……methoddoSomethingdocumentationment……

*/

publicvoiddoSomething(){

//……implementationgoeshere……

}

/**

*……methoddoSomethingElsedocumentationment……

*@paramsomeParamdescription

*/

publicvoiddoSomethingElse(ObjectsomeParam){

//……implementationgoeshere……

}

}