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

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");
主站蜘蛛池模板: 砀山县| 华阴市| 富川| 醴陵市| 天台县| 江门市| 浙江省| 邢台县| 海原县| 辉南县| 龙山县| 永胜县| 永福县| 太保市| 临汾市| 东阳市| 玛多县| 广灵县| 鹤壁市| 宜良县| 湖北省| 灯塔市| 广元市| 湛江市| 东乡县| 会昌县| 巫溪县| 东城区| 蒙山县| 杭州市| 若尔盖县| 兰坪| 离岛区| 苏州市| 环江| 乌拉特前旗| 高雄市| 宿州市| 读书| 赣州市| 乌兰浩特市|