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

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");
主站蜘蛛池模板: 沛县| 佛学| 洪洞县| 东光县| 赤峰市| 九江县| 南昌县| 奉贤区| 赤水市| 石渠县| 凤凰县| 鄢陵县| 凤城市| 多伦县| 榆树市| 永德县| 青海省| 平利县| 镇平县| 云林县| 临安市| 香格里拉县| 宜都市| 曲阜市| 历史| 巴东县| 沙田区| 郴州市| 台东县| 永丰县| 昌宁县| 胶州市| 津市市| 上林县| 嘉峪关市| 敖汉旗| 南皮县| 霍州市| 山东| 务川| 绥中县|