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

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");
主站蜘蛛池模板: 惠来县| 什邡市| 临澧县| 博乐市| 巴里| 三原县| 巩留县| 莒南县| 普兰店市| 都兰县| 伊川县| 沙河市| 自贡市| 遂川县| 唐海县| 九寨沟县| 阜新| 恭城| 方城县| 永和县| 涡阳县| 黄浦区| 荆州市| 盈江县| 沛县| 区。| 天水市| 阳城县| 滨海县| 海丰县| 萨迦县| 霍邱县| 昭苏县| 河池市| 呼图壁县| 攀枝花市| 松潘县| 夹江县| 乐业县| 上饶市| 高雄市|