神刀安全网

独身模式+构建者模式打造私有个性化“商城”

需求是这样的
我需要打造一个商城,且商城是注册过的读一无二的,就像是万达或者沃尔玛。商城每天都提供食物和衣服,但是为了提供个性化的促销服务,商城每天提供不同的廉价商品搭配。
比如:
day1:(提供)调料品
day2:(提供)床上用品+婴儿用品+家具
day3:(提供)厨具+洗护用品

好了,需求就是这样。不废话,直接上代码。

/**  * Created by zhangxiang on 16-9-22.  */ public class Mall{     private  int food;     private  int clothes;     private  int seasoning;     private  int bedding;     private  int babyProducts;     private  int furniture;     private  int cook;     private  int washProducts;      private static Mall sInstance = null;      public static synchronized Mall getInstance(Builder builder) {         if (sInstance == null) {             sInstance = new Mall(builder);         }else {             //这里注意else重新初始化了Mall的内容,但是保持了Mall为单例             sInstance.setMall(builder);         }         return sInstance;     }      public static class Builder {         private final int food;         private final int clothes;          private int seasoning = 0;         private int bedding = 0;         private int babyProducts = 0;         private int furniture = 0;         private int cook = 0;         private int washProducts = 0;         //初始化必备商品         public Builder(int food,int clothes){             this.food = food;             this.clothes = clothes;         }          public Builder seasoning(int seasoning){             this.seasoning = seasoning;             return this;         }          public Builder bedding(int bedding){             this.bedding = bedding;             return this;         }          public Builder babyProducts(int babyProducts){             this.babyProducts = babyProducts;             return this;         }          public Builder furniture(int furniture){             this.furniture = furniture;             return this;         }          public Builder cook(int cook){             this.cook = cook;             return this;         }          public Builder washProducts(int washProducts){             this.washProducts = washProducts;             return this;         }         //返回唯一商城         public Mall build(){             return Mall.getInstance(this);         }     }      private Mall(Builder builder){         food = builder.food;         clothes = builder.clothes;         seasoning = builder.seasoning;         bedding = builder.bedding;         babyProducts = builder.babyProducts;         furniture = builder.furniture;         cook = builder.cook;         washProducts = builder.washProducts;     }      private void setMall(Builder builder){         food = builder.food;         clothes = builder.clothes;         seasoning = builder.seasoning;         bedding = builder.bedding;         babyProducts = builder.babyProducts;         furniture = builder.furniture;         cook = builder.cook;         washProducts = builder.washProducts;     }      public void printProducts(int day)     {         System.out.print(this.toString()+"---------"+day+"-----------//n");         System.out.println("food num is:" +this.food+"//n"+                 "clothes num is:"+this.clothes+"//n"+                 "seasoning num is:"+this.seasoning+"//n"+                 "bedding num is:"+this.bedding+"//n"+                 "babyProducts num is:"+this.babyProducts+"//n"+                 "furniture num is:"+this.furniture+"//n"+                 "cook num is:"+this.cook+"//n"+                 "washProducts num is:"+this.washProducts+"//n");     }      public static void main(String[] args) {         /** 第一天:(食物+衣物)+调料品 **/         Mall dayOne = new Mall.Builder(240,80).seasoning(100).build();         dayOne.printProducts(1);         /** 第二天:(食物+衣物)+床上用品+婴儿用品+家具 **/         Mall dayTwo = new Mall.Builder(300,100).bedding(20).babyProducts(30).furniture(50).build();         dayTwo.printProducts(2);         /** 第三天:(食物+衣物)+厨具+洗护用品 **/         Mall dayThree = new Builder(400,20).cook(300).washProducts(50).build();         dayThree.printProducts(3);     } }

打印结果:
Mall@3ba76e19可以看出都是同一个实例

Mall@3ba76e19———1———–
food num is:240
clothes num is:80
seasoning num is:100
bedding num is:0
babyProducts num is:0
furniture num is:0
cook num is:0
washProducts num is:0

Mall@3ba76e19———2———–
food num is:300
clothes num is:100
seasoning num is:0
bedding num is:20
babyProducts num is:30
furniture num is:50
cook num is:0
washProducts num is:0

Mall@3ba76e19———3———–
food num is:400
clothes num is:20
seasoning num is:0
bedding num is:0
babyProducts num is:0
furniture num is:0
cook num is:300
washProducts num is:50

很好,虽然你不是王思聪,但是你和他爸一样有一个个性化商城了。

独身模式+构建者模式打造私有个性化“商城”
先来个小目标!

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » 独身模式+构建者模式打造私有个性化“商城”

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址