锘??xml version="1.0" encoding="utf-8" standalone="yes"?>伊人婷婷综合缴情亚洲五月,亚洲中文精品久久久久久不卡,久久久久亚洲AV无码专区体验http://www.tkk7.com/dodoma/category/9061.htmlzh-cnFri, 02 Mar 2007 06:52:09 GMTFri, 02 Mar 2007 06:52:09 GMT60java铏氭嫙鏈鴻鐐?http://www.tkk7.com/dodoma/archive/2006/04/05/39445.htmldodomadodomaWed, 05 Apr 2006 10:25:00 GMThttp://www.tkk7.com/dodoma/archive/2006/04/05/39445.htmlhttp://www.tkk7.com/dodoma/comments/39445.htmlhttp://www.tkk7.com/dodoma/archive/2006/04/05/39445.html#Feedback0http://www.tkk7.com/dodoma/comments/commentRss/39445.htmlhttp://www.tkk7.com/dodoma/services/trackbacks/39445.html1:鎶借薄瑙勮寖
2:涓涓叿浣撳疄鐜?br />3:涓涓櫄鎷熸満瀹炰緥

java铏氭嫙鏈虹殑鐢熷懡鍛ㄦ湡
java铏氭嫙鏈虹殑澶╄亴灝辨槸:榪愯涓涓猨ava紼嬪簭.褰撲竴涓猨ava紼嬪簭榪愯寮濮嬭繍琛屾椂,涓涓櫄鎷熸満瀹炰緥灝變駭鐢熶簡(jiǎn).褰撲竴涓綆楁満涓婂悓鏃惰繍琛屼笁涓猨ava紼嬪簭.鍒欏皢浜х敓涓変釜java铏氭嫙鏈哄疄渚?姣忎釜紼嬪簭榪愯鍦ㄨ嚜宸辯殑铏氭嫙鏈洪噷闈?涓嶄細(xì)騫叉壈.褰撶▼搴忚繍琛屽畬姣曟椂,铏氭嫙鏈哄皢鑷姩閫鍑?

java铏氭嫙鏈洪噷闈㈡湁涓ょ綰跨▼,瀹堟姢綰跨▼鍜岄潪瀹堟姢綰跨▼.瀹堟姢綰跨▼鏄java铏氭嫙鏈鴻嚜宸辯殑綰跨▼,濡傚瀮鍦炬敹闆嗙嚎紼?鑰岄潪瀹堟姢綰跨▼鍒欐槸java涓繍琛岀殑紼嬪簭綰跨▼.褰撻潪瀹堟姢綰跨▼閮借繍琛屽畬浜?java铏氭嫙鏈哄皢閫鍑?

涓涓猨ava铏氭嫙鏈轟富瑕佸寘鎷簡(jiǎn):綾昏漿杞藉瓙緋葷粺,榪愯鏃舵暟鎹尯,鎵ц寮曟搸,鍐呭瓨鍖虹瓑絳?

榪愯鏃舵暟鎹尯------涓昏鏄?1 鍫?2聽(tīng) 鏂規(guī)硶鍖?3 java鏍?br />
鍫嗗拰鏂規(guī)硶鍖哄铏氭嫙鏈哄疄渚嬩腑鎵鏈夌殑瀵硅薄閮芥槸鍏變韓鐨?鑰宩ava鏍堝尯,鏄姣忎釜綰跨▼閮芥槸鐙珛鐨? 褰撲竴涓猚lass琚澆鍏ュ埌 classloader涓椂,浼?xì)瑙f瀽瀹冪殑绫诲瀷淇℃?鎶婅繖浜涚被鍨嬩俊鎭斁鍒版柟娉曞尯,鑰屾妸紼嬪簭涓繍琛岀殑瀵硅薄,鏀懼埌鍫嗗尯.褰撲竴涓柊綰跨▼琚垱寤?灝卞垎閰嶄竴涓柊鐨刯ava鏍?java鏍堜腑淇濆瓨鐨?鏄柟娉曚腑鐨勪竴浜涘彉閲?鐘舵?java鏍堟槸鐢卞緢澶氱殑java鏍堝撫緇勬垚鐨?涓涓爤甯у寘鍚簡(jiǎn)涓涓柟娉曡繍琛岀殑鐘舵?褰撲竴涓柟娉曡鎵ц鐨勬椂鍊?灝卞帇鍏ヤ竴涓柊鐨刯ava鏍堝撫鍒癹ava鏍堜腑,鏂規(guī)硶榪斿洖鐨勬椂鍊?灝辨妸鏍堝撫寮瑰嚭鏉?鎶涘純鎺?


鏂規(guī)硶鍖?br />
鍦╦ava铏氭嫙鏈轟腑,琚杞界殑綾葷殑綾誨瀷淇℃伅鍜岀被鐨勯潤(rùn)鎬佸彉閲忚瀛樺偍鍦ㄦ柟娉曞尯榪欐牱鐨勫唴瀛橀噷闈?java紼嬪簭榪愯鏃?浼?xì)鏌ユ壘杩欎簺涓俊鎭?鏂規(guī)硶鍖虹殑澶у皬,鏄姩鎬佺殑.涔熷彲浠ヤ笉鏄繛緇殑.鍙嚜鐢卞湪鍫嗕腑鍒嗛厤.涔熷彲浠ョ敱鐢ㄦ埛鎴栬呯▼搴忓憳鎸囧畾.鏂規(guī)硶鍖哄彲琚瀮鍦炬敹闆?

鏂規(guī)硶鍖哄彲浠ヤ繚瀛樹(shù)互涓嬩俊鎭?br />榪欎釜綾誨瀷鐨勫叏闄愬畾鍚?br />鐩存帴瓚呯被鐨勫叏闄愬畾鍚?br />鏄被綾誨瀷榪樻槸鎺ュ彛
綾誨瀷鐨勮闂慨楗扮
浠諱綍鐩存帴瓚呯被鎺ュ彛鐨勫叏闄愬畾鍚嶇殑鏈夊簭鍒楄〃.
璇ョ被鍨嬬殑甯擱噺姹?br />瀛楁淇℃伅 綾諱腑澹版槑鐨勬瘡涓瓧孌靛強(qiáng)鍏墮『搴?濡傚瓧孌靛悕,綾誨瀷.淇グ絎﹀彿.
鏂規(guī)硶淇℃伅:濡傛柟娉曞悕,榪斿洖綾誨瀷.鍙傛暟琛ㄥ垪.淇グ絎﹀彿.瀛楄妭鐮?鎿嶄綔鏁版爤鍜屾爤甯т腑灞閮ㄥ彉閲忓尯澶у皬絳夌瓑
綾婚潤(rùn)鎬佸彉閲?br />涓涓埌綾籧lassloader鐨勫紩鐢?br />涓涓埌class綾葷殑寮曠敤


鍫?br />鐢ㄦ潵瀛樺偍榪愯鏃剁殑瀵硅薄瀹炰緥

java鏍?br />姣忓惎鍔ㄤ竴涓柊鐨勭嚎紼?灝變細(xì)鍒嗛厤鍒頒竴涓猨ava鏍?java鏍堜互甯т負(fù)鍗曚綅淇濆瓨綰跨▼鐨勮繍琛岀姸鎬?瀹冩湁涓ょ鎿嶄綔.鍏ユ爤,鍑烘爤.
褰撲竴涓柟娉曡璋冪敤鏃?鍏ユ爤,褰撲竴涓柟娉曡繑鍥炴椂,鍑烘爤,鎴栬呭綋鏂規(guī)硶鍑虹幇寮傚父.涔熷嚭鏍?

鏍堝撫
緇勬垚閮ㄥ垎 灞閮ㄥ彉閲忓尯,鎿嶄綔鏁版爤,甯ф暟鎹尯.


