Skip to content

About

Factory Pattern Simulation in Pure PHP

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

Factory Pattern Simulation in Pure PHP

A simple export management system built with pure PHP using OOP principles and the Factory Design Pattern.

This project demonstrates how to create different export strategies (PDF, TXT, Word, Image) without tightly coupling business logic to concrete implementations.

Note that no actual file is downloaded, This is just a test

🌐 Visit Demo


🎯 What this project demonstrates

  • Factory Design Pattern
  • SOLID Principles
  • Open/Closed Principle (OCP)
  • Dependency Inversion Principle (DIP)
  • Clean OOP Architecture
  • Service-Oriented Design
  • Extensible Export System

🚀 Features

  • Export content as PDF
  • Export content as TXT
  • Export content as Word
  • Export content as Image
  • Factory-based exporter creation
  • Centralized export service
  • Download logging support
  • Clean and maintainable architecture

📂 Project Structure

Assets/

Classes/
├── FileController.php
├── LogController.php

Config/
├── app.php
├── DBConnection.php

Contracts/
├── ExporterInterface.php

Enums/
├── ExportType.php

Exporters/
├── ImageExporter.php
├── PdfExporter.php
├── TxtExporter.php
├── WordExporter.php

Factories/
├── ExportFactory.php

Helper/
├── GeneralHelperFunction.php

Pages/
├── notfound.php

Services/
├── ExportService.php

vendor/

.htaccess
bootstrap.php
check.php
index.php
composer.json
README.md

🏗 Architecture Overview

The application follows a Factory-based architecture.

User Action
      │
      ▼
ExportService
      │
      ▼
ExportFactory
      │
      ├── PdfExporter
      ├── TxtExporter
      ├── WordExporter
      └── ImageExporter

The service layer never directly creates exporter objects.

Instead, it delegates object creation to the Factory, making the system easy to extend and maintain.


⚙️ How It Works

  1. User enters content in the editor.
  2. User selects an export type.
  3. ExportService receives the request.
  4. ExportFactory creates the correct exporter instance.
  5. The selected exporter generates the output.
  6. The file download is simulated.

🧩 Factory Pattern

The Factory Pattern centralizes object creation.

Instead of:

new PdfExporter();
new TxtExporter();
new WordExporter();

the application uses:

$exporter = ExportFactory::make($type);

This removes direct dependencies from the business layer.


🔓 Open For Extension

Adding a new exporter requires only three steps:

1. Create Exporter

class CsvExporter implements ExporterInterface
{
    public function export(string $content): void
    {
        // export logic
    }
}

2. Add Export Type

case CSV;

3. Register In Factory

ExportType::CSV => new CsvExporter(),

No existing business logic needs to be modified.


🧠 SOLID Principles Applied

Single Responsibility Principle (SRP)

Each exporter is responsible for one export format.

Open/Closed Principle (OCP)

New exporters can be added without modifying existing code.

Dependency Inversion Principle (DIP)

The service layer depends on abstractions (ExporterInterface) rather than concrete implementations.


🛠 Requirements

  • PHP 8.1+
  • Composer
  • MySQL

▶️ Installation

Clone the repository:

git clone https://github.com/hesam1996/php-factory.git

Install dependencies:

composer install

Import database:

php_factory.sql

Run the project:

http://127.0.0.1/php-factory Or http://localhost/php-factory

👨‍💻 Author

Implemented by Hesam kazembeygi

📌 My Website

📌 My LinkedIn


📄 License

This project is intended for educational purposes and design pattern practice.

About

Factory Pattern Simulation in Pure PHP

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages