Sandwich blt = new Sandwich("BLT", Bread.WHEAT, 3);
blt.addCondiment(Condiment.MAYO);
blt.addMeat(Meat.BACON);
blt.addVegetable(Vegetable.Lettuce);
blt.addVegetable(Vegetable.Tomato);
. . .
public class Sandwich {
// private instance variables
public Sandwich() { }
public Sandwich(String name) {
this();
this.name = name;
}
// telescoping constructors
public Sandwich(String name, Bread bread) {
this(name, bread, bread.minNumSlices);
}
public Sandwich(String name, Bread bread, int numSlices) {
this(name);
this.bread = bread;
this.numBreadSlices = numSlices;
}
public void addCondiment(Condiment meat) { ... }
public void addMeat(Meat meat) { ... }
public void addVegetable(Vegetable veggie) { ... }
}Sandwich blt = new Sandwich.Builder()
.name("BLT")
.bread(Bread.WHEAT)
.numBreadSlices(3)
.withCondiment(Condiment.MAYO)
.withMeat(Meat.BACON)
.withVegetable(Vegetable.Lettuce)
.withVegetable(Vegetable.Tomato)
.build();
. . .
public class Sandwich {
// private instance variables
public Sandwich() { }
public void addCondiment(Condiment meat) { ... }
public void addMeat(Meat meat) { ... }
public void addVegetable(Vegetable veggie) { ... }
public class Builder {
public Sandwich build() {
Sandwich s = new Sandwich();
s.setName(this.name);
...
return s;
}
public SandwichBuilder withBread();
}
}// Quartz scheduler Trigger builder
public T build() {
if(scheduleBuilder == null)
scheduleBuilder = SimpleScheduleBuilder.simpleSchedule();
MutableTrigger trig = scheduleBuilder.build();
trig.setCalendarName(calendarName);
trig.setDescription(description);
trig.setEndTime(endTime);
if(key == null)
key = new TriggerKey(Key.createUniqueName(null), null);
trig.setKey(key);
if(jobKey != null)
trig.setJobKey(jobKey);
trig.setPriority(priority);
trig.setStartTime(startTime);
if(!jobDataMap.isEmpty())
trig.setJobDataMap(jobDataMap);
return (T) trig;
}
// Build then schedule a Job and a Trigger
JobDetail job = newJob(MyJob.class)
.withIdentity("myJob")
.build();
Trigger trigger = newTrigger()
.withIdentity(triggerKey("myTrigger", "myTriggerGroup"))
.withSchedule(simpleSchedule()
.withIntervalInHours(1)
.repeatForever())
.startAt(futureDate(10, MINUTES))
.build();
scheduler.scheduleJob(job, trigger);public double calculate(Operator op, double firstOperand, double secondOperand) {
switch (op) {
case ADD:
return firstOperand + secondOperand;
case SUBTRACT:
return firstOperand - secondOperand;
case MULTIPLY:
return firstOperand * secondOperand;
case DIVIDE:
return firstOperand / secondOperand;
}
}public class AdditionStrategy implements CalculationStrategy {
@Override
public double calculate(double firstOperand, double secondOperand) {
return firstOperand + secondOperand;
}
}
. . .
public double calculate(CalculationStrategy strat, double firstOperand, double secondOperand) {
return strat.calculate(firstOperand, secondOperand);
}DP Examples
By Reid Harrison
DP Examples
- 696