This project is a simple C++ application that demonstrates the Abstract Factory design pattern.
The Abstract Factory pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes.
This project implements two families of shapes:
- Simple Shapes:
Circle(curved) andSquare(straight). - Robust Shapes:
Ellipse(curved) andRectangle(straight).
main.cpp: Contains the entire implementation.Shape: An abstract base class for all shapes.Circle,Square,Ellipse,Rectangle: Concrete shape implementations.Factory: An abstract factory interface for creating shapes.SimpleShapeFactory: A concrete factory for creating simple shapes.RobustShapeFactory: A concrete factory for creating robust shapes.
The main function uses a factory to create a set of shapes and then calls their draw() method.
You can build the project using a C++ compiler like g++. The behavior of the program is controlled by a preprocessor macro (SIMPLE or ROBUST) passed during compilation.
To compile the program to use SimpleShapeFactory (creates Circle and Square), use the -DSIMPLE flag:
g++ -std=c++11 -DSIMPLE main.cpp -o simple_factory_appTo run the application:
./simple_factory_appExpected Output:
circle 0: draw
square 1: draw
circle 2: draw
To compile the program to use RobustShapeFactory (creates Ellipse and Rectangle), use the -DROBUST flag:
g++ -std=c++11 -DROBUST main.cpp -o robust_factory_appTo run the application:
./robust_factory_appExpected Output:
ellipse 0: draw
rectangle 1: draw
ellipse 2: draw
The provided Abstract-Factory.pro file is configured to use the RobustShapeFactory. You can use qmake and make to build it.
qmake
makeThis will create an executable named Abstract-Factory (or similar, depending on your system).