dodoma 2006-04-05 18:25 鍙戣〃璇勮
]]>
effective_java闃呰絎旇5http://www.tkk7.com/dodoma/archive/2006/03/31/38503.htmldodomadodomaFri, 31 Mar 2006 08:35:00 GMThttp://www.tkk7.com/dodoma/archive/2006/03/31/38503.htmlhttp://www.tkk7.com/dodoma/comments/38503.htmlhttp://www.tkk7.com/dodoma/archive/2006/03/31/38503.html#Feedback0http://www.tkk7.com/dodoma/comments/commentRss/38503.htmlhttp://www.tkk7.com/dodoma/services/trackbacks/38503.htmlWhen you switch from a language with manual memory management, such as C or C++, to agarbage-collected language, your job as a programmer is made much easier by the fact that your objects are automatically reclaimed when you're through with them. It seems almost like magic when you first experience it. It can easily lead to the impression that you don't have to think about memory management, but this isn't quite true.
java涓篃瑕佹敞鎰忓唴瀛樼鐞?br />
// Can you spot the "memory leak"?
public class Stack {
private Object[] elements;
private int size = 0;
public Stack(int initialCapacity) {
this.elements = new Object[initialCapacity];
}
Effective Java: Programming Language Guide
17
public void push(Object e) {
ensureCapacity();
elements[size++] = e;
}
public Object pop() {
if (size == 0)
throw new EmptyStackException();
return elements[--size];
}
/**
* Ensure space for at least one more element, roughly
* doubling the capacity each time the array needs to grow.
*/
private void ensureCapacity() {
if (elements.length == size) {
Object[] oldElements = elements;
elements = new Object[2 * elements.length + 1];
System.arraycopy(oldElements, 0, elements, 0, size);
}
}
}

榪欓噷鏈変竴涓唴瀛樻硠婕?br />濡傛灉涓涓爤鍏堟槸澧為暱錛岀劧鍚庡啀鏀剁緝錛岄偅涔堬紝浠庢爤涓脊鍑烘潵鐨勫璞″皢涓嶄細(xì)琚綋鍋氬瀮鍦懼洖鏀訛紝鍗充嬌浣跨敤鏍堢殑瀹㈡埛紼嬪簭涓嶅啀寮曠敤榪欎簺瀵硅薄錛屽畠浠篃涓嶄細(xì)琚洖鏀躲傝繖鏄洜涓猴紝鏍堝唴閮ㄧ淮鎶ょ潃瀵硅繖浜涘璞$殑榪囨湡寮曠敤錛坥bsolete re f e re n c e錛夈傛墍璋撹繃鏈熷紩鐢紝鏄寚姘歌繙涔熶笉浼?xì)鍐嶈瑙i櫎鐨勫紩鐢?br />
鏀規(guī)
public Object pop() {
if (size==0)
throw new EmptyStackException();
Object result = elements[--size];
elements[size] = null; // Eliminate obsolete reference娓呴櫎寮曠敤
return result;
}

