官术网_书友最值得收藏!

Anonymous concrete factory

We continue the previous code by adding a BikeFactory from where customers can select a small bike or a large bike. We can do this without creating a separate class file; we can simply create an anonymous class that extends the VehicleFactory directly in the client code:

VehicleFactory bikeFactory = new VehicleFactory() 
{
@Override
protected Vehicle createVehicle(String size)
{
if (size.equals("small"))
return new MountainBike();
else if (size.equals("large"))
return new CityBike();
return null;
}
};
bikeFactory.orderVehicle("large", "blue");
主站蜘蛛池模板: 于都县| 玛多县| 周口市| 钟山县| 吉林市| 西乡县| 富阳市| 丁青县| 瑞金市| 清苑县| 开化县| 泗阳县| 泸西县| 虞城县| 秦安县| 渑池县| 镇康县| 常山县| 淄博市| 榕江县| 霸州市| 花垣县| 四会市| 江北区| 阿克陶县| 南投县| 威海市| 定州市| 阜新| 彰化县| 崇州市| 海阳市| 新建县| 石城县| 大名县| 武安市| 镇原县| 南华县| 襄樊市| 太谷县| 龙泉市|