Skip to content
-
Abstract Factory Pattern | C++ Design Patterns
Last Updated :
30 Oct, 2023
Abstract Factory Pattern is a creational design pattern used in object-oriented programming. It provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern is a way to encapsulate the creation of objects and ensure that they are compatible and properly configured. In this article, we'll look at the Abstract Factory Pattern in detail with an example
Important Topics for the Abstract Factory Pattern in C++ Design Patterns
UML Diagram for the Abstract Factory Pattern in C++ Design Patterns
UML Diagram for Abstract Factory Pattern
- Abstract Factory Interface: This interface defines a set of methods for creating various abstract product types. Each method in the interface corresponds to a different product family.
- Concrete Factories: Concrete factory classes implement the Abstract Factory interface. Each concrete factory is responsible for creating a specific family of related products.
- Abstract Product Interfaces: These interfaces define the structure and behavior of the product objects created by the factory. Each product family has its set of abstract product interfaces.
- Concrete Products: Concrete product classes implement the abstract product interfaces. These classes represent the actual objects that the client code will use.
- Client: The client code works with the abstract factory and abstract product interfaces. It doesn't need to know the concrete classes of the products it uses. Instead, it relies on the factory to create compatible objects.
Note: The Abstract Factory Pattern is particularly useful in situations where we need to ensure that the products created by a factory are consistent and work together.
Example:
In a GUI application, you might have different themes (e.g., light theme and dark theme) where each theme includes a set of related UI components lie buttons, windows, dialogs, etc. Abstract Factory Pattern can be used to create UI components that are consistent with the selected theme without the client code having to be aware of the specific theme or component implementations.
Implementation of the Abstract Factory Pattern in C++ Design Patterns
Let's look at the example of the Abstract Factory Pattern in the context of creating different types of pizzas in a pizza restaurant.
Example:
We'll have two styles of pizza: "New York" and "Chicago," each with its own set of pizza types.
- Step 1: We'll define an abstract factory interface (PizzaFactory) with methods for creating different types of pizzas for different styles.
- Step 2: We'll create two concrete factories (NewYorkPizzaFactory and ChicagoPizzaFactory) implementing this interface and create corresponding New York and Chicago style pizzas.
- Step 3: We'll create an abstract product interface (Pizza) that defines the methods that concrete pizzas must implement.Concrete pizza classes (NewYorkCheesePizza, NewYorkPepperoniPizza, ChicagoCheesePizza, ChicagoPepperoniPizza) implement the abstract Pizza interface for each style.
- The client code will create and prepare pizzas using the abstract factories and interact with them without needing to know the specific pizza styles or implementations.
This pattern allows for easy addition of new pizza styles without affecting the existing code
C++
#include <iostream>
// Abstract Product Interface
class Pizza {
public:
virtual void bake() = 0;
virtual void cut() = 0;
virtual void box() = 0;
};
// Concrete New York Cheese Pizza
class NewYorkCheesePizza : public Pizza {
public:
void bake() override
{
std::cout << "Baking New York-style cheese pizza."
<< std::endl;
}
void cut() override
{
std::cout << "Cutting New York-style cheese pizza."
<< std::endl;
}
void box() override
{
std::cout << "Boxing New York-style cheese pizza."
<< std::endl;
}
};
// Concrete New York Pepperoni Pizza
class NewYorkPepperoniPizza : public Pizza {
public:
void bake() override
{
std::cout
<< "Baking New York-style pepperoni pizza."
<< std::endl;
}
void cut() override
{
std::cout
<< "Cutting New York-style pepperoni pizza."
<< std::endl;
}
void box() override
{
std::cout
<< "Boxing New York-style pepperoni pizza."
<< std::endl;
}
};
// Concrete Chicago Cheese Pizza
class ChicagoCheesePizza : public Pizza {
public:
void bake() override
{
std::cout << "Baking Chicago-style cheese pizza."
<< std::endl;
}
void cut() override
{
std::cout << "Cutting Chicago-style cheese pizza."
<< std::endl;
}
void box() override
{
std::cout << "Boxing Chicago-style cheese pizza."
<< std::endl;
}
};
// Concrete Chicago Pepperoni Pizza
class ChicagoPepperoniPizza : public Pizza {
public:
void bake() override
{
std::cout << "Baking Chicago-style pepperoni pizza."
<< std::endl;
}
void cut() override
{
std::cout
<< "Cutting Chicago-style pepperoni pizza."
<< std::endl;
}
void box() override
{
std::cout << "Boxing Chicago-style pepperoni pizza."
<< std::endl;
}
};
// Abstract Factory Interface
class PizzaFactory {
public:
virtual Pizza* createCheesePizza() = 0;
virtual Pizza* createPepperoniPizza() = 0;
};
// Concrete New York Pizza Factory
class NewYorkPizzaFactory : public PizzaFactory {
public:
Pizza* createCheesePizza() override
{
return new NewYorkCheesePizza();
}
Pizza* createPepperoniPizza() override
{
return new NewYorkPepperoniPizza();
}
};
// Concrete Chicago Pizza Factory
class ChicagoPizzaFactory : public PizzaFactory {
public:
Pizza* createCheesePizza() override
{
return new ChicagoCheesePizza();
}
Pizza* createPepperoniPizza() override
{
return new ChicagoPepperoniPizza();
}
};
int main()
{
// Create a New York Pizza Factory
PizzaFactory* newYorkFactory
= new NewYorkPizzaFactory();
Pizza* newYorkCheesePizza
= newYorkFactory->createCheesePizza();
Pizza* newYorkPepperoniPizza
= newYorkFactory->createPepperoniPizza();
// Create a Chicago Pizza Factory
PizzaFactory* chicagoFactory
= new ChicagoPizzaFactory();
Pizza* chicagoCheesePizza
= chicagoFactory->createCheesePizza();
Pizza* chicagoPepperoniPizza
= chicagoFactory->createPepperoniPizza();
// Order and prepare the pizzas
newYorkCheesePizza->bake();
newYorkCheesePizza->cut();
newYorkCheesePizza->box();
newYorkPepperoniPizza->bake();
newYorkPepperoniPizza->cut();
newYorkPepperoniPizza->box();
chicagoCheesePizza->bake();
chicagoCheesePizza->cut();
chicagoCheesePizza->box();
chicagoPepperoniPizza->bake();
chicagoPepperoniPizza->cut();
chicagoPepperoniPizza->box();
// Clean up
delete newYorkFactory;
delete newYorkCheesePizza;
delete newYorkPepperoniPizza;
delete chicagoFactory;
delete chicagoCheesePizza;
delete chicagoPepperoniPizza;
return 0;
}
Output:
Baking New York-style cheese pizza.
Cutting New York-style cheese pizza.
Boxing New York-style cheese pizza.
Baking New York-style pepperoni pizza.
Cutting New York-style pepperoni pizza.
Boxing New Y...
Advantages of the Abstract Factory Pattern in C++ Design Patterns
- Consistency: It ensures that objects created by a factory are compatible and consistent within a family, improving the overall system's integrity.
- Flexibility: The pattern allows for the easy addition of new product families or variations without modifying existing client code.
- Encapsulation: Concrete classes are encapsulated within their respective factories, reducing dependencies and making it easier to manage changes.
Disadvantages of the Abstract Factory Pattern in C++ Design Patterns
- Complexity: Implementing the pattern can lead to a large number of classes and interfaces, potentially increasing code complexity.
- Rigidity: Modifying or extending a product family may require changes in multiple places, making the system less flexible.
- Runtime Costs: Creating objects through factories can introduce some runtime overhead, although it's usually negligible.
Similar Reads
Modern C++ Design Patterns Tutorial Design patterns in C++ help developers create maintainable, flexible, and understandable code. They encapsulate the expertise and experience of seasoned software architects and developers, making it easier for newer programmers to follow established best practices. What are C++ Design Patterns?A des
7 min read
Creational Software Design Patterns in C++
Factory Method Pattern | C++ Design Patterns Factory Method Pattern provides an interface for creating objects but leaves the actual object instantiation to derived classes. This allows for flexibility in object creation and promotes loose coupling between the client code and the concrete products.Factory Method Pattern | C++ Design PatternsTa
8 min read
Abstract Factory Pattern | C++ Design Patterns Abstract Factory Pattern is a creational design pattern used in object-oriented programming. It provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern is a way to encapsulate the creation of objects and ensure that they are
6 min read
Builder Pattern | C++ Design Patterns The builder pattern is defined as a creational design pattern that separates the construction of a complex object from its representation, allowing us to create different representations of an object using the same construction process. It's beneficial when an object has many optional properties or
6 min read
Prototype Pattern | C++ Design Patterns When designing software, it's crucial to make it efficient, easy to reuse, and simple to maintain. One way to achieve these goals is by using design patterns, and one such pattern is the Prototype Pattern. In this article, we'll explore the Prototype Design Pattern in the context of C++. Important T
5 min read
Singleton Pattern | C++ Design Patterns A singleton pattern is a design pattern that ensures that only one instance of a class can exist in the entire program. This means that if you try to create another instance of the class, it will return the same instance that was created earlier. The Singleton pattern is useful when we need to have
11 min read
Structural Software Design Patterns in C++
Adapter Pattern | C++ Design Patterns Adapter Pattern is a structural design pattern used to make two incompatible interfaces work together. It acts as a bridge between two incompatible interfaces, allowing them to collaborate without modifying their source code. This pattern is particularly useful when integrating legacy code or third-
6 min read
Bridge Method | C++ Design Patterns Bridge Pattern is basically a structural design pattern in software engineering or in C++ programming that is used to separate an object's abstraction from its implementation. It is part of the Gang of Four (GoF) design patterns and is particularly useful when we need to avoid a permanent binding be
9 min read
`;
$(commentSectionTemplate).insertBefore(".article--recommended");
}
loadComments();
});
});
function loadComments() {
if ($("iframe[id*='discuss-iframe']").length top_of_element && top_of_screen articleRecommendedTop && top_of_screen articleRecommendedBottom)) {
if (!isfollowingApiCall) {
isfollowingApiCall = true;
setTimeout(function(){
if (loginData && loginData.isLoggedIn) {
if (loginData.userName !== $('#followAuthor').val()) {
is_following();
} else {
$('.profileCard-profile-picture').css('background-color', '#E7E7E7');
}
} else {
$('.follow-btn').removeClass('hideIt');
}
}, 3000);
}
}
});
}
$(".accordion-header").click(function() {
var arrowIcon = $(this).find('.bottom-arrow-icon');
arrowIcon.toggleClass('rotate180');
});
});
window.isReportArticle = false;
function report_article(){
if (!loginData || !loginData.isLoggedIn) {
const loginModalButton = $('.login-modal-btn')
if (loginModalButton.length) {
loginModalButton.click();
}
return;
}
if(!window.isReportArticle){
//to add loader
$('.report-loader').addClass('spinner');
jQuery('#report_modal_content').load(gfgSiteUrl+'wp-content/themes/iconic-one/report-modal.php', {
PRACTICE_API_URL: practiceAPIURL,
PRACTICE_URL:practiceURL
},function(responseTxt, statusTxt, xhr){
if(statusTxt == "error"){
alert("Error: " + xhr.status + ": " + xhr.statusText);
}
});
}else{
window.scrollTo({ top: 0, behavior: 'smooth' });
$("#report_modal_content").show();
}
}
function closeShareModal() {
const shareOption = document.querySelector('[data-gfg-action="share-article"]');
shareOption.classList.remove("hover_share_menu");
let shareModal = document.querySelector(".hover__share-modal-container");
shareModal && shareModal.remove();
}
function openShareModal() {
closeShareModal(); // Remove existing modal if any
let shareModal = document.querySelector(".three_dot_dropdown_share");
shareModal.appendChild(Object.assign(document.createElement("div"), { className: "hover__share-modal-container" }));
document.querySelector(".hover__share-modal-container").append(
Object.assign(document.createElement('div'), { className: "share__modal" }),
);
document.querySelector(".share__modal").append(Object.assign(document.createElement('h1'), { className: "share__modal-heading" }, { textContent: "Share to" }));
const socialOptions = ["LinkedIn", "WhatsApp","Twitter", "Copy Link"];
socialOptions.forEach((socialOption) => {
const socialContainer = Object.assign(document.createElement('div'), { className: "social__container" });
const icon = Object.assign(document.createElement("div"), { className: `share__icon share__${socialOption.split(" ").join("")}-icon` });
const socialText = Object.assign(document.createElement("span"), { className: "share__option-text" }, { textContent: `${socialOption}` });
const shareLink = (socialOption === "Copy Link") ?
Object.assign(document.createElement('div'), { role: "button", className: "link-container CopyLink" }) :
Object.assign(document.createElement('a'), { className: "link-container" });
if (socialOption === "LinkedIn") {
shareLink.setAttribute('href', `https://www.linkedin.com/sharing/share-offsite/?url=${window.location.href}`);
shareLink.setAttribute('target', '_blank');
}
if (socialOption === "WhatsApp") {
shareLink.setAttribute('href', `https://api.whatsapp.com/send?text=${window.location.href}`);
shareLink.setAttribute('target', "_blank");
}
if (socialOption === "Twitter") {
shareLink.setAttribute('href', `https://twitter.com/intent/tweet?url=${window.location.href}`);
shareLink.setAttribute('target', "_blank");
}
shareLink.append(icon, socialText);
socialContainer.append(shareLink);
document.querySelector(".share__modal").appendChild(socialContainer);
//adding copy url functionality
if(socialOption === "Copy Link") {
shareLink.addEventListener("click", function() {
var tempInput = document.createElement("input");
tempInput.value = window.location.href;
document.body.appendChild(tempInput);
tempInput.select();
tempInput.setSelectionRange(0, 99999); // For mobile devices
document.execCommand('copy');
document.body.removeChild(tempInput);
this.querySelector(".share__option-text").textContent = "Copied"
})
}
});
// document.querySelector(".hover__share-modal-container").addEventListener("mouseover", () => document.querySelector('[data-gfg-action="share-article"]').classList.add("hover_share_menu"));
}
function toggleLikeElementVisibility(selector, show) {
document.querySelector(`.${selector}`).style.display = show ? "block" : "none";
}
function closeKebabMenu(){
document.getElementById("myDropdown").classList.toggle("show");
}