PatternUsageCommon InUse in Core PHP
⭐ MVCVery HighLaravel, CodeIgniter, SymfonyBuild web applications with separate Model, View, and Controller
⭐ Dependency Injection (DI)Very HighLaravel Service ContainerInject database, mail, logger, or service classes into other classes
⭐ RepositoryVery HighLarge applicationsSeparate database queries from business logic
⭐ FactoryVery HighModel factories, object creationCreate objects like Payment, Notification, or Database drivers
⭐ SingletonHighService container, configurationSingle database connection, configuration manager, logger
⭐ FacadeVery HighDB, Cache, Auth, MailProvide one simple interface to multiple classes (e.g., DatabaseFacade)
⭐ StrategyHighPayment gateways, notificationsSwitch between payment methods, tax calculation, or file upload strategies
⭐ ObserverHighModel eventsSend email, SMS, or log events when data changes
⭐ BuilderHighQuery Builder, EloquentBuild SQL queries or complex objects step by step
⭐ AdapterMediumThird-party APIsIntegrate different payment gateways or external APIs with a common interface
⭐ CommandMediumArtisan commands, queuesExecute background jobs, scheduled tasks, or file processing

1. MVC (Model-View-Controller):

Separate your code into three parts.

  • Model → Data (Database)
  • View → UI (HTML)
  • Controller → Connects Model and View
// Model
class User
{
    public function getName()
    {
        return "John";
    }
}

// Controller
$user = new User();
$name = $user->getName();

// View
echo $name;

2. Factory Pattern:

Creates one object based on input

Suppose you want different payment methods.

class PayPal
{
    public function pay()
    {
        echo "Paid using PayPal";
    }
}

class Stripe
{
    public function pay()
    {
        echo "Paid using Stripe";
    }
}

class PaymentFactory
{
    public function create($type)
    {
        if ($type == "paypal") {
            return new PayPal();
        }

        return new Stripe();
    }
}
$factory = new PaymentFactory();

$payment = $factory->create("paypal");

$payment->pay();

3. Facade Pattern:

 Uses multiple objects to perform one task

Suppose processing an order requires:

  • Payment
  • Inventory
  • Email
class Payment
{
    public function pay()
    {
        echo "Payment Done<br>";
    }
}

class Inventory
{
    public function update()
    {
        echo "Stock Updated<br>";
    }
}

class Email
{
    public function send()
    {
        echo "Email Sent<br>";
    }
}
class OrderFacade
{
    public function placeOrder()
    {
        $payment = new Payment();
        $inventory = new Inventory();
        $email = new Email();

        $payment->pay();
        $inventory->update();
        $email->send();
    }
}
$order = new OrderFacade();

$order->placeOrder();

4. Singleton Design Pattern:

  • Private Static variable
  • Private construct
  • Public static method return class single instance.
class Singleton
{
private static $instance = null;
  private function __construct()
  {
    echo "connect";
  }
  public static function showInstance()
  {
    if(self::$instance == null)
    {
      self::$instance = new static();
    }
    else
    {
      echo "Already connected";
    }
  }
}
$obj1= Singleton::showInstance();

5. Observer Design Pattern:

One object changes → Many objects get notified automatically.

When a new video is uploaded, all subscribers receive a notification.

  • YouTube Channel = Subject
  • Subscribers = Observers

Observer:

interface Observer
{
    public function update($message);
}

class User implements Observer
{
    public function update($message)
    {
        echo "Notification: $message <br>";
    }
}

Subject:

class Channel
{
    private $observers = [];

    public function subscribe(Observer $observer)
    {
        $this->observers[] = $observer;
    }

    public function notify($message)
    {
        foreach ($this->observers as $observer) {
            $observer->update($message);
        }
    }
}

Use It:

$channel = new Channel();

$user1 = new User();
$user2 = new User();

$channel->subscribe($user1);
$channel->subscribe($user2);

$channel->notify("New Video Uploaded");