移动开发之设计模式-建造者模式(IOS&Android)

资源

完全参照 建造者模式|菜鸟教程 ,但不包括IOS代码

建造者模式

建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
一个 Builder 类会一步一步构造最终的对象。该 Builder 类是独立于其他对象的。

介绍

意图: 将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。
主要解决: 主要解决在软件系统中,有时候面临着"一个复杂对象"的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法却相对稳定。
何时使用: 一些基本部件不会变,而其组合经常变化的时候。
如何解决: 将变与不变分离开。
关键代码: 建造者:创建和提供实例,导演:管理建造出来的实例的依赖关系。
应用实例:
1、去肯德基,汉堡、可乐、薯条、炸鸡翅等是不变的,而其组合是经常变化的,生成出所谓的"套餐"。
2、JAVA 中的 StringBuilder。

优点:
1、建造者独立,易扩展。
2、便于控制细节风险。

缺点:
1、产品必须有共同点,范围有限制。
2、如内部变化复杂,会有很多的建造类。

使用场景:
1、需要生成的对象具有复杂的内部结构。
2、需要生成的对象内部属性本身相互依赖。

注意事项: 与工厂模式的区别是:建造者模式更加关注与零件装配的顺序。

在这里插入图片描述

Android

Item.java

public interface Item {
   public String name();
   public Packing packing();
   public float price();    
}

Packing.java

public interface Packing {
   public String pack();
}

Wrapper.java

public class Wrapper implements Packing {
 
   @Override
   public String pack() {
      return "Wrapper";
   }
}

Bottle.java

public class Bottle implements Packing {
 
   @Override
   public String pack() {
      return "Bottle";
   }
}

Burger.java

public abstract class Burger implements Item {
 
   @Override
   public Packing packing() {
      return new Wrapper();
   }
 
   @Override
   public abstract float price();
}

ColdDrink.java

public abstract class ColdDrink implements Item {
 
    @Override
    public Packing packing() {
       return new Bottle();
    }
 
    @Override
    public abstract float price();
}

VegBurger.java

public class VegBurger extends Burger {
 
   @Override
   public float price() {
      return 25.0f;
   }
 
   @Override
   public String name() {
      return "Veg Burger";
   }
}

ChickenBurger.java

public class ChickenBurger extends Burger {
 
   @Override
   public float price() {
      return 50.5f;
   }
 
   @Override
   public String name() {
      return "Chicken Burger";
   }
}

Coke.java

public class Coke extends ColdDrink {
 
   @Override
   public float price() {
      return 30.0f;
   }
 
   @Override
   public String name() {
      return "Coke";
   }
}

Pepsi.java

public class Pepsi extends ColdDrink {
 
   @Override
   public float price() {
      return 35.0f;
   }
 
   @Override
   public String name() {
      return "Pepsi";
   }
}

Meal.java

import java.util.ArrayList;
import java.util.List;
 
public class Meal {
   private List<Item> items = new ArrayList<Item>();    
 
   public void addItem(Item item){
      items.add(item);
   }
 
   public float getCost(){
      float cost = 0.0f;
      for (Item item : items) {
         cost += item.price();
      }        
      return cost;
   }
 
   public void showItems(){
      for (Item item : items) {
         System.out.print("Item : "+item.name());
         System.out.print(", Packing : "+item.packing().pack());
         System.out.println(", Price : "+item.price());
      }        
   }    
}

MealBuilder.java

public class MealBuilder {
 
   public Meal prepareVegMeal (){
      Meal meal = new Meal();
      meal.addItem(new VegBurger());
      meal.addItem(new Coke());
      return meal;
   }   
 
   public Meal prepareNonVegMeal (){
      Meal meal = new Meal();
      meal.addItem(new ChickenBurger());
      meal.addItem(new Pepsi());
      return meal;
   }
}

BuilderPatternDemo.java

public class BuilderPatternDemo {
   public static void main(String[] args) {
      MealBuilder mealBuilder = new MealBuilder();
 
      Meal vegMeal = mealBuilder.prepareVegMeal();
      System.out.println("Veg Meal");
      vegMeal.showItems();
      System.out.println("Total Cost: " +vegMeal.getCost());
 
      Meal nonVegMeal = mealBuilder.prepareNonVegMeal();
      System.out.println("\n\nNon-Veg Meal");
      nonVegMeal.showItems();
      System.out.println("Total Cost: " +nonVegMeal.getCost());
   }
}

结果

Veg Meal
Item : Veg Burger, Packing : Wrapper, Price : 25.0
Item : Coke, Packing : Bottle, Price : 30.0
Total Cost: 55.0


Non-Veg Meal
Item : Chicken Burger, Packing : Wrapper, Price : 50.5
Item : Pepsi, Packing : Bottle, Price : 35.0
Total Cost: 85.5

