Mojito builder
Last week, in spite of the winter the weather was very mild. The beams of the winter sun brought forward the memories of the summer: hot weather, girls in bikinis, mojitos! We have already gotten a promise for the first two in a certain form here in Helloandroid HQ (the heating system will be repaired, and we 'll have some women colleagues), but it is our task to make coctails.

- public class Mojito {
- private final int mWhiteRum; //cl
- private final int mBrownSugar; //spoun
- private final int mLime; //clove
- private final int mMenta; //piece
- private final int mSoda; //decilitre
- public Mojito(int whiteRum, int brownSugar, int lime, int menta, int soda){
- mWhiteRum = whiteRum;
- mBrownSugar = brownSugar;
- mLime = lime;
- mMenta = menta;
- mSoda = soda;
- }
- }
This is a quite correct code. But I often use 3 cloves of lime and 5 mint leaves, and the proportion of rum and soda strongly depends on who it is made for. We should have a special constructor to this case:
- public class Mojito {
- private final int mWhiteRum; //cl
- private final int mBrownSugar; //spoun
- private final int mLime; //clove
- private final int mMenta; //piece
- private final int mSoda; //decilitre
- public Mojito(int whiteRum, int brownSugar, int lime, int menta, int soda){
- mWhiteRum = whiteRum;
- mBrownSugar = brownSugar;
- mLime = lime;
- mMenta = menta;
- mSoda = soda;
- }
- public Mojito(int whiteRum, int brownSugar, int soda){
- this(whiteRum, brownSugar, 3, 5, soda);
- }
- }
The problems start, as I make children mojito with 5 mint leaves. We should have a third constructor too:
- public Mojito(int brownSugar, int lime, int soda){
- this(0, brownSugar, lime, 5, soda);
- }
However these parameters are equal with the previous constructor's parameters, only one of them can be imlemented. Even if I manage to live together with this problem, a larger problem is that I'm oblivious, and now I already don't know the order of the parameters.
- // Which component dropped out?
- Mojito m = new Mojito(5, 3, 0, 6, 2);
Fortunately, there is a possibility to solve both problems, we can make a constructive object, instead of the direct formation of the object.
- public class Mojito {
- private final int mWhiteRum; // cl
- private final int mBrownSugar; // spoun
- private final int mLime; // clove
- private final int mMenta; // piece
- private final int mSoda; // decilitre
- private Mojito(Builder builder) {
- mWhiteRum = builder.mWhiteRum;
- mBrownSugar = builder.mBrownSugar;
- mLime = builder.mLime;
- mMenta = builder.mMenta;
- mSoda = builder.mSoda;
- }
- public static class Builder {
- private int mWhiteRum; // cl
- private int mBrownSugar; // spoun
- private int mLime; // clove
- private int mMenta; // piece
- private int mSoda; // decilitre
- public Builder() {
- }
- public Builder setWhiteRum(int whiteRum) {
- mWhiteRum = whiteRum; return this;
- }
- public Builder setBrownSugar(int brownSugar) {
- mBrownSugar = brownSugar; return this;
- }
- public Builder setLime(int lime) {
- mLime = lime; return this;
- }
- public Builder setMenta(int menta) {
- mMenta = menta; return this;
- }
- public Builder setSoda(int soda) {
- mSoda = soda; return this;
- }
- public Mojito build() {
- return new Mojito(this);
- }
- }
- }
Thus the creation of the code we get a bit wordy, but then also easy to understand.
- Mojito m = new Mojito.Builder().setWhiteRum(5)
- .setBrownSugar(3)
- .setLime(3)
- .setMenta(5)
- .setSoda(2).build();
The number of the setters can be reduced if we move some (required) parameters to the constructor of the Builder class, or we add default initial values to the members of the Builder.
- public static class Builder {
- private int mWhiteRum = 5; // cl
- private int mBrownSugar = 3; // spoun
- private int mLime = 3; // clove
- private int mMenta = 5; // piece
- private int mSoda = 2; // decilitre
- public Builder(){
- this(5);
- }
- public Builder(int whiteRum) {
- mWhiteRum = whiteRum;
- }
This way the completion of the original mojito coctial is sufficiently short, and the preparation of the versions will be easy to understand.
- Mojito offical = new Mojito.Builder().build();
- // Mix mojito for children
- Mojito childMojito = new Mojito.Builder().setWhiteRum(0).build();
- // Or ...
- Mojito childMojito2 = new Mojito.Builder(0).build();
We can prepare the build() method to pay attention to that the ready-made coctail should be similar to a mojito in any case.
- public Mojito build() {
- if(0 == mLime && 0 == mMenta){
- }
- return new Mojito(this);
- }
Important property that the coctail what we got, will be immutable. Nobody should change the proportion after the completion.
Optimize Mojito code for Android! „Designing for Performance” does not recommend the use of getter/setter methods. We set the class variables first (and last) in the constructor, we can't do that previously, as there is no copy of it yet. This way we can achieve 3-7X increase in the performance.
- public class Mojito {
- public final int whiteRum; // cl
- public final int brownSugar; // spoun
- public final int lime; // clove
- public final int menta; // piece
- public final int soda; // decilitre
- private Mojito(Builder builder) {
- whiteRum = builder.mWhiteRum;
- brownSugar = builder.mBrownSugar;
- lime = builder.mLime;
- menta = builder.mMenta;
- soda = builder.mSoda;
- }
We may treat the Mojito Builder class similary too, if we sacrifice its easy use, because that way we cannot call the setter methods onto each other.
We can only keep the immutability of classes with the help of getter.
Even if a final modifier is given, the date remains changeable:
- // Cheat!!!
In this case, the getter is the only solution:
- public class Mojito {
- ...
- public final int soda; // decilitre
- private Mojito(Builder builder) {
- ...
- }
- temp.setTimeInMillis(created.getTimeInMillis());
- return temp;
- }
This extremely useful Mojito class can be used freely without any modification!
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@lorealmichelle (Loreal Witherspoon ♔)I've just received an achievement: Tax Collector http://t.co/vektXV7M #Android #Androidgames #Gameinsight
4 min 35 sec ago -
@mawangbird (정진현)Start playing Paradise Island on Android http://t.co/QhMy4qEk #Android #Androidgames #Gameinsight http://t.co/FxWuH7D6
4 min 37 sec ago -
@babybluesnshn11 (Dawn wolf)I've just received an achievement: Parks Developer http://t.co/poJCRd2M #Android #Androidgames
4 min 45 sec ago -
@lyazi256 (lyazi)
The "Farm" collection in Big Business has been completed! 71 http://t.co/qPVxRX47 #android #gameinsight #androidgames
4 min 48 sec ago -
@adrianthompkins (Adrian Thompkins)#Android Stuff: S-Voice gets gagged: Samsung blocks leaked APKs http://t.co/TiWjmmux
4 min 48 sec ago
Poll
Useful resources
Android Development Projects
- Private project for Petterpp1 [WRD] by geomaster909
- Modifications on an Android Game by tymex
- app dev for techcity by dany2g
- Android App by dvlinh
- Iphone app by nabzyd
- Betfair Mobile App by Daaniel
- Need App Developer/Designer for iPhone, iPad by lplco
- Android Maps - Saving GeoLocations to MySql database. by adelshehri
- Prototype Android Apps display Information from database by getitonline
- Metal Weigh Calculator by mpmakwana



Comments
Great ArticlE >> ThX AloT
Great ArticlE >> ThX AloT ..
I ReallY EnjoyeD ReadinG it
It is very useful .. and u have great style
I hope u can visit my site below
And .. I,m Waiting for more
شات مصرى ,
شات مصرى ,
شات مصرى ,
شات مصرية ,
شات مصريه ,
دردشة مصرية ,
دردشة بنت مصرية ,
شات بنت مصرية ,
شات بنت مصر ,
دردشة بنت مصر ,
شات ,
دردشة بنات ,
شات اسكندرية ,
شات القاهرة ,
شات المنصورة ,
شات طنطا ,
شات السويس ,
شات الشرقية ,
شات المنوفية ,
شات الفيوم ,
شات بنى سويف ,
شات سوهاج ,
شات الصعيد ,
شات الزقازيق ,
شات جامعة القاهرة ,
شات جامعة اسكندرية ,
شات بنت السعودية ,
شات بنت البحرين ,
شات بنت الكويت ,
شات بنت فلسطين ,
شات بنت المغرب ,
شات بنت الامارات ,
شات بنت اليمن ,
شات بنت العراق ,
شات مصراوى ,
شات بنات ,
روتانا سينما ,
روتانا سينما مشاهدة مباشرة ,
روتانا سينما بث مباشر ,
روتانا سينما اونلاين ,
I hope u can find something useful About it .. Enjoy
Thanks .. lol i had to read it twice
Cocktail Programming
Thankyou for this interesting programming tutorial. It makes it much easier to concentrate when the finished object is an exotic cocktail - Malpensa