Salesforce - Developer - Design Patterns

salesforce-developer

public class Geocoder {
  public class NameException extends Exception{};
  public static final Map<String,GeocodeService> strategies;
  static {
    GlobalVariable__c gv = GlobalVariable__c.getInstance('strategies');
    List<String> strategyName = new List<String>();
    if (gv != null && gv.value__c != null) {
      strategyNames = gv.value__c.split(',');
    }
    strategies = new Map<String, GeocodeService>();
    for (String name: strategyNames) {
      try {
        strategies.put(name,(GeocodeService)Type.forName(name + 'impl').
          newInstance());
      } catch (Exception e) { 
        continue;
      }
    }
  }
  private GeocodeService strategy
  public Geocoder(String name) {
    if (! strategies.containsKey(name)) {
      throw new NameException(name);
    }
    strategy = strategy.get(name);
  }
  public List<Double> getLatLong(String address) {
    return strategy.getLatLong(address);
  }
}

public class DecorateWeather {
  public Weather__c weather { get; private set; }

  public DecorateWeather (Weather__c weather) {
    this.weather = weather;
  }

  public Decimal temInCelcius {
    get {
    }
    set {
    }
  }
}

trigger OpptyTrigger on Opportunity (after insert, after update) {
  if (trigger.isAfter && (trigger.isInsert || trigger.isUpdate)) {
    List<Opportunity> closedOpptyList = new List<Opportunity>();
    for (Opportunity oppty : trigger.new) {
      if (oppty.isClosed && (trigger.isInsert ||
        (trigger.isUpdate && !trigger.oldMap.get(oppty.id).isClosed))) {
        closedOpptyList.add(oppty);
      }
    }
    if (! closedOpptyList.isEmpty()) {
      new OrderClass().CreateOrderFromOpptys(closedOpptyList);
    }
  }
}

public class OrderClass {
  public void CreateOrdersFromOpptys(List<Opportunity> opptyList) {
    List<Order__c> orderList = new List<Order__c>();
    for (Opportunity oppty : opptyList) {
      Order__c order = new Order__c();
      ...
      orderList.add(order);
    }
    insert orderList;
  }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License