褰撶▼搴忓憳絎竴嬈¤綾諱技榪欐牱鐨勯棶棰樺洶鎵扮殑鏃跺欙紝浠栦滑寰寰浼?xì)杩囧垎灏忓績(jī)锛?xì)瀵逛簬姣忎竴涓璞″紩鐢紝涓鏃︾▼搴忎笉鍐嶇敤鍒板畠錛屽氨鎶婂畠娓呯┖銆傝繖鏍峰仛鏃㈡病蹇呰錛屼篃涓嶆槸鎴戜滑鎵鏈熸湜鐨勶紝鍥犱負(fù)榪欐牱鍋氫細(xì)鎶婄▼搴忎唬鐮佸紕寰楀緢涔憋紝騫朵笖鍙互鎯沖儚榪樹(shù)細(xì)闄嶄綆紼嬪簭鐨勬ц兘銆傗滄竻絀哄璞″紩鐢ㄢ濊繖鏍風(fēng)殑鎿嶄綔搴旇鏄竴縐嶄緥澶栵紝鑰屼笉鏄竴縐嶈鑼冭涓恒傛秷闄よ繃鏈熷紩鐢ㄦ渶濂界殑鏂規(guī)硶鏄噸鐢ㄤ竴涓湰鏉ュ凡緇忓寘鍚璞″紩鐢ㄧ殑鍙橀噺錛屾垨鑰呰榪欎釜鍙橀噺緇撴潫鍏剁敓鍛藉懆鏈熴傚鏋滀綘鏄湪鏈绱у噾鐨勪綔鐢ㄥ煙鑼冨洿鍐呭畾涔夋瘡涓涓彉閲忥紙瑙佺2 9鏉★級(jí)錛屽垯榪欑鎯呭艦灝變細(xì)鑷劧鑰岀劧鍦板彂鐢熴傚簲璇ユ敞鎰忓埌錛屽湪鐩墠鐨凧 V M瀹炵幇騫沖彴涓婏紝浠呬粎閫鍑哄畾涔夊彉閲忕殑浠g爜鍧楁槸涓嶅鐨勶紝瑕佹兂浣垮紩鐢ㄦ秷澶憋紝蹇呴』閫鍑哄寘鍚鍙橀噺鐨勬柟娉曘?br />
涓鑸岃█錛屽彧瑕佷竴涓被鑷繁綆$悊瀹冪殑鍐呭瓨錛岀▼搴忓憳灝卞簲璇ヨ鎯曞唴瀛樻硠婕忛棶棰樸備竴鏃︿竴涓厓绱犺閲婃斁鎺夛紝鍒欒鍏冪礌涓寘鍚殑浠諱綍瀵硅薄寮曠敤搴旇瑕佽娓呯┖銆?br />
鍐呭瓨娉勬紡鐨勫彟涓涓父瑙佹潵婧愭槸緙撳瓨銆備竴鏃︿綘鎶婁竴涓璞″紩鐢ㄦ斁鍒頒竴涓紦瀛樹(shù)腑錛屽畠?yōu)寰堝?guī)槗琚仐蹇樻帀錛屼粠鑰屼嬌寰楀畠涓嶅啀鏈夌敤涔嬪悗寰堥暱涓孌墊椂闂村唴浠嶇劧鐣欏湪緙撳瓨?shù)?br />

dodoma 2006-03-31 16:35 鍙戣〃璇勮
]]>
effective_java闃呰絎旇4http://www.tkk7.com/dodoma/archive/2006/03/31/38485.htmldodomadodomaFri, 31 Mar 2006 07:19:00 GMThttp://www.tkk7.com/dodoma/archive/2006/03/31/38485.htmlhttp://www.tkk7.com/dodoma/comments/38485.htmlhttp://www.tkk7.com/dodoma/archive/2006/03/31/38485.html#Feedback0http://www.tkk7.com/dodoma/comments/commentRss/38485.htmlhttp://www.tkk7.com/dodoma/services/trackbacks/38485.htmlIt is often appropriate to reuse a single object instead of creating a new functionally equivalent object each time it is needed. Reuse can be both faster and more stylish. An object can always be reused if it is immutable

String s = new String("silly"); // DON'T DO THIS!

String s = "No longer silly";//do this

In addition to reusing immutable objects, you can also reuse mutable objects that you know will not be modified. Here is a slightly more subtle and much more common example of what not to do, involving mutable objects that are never modified once their values have been computed:
闄や簡(jiǎn)閲嶇敤闈炲彲鍙樼殑瀵硅薄涔嬪錛屽浜庨偅浜涘凡鐭ヤ笉浼?xì)琚慨鏀圭殑鍙彉瀵硅薄锛屼綘涔熷彲浠ラ噸鐢ㄥ畠浠?br />
such as
public class Person {
private final Date birthDate;
// Other fields omitted
public Person(Date birthDate) {
this.birthDate = birthDate;
}
// DON'T DO THIS!
public boolean isBabyBoomer() {
Calendar gmtCal =
Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
Date boomStart = gmtCal.getTime();
gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
Date boomEnd = gmtCal.getTime();
return birthDate.compareTo(boomStart) >= 0 &&
birthDate.compareTo(boomEnd) < 0;
}
}

