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
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
11 years 45 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
11 years 45 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
11 years 45 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
11 years 45 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
11 years 45 weeks ago
Poll
Useful resources
Android Development Projects
- iOS/Android Developer to take older Games and bring them Current
- Android apps developer - need to finish urgent.
- Buliding MobileApp For onlie order
- looking for android APP developers
- Create an ecommerce app
- text-to voice for smartphones IOS - GOOGLE - HARMONY - AND ALEXA
- Optimize Images on App
- Create small feature with drag-drop text for Android
- Scouting for advanced website and Mobile apps developers. Potential Long-term contract.
- BLACK SCREEN