IOS

Packing.h

#import <Foundation/Foundation.h>

@protocol Packing <NSObject>
-(NSString*)pack;
@end

@interface Wrapper : NSObject <Packing>
@end

@interface Bottle : NSObject <Packing>
@end



Packing.m

#import "Packing.h"

@implementation Wrapper
- (NSString*)pack {
    return @"Wrapper";
}

@end

@implementation Bottle

- (NSString*)pack {
    return @"Bottle";
}

@end

Item.h

#import <Foundation/Foundation.h>
#import "Packing.h"

@protocol Item <NSObject>
-(NSString*)name;
-(id<Packing>) packing;
-(double)price;
@end

#pragma  ===热食物===
@interface Burger : NSObject <Item>
@end

@interface VegBurger : Burger
@end

@interface ChickBurger : Burger
@end

#pragma ===冷饮===
@interface ColdDrink : NSObject <Item>
@end

@interface Coke : ColdDrink
@end

@interface Pepsi : ColdDrink
@end

Item.m

#import "Item.h"

#pragma ===热食物===
@implementation Burger
- (NSString *)name{
    return @"Burger";
}

- (id<Packing>)packing {
    return Wrapper.new;
}

- (double)price {
    return 0;
}

@end

@implementation VegBurger
- (NSString *)name{
    return @"Veg Burger";
}

- (id<Packing>)packing{
    return [super packing];
}

- (double)price{
    return 25.0f;
}
@end

@implementation ChickBurger
- (NSString *)name{
    return @"Chicken Burger";
}

- (id<Packing>)packing{
    return [super packing];
}

- (double)price{
    return 50.5f;
}
@end


#pragma ===冷饮===
@implementation ColdDrink
- (NSString *)name{
    return @"ColdDrink";
}

- (id<Packing>)packing{
    return Bottle.new;
}

- (double)price{
    return 0;
}
@end

@implementation Coke
- (NSString *)name{
    return @"Coke";
}

- (id<Packing>)packing{
    return [super packing];
}

- (double)price{
    return 30.0f;
}
@end

@implementation Pepsi
- (NSString *)name{
    return @"Pepsi";
}

- (id<Packing>)packing{
    return [super packing];
}

- (double)price{
    return 35.0f;
}
@end

Meal.h

#import <Foundation/Foundation.h>
#import "Item.h"

@interface Meal : NSObject
-(void)addItem:(id<Item>)item;
-(float)getCost;
-(void)showItems;
@end

Meal.m

#import "Meal.h"
@interface Meal()
@property (nonatomic, strong) NSMutableArray<Item> *items;
@end

@implementation Meal

- (NSArray *)items{
    if(_items == nil) {
        _items = [[NSMutableArray<Item> alloc]init];
    }
    return _items;
}

- (void)addItem:(id<Item>)item{
    [self.items addObject:item];
}

- (float)getCost{
    float cost = 0.0f;
    for(id<Item> item in self.items) {
        cost += [item price];
    }
//    for (id<Item> *item in self.items) {
//        cost += [item price];
//    }
    return cost;
}

- (void)showItems{
    for (id<Item> item in self.items) {
        NSLog(@"%@, %@, %f",[item name], [[item packing]pack], [item price]);
    }
}

@end

MealBuilder.h

#import <Foundation/Foundation.h>
#import "Meal.h"

@interface MealBuilder : NSObject
-(Meal*)prepareVegMeal;
-(Meal*)prepareNonVegMeal;
@end

MealBuilder.m

#import "MealBuilder.h"

@implementation MealBuilder
- (Meal *)prepareVegMeal{
    Meal *meal = Meal.new;
    [meal addItem:VegBurger.new];
    [meal addItem:Coke.new];
    return meal;
}
- (Meal *)prepareNonVegMeal{
    Meal *meal = Meal.new;
    [meal addItem:ChickBurger.new];
    [meal addItem:Pepsi.new];
    return meal;
}
@end

ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    MealBuilder *mealBuilder = MealBuilder.new;
    
    Meal *vegMeal = [mealBuilder prepareNonVegMeal];
    NSLog(@"----Veg Meal----");
    [vegMeal showItems];
    NSLog(@"Total Cost: %f" , [vegMeal getCost]);
    
    Meal *nonVegMeal = [mealBuilder prepareNonVegMeal];
    NSLog(@"----Non-Veg Meal----");
    [nonVegMeal showItems];
    NSLog(@"Total Cost:%f", [nonVegMeal getCost]);
}

结果

 ----Veg Meal----
 Chicken Burger, Wrapper, 50.500000
 Pepsi, Bottle, 35.000000
 Total Cost: 85.500000
 ----Non-Veg Meal----
 Chicken Burger, Wrapper, 50.500000
 Pepsi, Bottle, 35.000000
 Total Cost:85.500000

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试