鏀硅壇鐨勭増鏈?
The isBabyBoomer method unnecessarily creates a new Calendar, TimeZone, and two Date instances each time it is invoked. The version that follows avoids this inefficiency with a static initializer:

class Person {
private final Date birthDate;
public Person(Date birthDate) {
this.birthDate = birthDate;
}
/**
* The starting and ending dates of the baby boom.
*/
private static final Date BOOM_START;
private static final Date BOOM_END;
static {
Calendar gmtCal =
Calendar.getInstance(TimeZone.getTimeZone("GMT"));
gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0);
BOOM_START = gmtCal.getTime();
gmtCal.set(1965, Calendar.JANUARY, 1, 0, 0, 0);
BOOM_END = gmtCal.getTime();
}
public boolean isBabyBoomer() {
return birthDate.compareTo(BOOM_START) >= 0 &&
birthDate.compareTo(BOOM_END) < 0;
}
}

銆備竴涓傞厤鍣ㄦ槸鎸囪繖鏍蜂竴涓璞★細(xì)瀹冩妸鍔熻兘濮?br />鎵樼粰鍚庨潰鐨勪竴涓璞★紝浠庤屼負(fù)鍚庨潰鐨勫璞℃彁渚涗竴涓彲閫夌殑鎺ュ彛銆傜敱浜庨傞厤鍣ㄩ櫎浜?jiǎn)鍚庨潰鐨勫璞′箣澶栧Q屾病鏈夊叾浠栫殑鐘舵佷俊鎭紝鎵浠ラ拡瀵規(guī)煇涓粰瀹氬璞$殑鐗瑰畾閫傞厤鍣ㄨ岃█錛屽畠涓嶉渶瑕佸垱寤哄涓傞厤鍣ㄥ疄渚嬨?br />
This item should not be misconstrued to imply that object creation is expensive and should be avoided. On the contrary, the creation and reclamation of small objects whose constructors do little explicit work is cheap, especially on modern JVM implementations. Creating additional objects to enhance the clarity, simplicity, or power of a program is generally a good thing.
Conversely, avoiding object creation by maintaining your own object pool is a bad idea unless the objects in the pool are extremely heavyweight. A prototypical example of an object that does justify an object pool is a database connection. The cost of establishing the connection is sufficiently high that it makes sense to reuse these objects. Generally speaking, however, maintaining your own object pools clutters up your code, increases memory footprint, and harms performance. Modern JVM implementations have highly optimized garbage collectors that easily outperform such object pools on lightweight objects.

dodoma 2006-03-31 15:19 鍙戣〃璇勮
]]>
effective_java闃呰絎旇3http://www.tkk7.com/dodoma/archive/2006/03/30/38371.htmldodomadodomaThu, 30 Mar 2006 14:22:00 GMThttp://www.tkk7.com/dodoma/archive/2006/03/30/38371.htmlhttp://www.tkk7.com/dodoma/comments/38371.htmlhttp://www.tkk7.com/dodoma/archive/2006/03/30/38371.html#Feedback0http://www.tkk7.com/dodoma/comments/commentRss/38371.htmlhttp://www.tkk7.com/dodoma/services/trackbacks/38371.html
Occasionally you'll want to write a class that is just a grouping of static methods and static fields.鏈夋椂鍊?浣犳兂鍐欎竴涓被,鍙槸闇瑕佷粬鎻愪緵浜?jiǎn)涓緋誨垪鐨勫嚱鏁版搷浣滅瓑,鑰屼笉鎯寵瀹冨疄渚嬪寲.濡?java.lang.Math or java.util.Arrays.
浣嗘槸濡傛灉浣犱笉鎻愪緵鏋勯犲嚱鏁?緙栬瘧鍣ㄤ細(xì)鑷姩娣誨姞涓涓?
鎵浠ュ繀欏繪彁渚涗竴涓?姝ゆ椂,鎶婃瀯閫犲嚱鏁拌緗負(fù)private.灝卞彲浠ヨ揪鍒扮洰鐨?
涓鑸敤涓庡伐鍏風(fēng)被.

