Skip to content

fro0m/Abstract-Factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Abstract Factory Pattern in C++

This project is a simple C++ application that demonstrates the Abstract Factory design pattern.

Abstract Factory 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) and Square (straight).
  • Robust Shapes: Ellipse (curved) and Rectangle (straight).

Code Structure

  • 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.

Build and Run

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.

Using SimpleShapeFactory

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_app

To run the application:

./simple_factory_app

Expected Output:

circle 0: draw
square 1: draw
circle 2: draw

Using RobustShapeFactory

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_app

To run the application:

./robust_factory_app

Expected Output:

ellipse 0: draw
rectangle 1: draw
ellipse 2: draw

Using the .pro file (with qmake)

The provided Abstract-Factory.pro file is configured to use the RobustShapeFactory. You can use qmake and make to build it.

qmake
make

This will create an executable named Abstract-Factory (or similar, depending on your system).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •