"Effective Java" learning notes

Source: Internet
Author: User
Tags sodium

1th: Consider replacing the constructor with a static factory method

"Not thoroughly read the author's intentions"

2nd: Consider using constructors when multiple constructor parameters are encountered

1. Using overlapping constructors: The disadvantage is that you write too much

2. Use JavaBean mode: disadvantage is inconsistent

3. Use Builder Mode: Create constructors first, too verbose

Builder mode:

1  Public classnutritionfacts{2     Private Final intservingsize;3     Private Final intservings;4     Private Final intcalories;5     Private Final intfat;6     Private Final intsodium;7     Private Final intcarbohydrate;8 9      Public Static classbuilder{Ten         Private Final intservingsize; One         Private Final intservings; A  -         Private intCalories=0; -         Private intFat=0; the         Private intSodium=0; -         Private intCarbohydrate=0; -  -          PublicBuilder (intServingsize,intservings) { +              This. servingsize=servingsize; -              This. servings=servings; +         } A  at          PublicBuilder Calories (intvalue) { -calories=value; -             return  This; -         } -  -          PublicBuilder Fat (intvalue) { infat=value; -             return  This; to         } +  -          PublicBuilder Sodium (intvalue) { thesodium=value; *             return  This; $         }Panax Notoginseng  -          PublicBuilder Carbohydrate (intvalue) { theCarbohydrate=value; +             return  This; A         } the  +          Publicnutritionfacts Build () { -             return NewNutritionfacts ( This); $         } $  -     } -  the     Privatenutritionfacts (Builder builder) { -Servingsize=builder.servingsize;Wuyiservings=builder.servings; thecalories=builder.calories; -fat=Builder.fat; Wusodium=Builder.sodium; -Carbohydrate=builder.carbohydrate; About     } $  -      Public Static voidMain (string[] args) { -Nutritionfacts cocacola=NewNutritionfacts.builder (240,8). Calories (+) sodium (+). Carbohydrate (27). Build (); -System.out.println ("CocaCola nutritionfacts Build success.")); A     } +}

3rd: Hardening the Singleton property with a private constructor or enumeration type

1. A public static member is a final domain: The disadvantage is that privileged clients can invoke the private constructor through the reflection mechanism through the Accessibleobject.setaccessible method

1  Public class elvis{2      Public Static Final Elvis instance=New  Elvis (); 3     Private Elvis () {} 4 }

2. A public member is a static factory method:

1  Public classelvis{2     Private Static FinalElvis instance=NewElvis ();3     PrivateElvis () {}4      Public StaticElvis getinstance () {5         returnINSTANCE;6     }7}

At the same time, using the static factory method to achieve the singleton class becomes serializable, just adding implements serializable to the declaration is not enough. In order to maintain and guarantee the singleton, it is necessary to declare that all instances are instantaneous and provide a readresolve method. Otherwise, a new instance will be created each time a serialized instance is deserialized. "It's not clear why the addition of the Readresolve method will prevent"

1  Public classelvis{2     Private Static FinalElvis instance=NewElvis ();3     PrivateElvis () {}4 5      Public StaticElvis getinstance () {6         returnINSTANCE;7     }8 9     PrivateObject readresolve () {Ten         returnINSTANCE; One     } A}

3. Write an enumeration type that contains a single element:

1  Public enum elvis{2    INSTANCE; 3 4      Public void leavethebuilding () {} 5 }

The single-element enumeration type has become the best way to implement Singleton.

4th: The ability to harden non-instancing through a private constructor:

1  Public class utilityclass{2     Private Utilityclass () {3         Throw New assertionerror (); 4     }5 }

5th: Avoid creating unnecessary objects 1. Static factory methods are typically used instead of constructors to avoid creating unnecessary objects.

1 classperson{2     Private FinalDate birthDate;3     Private Static FinalDate Boom_start;4     Private Static FinalDate boom_end;5 6     Static{7Calendar gmtcal = calendar.getinstance (Timezone.gettimezone ("GMT"));8Gmtcal.set (1946,calendar.january,1,0,0,0);9boom_start=gmtcal.gettime ();TenGmtcal.set (1965,calendar.januray,1,0,0,0); OneBoom_end=gmtcal.gettime (); A     } -  -      Public BooleanIsbabyboomer () { the         returnBirthdate.compareto (Boom_start) >=0&&bithdate.compareto (boom_end) <0; -     } -}

2.

"Effective Java" learning notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.