开发者

Factory Pattern - when do you say you need a specialised factory

I am having a little design crisis, I am making a Plane class, which is made of

an engine

2 wings

and N seats

The engine takes in 开发者_开发知识库a engine size, and the wings have a span.

would this still be feasible to have a PlaneFactory, when the factory may have to take in multiple parameters to setup the plane (wings, engine, no of seats)

thanks in advance

bones


if your plane object is turning into a complex one you could use builder design pattern to create the object part by part

you can easily apply the famous pizza example found in wikipedia on your problem , the builder class will have methods like build wings , build engine .... and finally getplane(). you can have different builder classes that implement the abstract builder class , each builder creates a different plane (using different components)


It depends really on what you exactly need, you could use more design patterns. Factory method is a good idea for a complex creation, that is actually the point of the pattern. If you would like to construct the plane gradualy, especially when you would need to work with parts of plane even before the whole plane was finished (e.g. you don't need wings to sell tickets for seats:))...) than the builder pattern is really what you need. It may also happed, that you create many planes and they all have many wings and seats, which are the same (e.g. you have three standard types of engines), than you can use the Flyweight pattern, so every plane with same type of wings will use exactly the same wings (totally unrealistic:)...). It may also happen, that creating a control panel for the plane would take much work, which can be done beforehand and than just copied - that is the Prototype pattern. I think those are the most likely you might need, for a more concrete anser, you should give more information about what you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