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

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");
主站蜘蛛池模板: 淮安市| 河源市| 甘南县| 宝应县| 北京市| 三原县| 浦北县| 基隆市| 平果县| 前郭尔| 江安县| 精河县| 永修县| 千阳县| 德清县| 堆龙德庆县| 浦城县| 祁东县| 石泉县| 贞丰县| 新竹县| 武清区| 都昌县| 永州市| 宝清县| 秭归县| 合江县| 彩票| 广平县| 宿松县| 阿克陶县| 交城县| 金坛市| 富顺县| 翼城县| 波密县| 灯塔市| 萝北县| 永嘉县| 镇雄县| 上林县|