AlexLecture_AbstractFactoryPattern

AlexLecture_AbstractFactoryPattern - Abstract Factory...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Abstract Factory Pattern 2 Car making ± You get an algorithm for making a car: Step 1. get a body, Step 2. get engine, Step 3. get 4 wheels, Step 4. connect them together. A car is done! ± Toyota makes Toyota body, Toyota engine, Toyota wheels At the end, you make a Toyota car. ± Ford makes Ford body, Ford engine, Ford wheels At the end, you make a Ford car. ± But your algorithm is the same, no matter using Toyota parts or Ford parts. ± How to code your genius car making algorithm only once, but can be used to make cars of any brands? You may get another auto part factory called Honda, then you can make Honda cars.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 3 Naïve Solution ± Use switch Switch ( car brand ) { case TOYOTA : // use TOYOTA parts case FORD: // use FORD parts } ± Is this a good desing? ± Switches often indicate a need for abstraction 4 Sample Code –Factory class AbstractAutopartsFactory{ public: virtual Body* makeBody() = 0; virtual Engine* makeEngine() = 0; virtual Wheels* makeWheels() = 0; virtual Abs* makeAbs() = 0; protected: AbstractAutopartsFactory();
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

AlexLecture_AbstractFactoryPattern - Abstract Factory...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online