Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 225 additions & 0 deletions src/content/design-pattern/code/abstract-factory/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
#include <iostream>
#include <memory>
#include <string>

/**
* Abstract Factory Pattern - Cross-Platform Graphics Framework
* Creates families of related objects for different rendering engines
*/

// Abstract products
class Renderer {
public:
virtual ~Renderer() = default;
virtual void renderShape(const std::string& shape) = 0;
virtual void setColor(const std::string& color) = 0;
};

class Window {
public:
virtual ~Window() = default;
virtual void create(int width, int height) = 0;
virtual void show() = 0;
virtual void close() = 0;
};

// Concrete OpenGL products
class OpenGLRenderer : public Renderer {
private:
std::string currentColor = "white";

public:
void renderShape(const std::string& shape) override {
std::cout << "OpenGL: Rendering " << shape << " with hardware acceleration" << std::endl;
std::cout << "OpenGL: Using vertex shaders and " << currentColor << " color" << std::endl;
}

void setColor(const std::string& color) override {
currentColor = color;
std::cout << "OpenGL: Setting color to " << color << " using RGB values" << std::endl;
}
};

class OpenGLWindow : public Window {
private:
int width, height;
bool isOpen = false;

public:
void create(int w, int h) override {
width = w;
height = h;
std::cout << "OpenGL: Creating window " << width << "x" << height
<< " with double buffering" << std::endl;
}

void show() override {
isOpen = true;
std::cout << "OpenGL: Showing window with hardware-accelerated context" << std::endl;
}

void close() override {
isOpen = false;
std::cout << "OpenGL: Closing window and cleaning up OpenGL context" << std::endl;
}
};

// Concrete DirectX products
class DirectXRenderer : public Renderer {
private:
std::string currentColor = "white";

public:
void renderShape(const std::string& shape) override {
std::cout << "DirectX: Rendering " << shape << " with Direct3D pipeline" << std::endl;
std::cout << "DirectX: Using HLSL shaders and " << currentColor << " color" << std::endl;
}

void setColor(const std::string& color) override {
currentColor = color;
std::cout << "DirectX: Setting color to " << color << " using DXGI format" << std::endl;
}
};

class DirectXWindow : public Window {
private:
int width, height;
bool isOpen = false;

public:
void create(int w, int h) override {
width = w;
height = h;
std::cout << "DirectX: Creating window " << width << "x" << height
<< " with DXGI swap chain" << std::endl;
}

void show() override {
isOpen = true;
std::cout << "DirectX: Showing window with DirectX 12 context" << std::endl;
}

void close() override {
isOpen = false;
std::cout << "DirectX: Closing window and releasing DirectX resources" << std::endl;
}
};

// Abstract Factory
class GraphicsFactory {
public:
virtual ~GraphicsFactory() = default;
virtual std::unique_ptr<Renderer> createRenderer() = 0;
virtual std::unique_ptr<Window> createWindow() = 0;
};

// Concrete Factories
class OpenGLFactory : public GraphicsFactory {
public:
std::unique_ptr<Renderer> createRenderer() override {
return std::make_unique<OpenGLRenderer>();
}

std::unique_ptr<Window> createWindow() override {
return std::make_unique<OpenGLWindow>();
}
};

class DirectXFactory : public GraphicsFactory {
public:
std::unique_ptr<Renderer> createRenderer() override {
return std::make_unique<DirectXRenderer>();
}

std::unique_ptr<Window> createWindow() override {
return std::make_unique<DirectXWindow>();
}
};

// Client application
class GraphicsApplication {
private:
std::unique_ptr<Renderer> renderer;
std::unique_ptr<Window> window;

public:
GraphicsApplication(std::unique_ptr<GraphicsFactory> factory) {
renderer = factory->createRenderer();
window = factory->createWindow();
}

void initialize() {
std::cout << "Initializing graphics application..." << std::endl;
window->create(800, 600);
window->show();
}

void render() {
std::cout << "\n--- Rendering Scene ---" << std::endl;

renderer->setColor("blue");
renderer->renderShape("triangle");

renderer->setColor("red");
renderer->renderShape("rectangle");

renderer->setColor("green");
renderer->renderShape("circle");
}

void cleanup() {
std::cout << "\nCleaning up graphics resources..." << std::endl;
window->close();
}
};

int main() {
std::cout << "=== Abstract Factory Pattern - Graphics Framework ===\n" << std::endl;

// Platform detection (simplified)
std::string platform;

#ifdef _WIN32
platform = "Windows";
#else
platform = "Linux";
#endif

std::cout << "Detected platform: " << platform << std::endl;

std::unique_ptr<GraphicsFactory> factory;

if (platform == "Windows") {
std::cout << "Creating DirectX Graphics Factory..." << std::endl;
factory = std::make_unique<DirectXFactory>();
} else {
std::cout << "Creating OpenGL Graphics Factory..." << std::endl;
factory = std::make_unique<OpenGLFactory>();
}

// Create and run application
GraphicsApplication app(std::move(factory));

app.initialize();
app.render();
app.cleanup();

std::cout << "\n--- Testing Both Graphics APIs ---" << std::endl;

// Demo both graphics systems
std::cout << "\nOpenGL Graphics System:" << std::endl;
auto openglFactory = std::make_unique<OpenGLFactory>();
GraphicsApplication openglApp(std::move(openglFactory));
openglApp.initialize();
openglApp.render();
openglApp.cleanup();

std::cout << "\nDirectX Graphics System:" << std::endl;
auto directxFactory = std::make_unique<DirectXFactory>();
GraphicsApplication directxApp(std::move(directxFactory));
directxApp.initialize();
directxApp.render();
directxApp.cleanup();

return 0;
}
160 changes: 160 additions & 0 deletions src/content/design-pattern/code/abstract-factory/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/**
* Abstract Factory Pattern - GUI Framework Example
* Creates families of related objects (Windows/Mac UI components) without specifying their concrete classes
*/

// Abstract products
interface Button {
void click();
void render();
}

interface Checkbox {
void check();
void render();
}

// Concrete Windows products
class WindowsButton implements Button {
@Override
public void click() {
System.out.println("Windows button clicked - Opening file dialog");
}

@Override
public void render() {
System.out.println("Rendering Windows-style button with system theme");
}
}

class WindowsCheckbox implements Checkbox {
@Override
public void check() {
System.out.println("Windows checkbox toggled with system sound");
}

@Override
public void render() {
System.out.println("Rendering Windows-style checkbox with blue checkmark");
}
}

// Concrete Mac products
class MacButton implements Button {
@Override
public void click() {
System.out.println("Mac button clicked - Smooth animation effect");
}

@Override
public void render() {
System.out.println("Rendering Mac-style button with rounded corners");
}
}

class MacCheckbox implements Checkbox {
@Override
public void check() {
System.out.println("Mac checkbox toggled with subtle haptic feedback");
}

@Override
public void render() {
System.out.println("Rendering Mac-style checkbox with gradient effect");
}
}

// Abstract Factory
interface GUIFactory {
Button createButton();
Checkbox createCheckbox();
}

// Concrete Factories
class WindowsFactory implements GUIFactory {
@Override
public Button createButton() {
return new WindowsButton();
}

@Override
public Checkbox createCheckbox() {
return new WindowsCheckbox();
}
}

class MacFactory implements GUIFactory {
@Override
public Button createButton() {
return new MacButton();
}

@Override
public Checkbox createCheckbox() {
return new MacCheckbox();
}
}

// Client application
class Application {
private Button button;
private Checkbox checkbox;

public Application(GUIFactory factory) {
this.button = factory.createButton();
this.checkbox = factory.createCheckbox();
}

public void renderUI() {
button.render();
checkbox.render();
}

public void handleInteraction() {
button.click();
checkbox.check();
}
}

public class AbstractFactoryDemo {
public static void main(String[] args) {
System.out.println("=== Abstract Factory Pattern Demo ===\n");

// Determine OS and create appropriate factory
String os = System.getProperty("os.name").toLowerCase();
GUIFactory factory;

if (os.contains("win")) {
System.out.println("Creating Windows GUI Factory...");
factory = new WindowsFactory();
} else if (os.contains("mac")) {
System.out.println("Creating Mac GUI Factory...");
factory = new MacFactory();
} else {
System.out.println("Defaulting to Windows GUI Factory...");
factory = new WindowsFactory();
}

// Create application with platform-specific components
Application app = new Application(factory);

System.out.println("\n--- Rendering UI Components ---");
app.renderUI();

System.out.println("\n--- User Interactions ---");
app.handleInteraction();

System.out.println("\n--- Testing Both Factories ---");

// Demo both factories
System.out.println("\nWindows Factory:");
Application windowsApp = new Application(new WindowsFactory());
windowsApp.renderUI();
windowsApp.handleInteraction();

System.out.println("\nMac Factory:");
Application macApp = new Application(new MacFactory());
macApp.renderUI();
macApp.handleInteraction();
}
}
Loading