// Noninstantiable utility class
public class UtilityClass {
// Suppress default constructor for noninstantiability
private UtilityClass() {
// This constructor will never be invoked
}
... // Remainder omitted
}

鐢變簬private鐨勬瀯鍜卞嚱鏁?璇ョ被涓嶈兘琚疄渚嬪寲.鍚屾椂.涓嶈兘琚戶鎵夸簡(jiǎn).

dodoma 2006-03-30 22:22 鍙戣〃璇勮
]]>
effective-java闃呰絎旇2http://www.tkk7.com/dodoma/archive/2006/03/30/38290.htmldodomadodomaThu, 30 Mar 2006 09:24:00 GMThttp://www.tkk7.com/dodoma/archive/2006/03/30/38290.htmlhttp://www.tkk7.com/dodoma/comments/38290.htmlhttp://www.tkk7.com/dodoma/archive/2006/03/30/38290.html#Feedback0http://www.tkk7.com/dodoma/comments/commentRss/38290.htmlhttp://www.tkk7.com/dodoma/services/trackbacks/38290.html
A singleton is simply a class that is instantiated exactly once [Gamma98, p. 127].濡傛暟鎹簱璧勬簮絳?
There are two approaches to implementing singletons. Both are based on keeping the
constructor private and providing a public static member to allow clients access to the sole
instance of the class. In one approach, the public static member is a final field:
// Singleton with final field
public class Elvis {
public static final Elvis INSTANCE = new Elvis();
private Elvis() {
...
}
... // Remainder omitted
}

1:鏋勯犲嚱鏁扮鏈?br />2:鎻愪緵涓涓潤(rùn)鎬乻tatic鐨刦inal鎴愬憳

The private constructor is called only once, to initialize the public static final field
Elvis.INSTANCE.聽(tīng)
Exactly one Elvis instance will exist once the Elvis class is initialized鈥攏o more,
no less. Nothing that a client does can change this.

In a second approach, a public static factory method is provided instead of the public static final field:
// Singleton with static factory
public class Elvis {
private static final Elvis INSTANCE = new Elvis();
private Elvis() {
...
}
public static Elvis getInstance() {
return INSTANCE;
}
... // Remainder omitted
}

鐢ㄩ潤(rùn)鎬佹柟娉曡繑鍥?鑰屼笉鏄洿鎺ヨ繑鍥炰竴涓疄渚?

濡傛灉瀵瑰璞″簭鍒楀寲,瑕佸仛澶氫竴鐐瑰伐浣?濡?br />To make a singleton class serializable (Chapter 10), it is not sufficient merely to add
implements Serializable to its declaration. To maintain the singleton guarantee, you must
also provide a readResolve method (Item 57). Otherwise, each deserialization of a serialized instance will result in the creation of a new instance, 鍚﹀垯,緇忚繃瀵硅薄鍙嶅簭鍒楀寲鍚?浼?xì)瀵艰嚧鍒涘晦Z簡(jiǎn)涓涓柊鐨勫璞?leading, in the case of our example, to spurious Elvis sightings. To prevent this, add the following readResolve method to the Elvis class:
// readResolve method to preserve singleton property
private Object readResolve() throws ObjectStreamException {
/*
* Return the one true Elvis and let the garbage collector
* take care of the Elvis impersonator.
*/
return INSTANCE;
}



dodoma 2006-03-30 17:24 鍙戣〃璇勮
]]>
effective-java闃呰絎旇1http://www.tkk7.com/dodoma/archive/2006/03/30/38282.htmldodomadodomaThu, 30 Mar 2006 09:08:00 GMThttp://www.tkk7.com/dodoma/archive/2006/03/30/38282.htmlhttp://www.tkk7.com/dodoma/comments/38282.htmlhttp://www.tkk7.com/dodoma/archive/2006/03/30/38282.html#Feedback0http://www.tkk7.com/dodoma/comments/commentRss/38282.htmlhttp://www.tkk7.com/dodoma/services/trackbacks/38282.htmlConsider providing static factory methods instead of
constructors 鑰冭檻浠ラ潤(rùn)鎬佸伐鍘傛柟娉曞彇浠f瀯閫犲嚱鏁?br />
The normal way for a class to allow a client to obtain an instance is to provide a public
constructor.

A class can provide a public static factory method, which is simply
a static method that returns an instance of the class

such as
public static Boolean valueOf(boolean b) {
return (b ? Boolean.TRUE : Boolean.FALSE);
}

advantages
1:One advantage of static factory methods is that, unlike constructors, they have names.
涓涓被闈?rùn)鎬佸伐鍘傛柟娉曟槸鏈夊悕瀛楃殑.If the parameters to a constructor do not, in and of themselves, describe the object being returned, a static factory with a well-chosen name can make a class easier to use and the resulting client code easier to read. 濡侭igInteger(int, int,Random)鍙煡閬搉ew浜?jiǎn)涓涓狟igInteger瀵硅薄,鑰孊igInteger.probablePrime()鑳借鏄庤繑鍥炵殑鍙兘鏄竴涓礌鏁板璞?鍙﹀,濡傛灉涓涓瀯閫犲嚱鏁頒粎浠呯敱浜庡弬鏁伴『搴忎笉鍚岃屾剰鎬濅篃涓嶅悓.闈?rùn)鎬佸伐鍘傛柟娉曞氨鏇村姞鏈夌敤浜?瀹冩洿鑳借鏄庡嚱鏁扮殑鎰忔?鍥犱負(fù)瀹冩湁涓涓悕瀛?
the reason聽(tīng)are : If the parameters to a constructor do not, describe the object being
returned, a static factory with a well-chosen name can make a class easier to use and the
resulting client code easier to read.

2:A second advantage of static factory methods is that, unlike constructors, they are not
required to create a new object each time they're invoked.姣忔璇鋒眰鏃?涓嶉渶瑕侀噸鏂板垱寤轟竴涓璞?---鍗曚緥鐨勬剰鎬?榪欏彲浠ュ湪浠諱綍鏃跺埢鎺у埗榪欎釜瀵硅薄,鍚屾椂鍦ㄦ瘮杈冪殑鏃跺欎篃涓嶉渶瑕佺敤equals鑰岀敤= =灝卞彲浠ヨВ鍐充簡(jiǎn).

3:A third advantage of static factory methods is that, unlike constructors, they can return
an object of any subtype of their return type.鍙互榪斿洖涓涓師榪斿洖綾誨瀷鐨勫瓙綾誨瀷瀵硅薄.
One application of this flexibility is that an API can return objects without making their
classes public.Hiding implementation classes in this fashion can lead to a very compact API.榪欑鐏墊椿鎬х殑涓涓簲鐢ㄦ槸錛屼竴涓狝 P I鍙互榪斿洖涓涓璞★紝鍚屾椂鍙堜笉浣胯瀵硅薄鐨勭被鎴愪負(fù)鍏湁鐨勩備互榪欑鏂瑰紡鎶婂叿浣撶殑瀹炵幇綾婚殣钘忚搗鏉ワ紝鍙互寰楀埌涓涓潪甯哥畝媧佺殑A P I銆傝繖欏規(guī)妧鏈潪甯擱傚悎
浜庡熀浜庢帴鍙g殑妗嗘灦緇撴瀯錛屽洜涓哄湪榪欐牱鐨勬鏋剁粨鏋勪腑錛屾帴鍙f垚涓洪潤(rùn)鎬佸伐鍘傛柟娉曠殑鑷劧榪斿洖綾誨瀷
sample:
// Provider framework sketch
public abstract class Foo {
// Maps String key to corresponding Class object
private static Map implementations = null;
// Initializes implementations map the first time it's called
private static synchronized void initMapIfNecessary() {
if (implementations == null) {
implementations = new HashMap();
// Load implementation class names and keys from
// Properties file, translate names into Class
// objects using Class.forName and store mappings.
...
}
}
public static Foo getInstance(String key) {
initMapIfNecessary();
Class c = (Class) implementations.get(key);
if (c == null)
return new DefaultFoo();
try {
return (Foo) c.newInstance();
} catch (Exception e) {
return new DefaultFoo();
}
}
}

鍏湁鐨勯潤(rùn)鎬佸伐鍘傛柟娉曟墍榪斿洖鐨勫璞$殑綾諱笉浠呭彲浠ユ槸闈炲叕鏈夌殑錛岃屼笖璇ョ被鍙互闅忕潃姣忔璋冪敤
鑰屽彂鐢熷彉鍖栵紝榪欏彇鍐充簬闈?rùn)鎬佸伐鍘傛柟娉曠殑鍙傛暟鍊箋傚彧瑕佹槸宸插0鏄庣殑榪斿洖綾誨瀷鐨勫瓙綾誨瀷錛岄兘鏄厑
璁哥殑銆傝屼笖錛屼負(fù)浜?jiǎn)澧炲鉴櫴Y浠剁殑鍙淮鎶ゆэ紝榪斿洖瀵硅薄鐨勭被涔熷彲浠ラ殢鐫涓嶅悓鐨勫彂琛岀増鏈屼笉鍚屻?br />disadvantages:

1:The main disadvantage of static factory methods is that classes without public or protected constructors cannot be subclassed.
綾誨鏋滀笉鍚叕鏈夌殑鎴栬呭彈淇濇姢鐨勬瀯閫犲嚱鏁幫紝灝變笉鑳借瀛愮被鍖栥傚浜庡叕鏈夌殑闈?rùn)鎬佸伐鍘傛墍榪斿洖鐨勯潪鍏湁綾伙紝涔熷悓鏍峰姝?br />
2:A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods.


In summary, static factory methods and public constructors both have their uses, and it pays to understand their relative merits. Avoid the reflex to provide constructors without first considering static factories because static factories are often more appropriate. If you've weighed the two options and nothing pushes you strongly in either direction, it's probably best to provide a constructor simply because it's the norm.






dodoma 2006-03-30 17:08 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 国产性爱在线观看亚洲黄色一级片 | 亚洲黄片手机免费观看| 99久久免费精品国产72精品九九| 久久久无码精品亚洲日韩蜜臀浪潮 | 亚洲伊人久久大香线蕉在观 | 免费夜色污私人影院网站| 国产免费人成视频在线观看| 亚洲乱妇老熟女爽到高潮的片| 免费黄色小视频网站| 亚洲妇女无套内射精| 伊人久久亚洲综合影院| 国产精品高清免费网站| 亚洲日韩小电影在线观看| 日韩精品极品视频在线观看免费| 亚洲第一永久在线观看| 国国内清清草原免费视频99| 亚洲精品人成网线在线播放va| 国产一级淫片视频免费看| h片在线播放免费高清 | 亚洲爆乳精品无码一区二区三区| 永久免费av无码入口国语片| 亚洲欧洲免费视频| 在线免费一区二区| sihu国产精品永久免费| 亚洲av无码国产精品色午夜字幕 | 亚洲国产精品综合久久网各| 午夜一级毛片免费视频| v片免费在线观看| 亚洲精品福利视频| 成人免费视频一区| 国产特黄一级一片免费| 亚洲欧洲国产成人精品| 免费一级毛片在线观看| 三年片在线观看免费大全电影| 亚洲综合伊人制服丝袜美腿| mm1313亚洲精品国产| 100部毛片免费全部播放完整| 亚洲欧美日韩中文高清www777| 久久夜色精品国产亚洲av| 亚洲电影免费观看| 一级成人a免费视频|