Skip to main content

Posts

Showing posts from May, 2025

6_Observer Design Pattern

 observer design pattern is a behavioral design pattern. Behavioral design pattern deals with the interaction of object of different classes within the system. Definition: One Subject state is change, the observer get notified about the change. In Simple words When something happened, inform information to all people. You subscribe the channel(observer) The channel is subject When the new video is upload( state changes ), all subscribers are notified .  Php Code Example Step 1: Define observer Interface <?php interface Observer {     public function update ( string $message ); } ? > Step 2: Define Subject Interface <?php interface Subject {     public function attach ( Observer $observer );     public function detach ( Observer $observer );     public function notify ( string $message ); } ? > Step 3: Create Concrete Subject e.g New Publisher <?php class NewsPublisher implements Subject {     ...

5_example scenario of abstract pattern

 For creating better understanding, let implement the abstract design pattern the way it fit in real scenario. There ia a company on large scale build cars in large numbers. They build different types of cars. Petrol, Electric, Hybrid. But there was a big problem Problem: Wrong Parts, Wrong Car Car builder mixes the parts in cars, Put petrol engine in electric car and Electric tyres in Petrol car. In result the car explosion, customers got angry and choas! Than Leadership of the company summoned the best engineers to fix this problem. He said we need a system where, 1. Right part fit in the right car. 2. Builder no need to know the making of the parts they just build the car. 3. We can add new car easily in future. So Engineers decide to have a Abstract Factory Design pattern. Abstract Factory Engineer Build the abstract factory(Main Factory) that is blueprint(Interface). This will decide what will be build in the factory. <?php interface CarFactory {     public fun...

4_builder design pattern

What was the problem in factory design pattern? We create a class and assign all responsibilities for object creation. This class have static method also called static factory. Remember we pass parameters to factory method and these parameters will be pass to the next class constructor, whose object we want to create. What was the problem? The parameters we want to pass in next class constructor, first they all need to pass to factory method. The order of the parameters matters. Might be some parameters are optional. Therefore management of parameters is very difficult. Imagine you are building  a system for Car, Burger, Computer. These system have 1. multiple parts. 2. can be created with different configuration 3. some parts are optional 4. You can't pass 10+ argument to construction order management start complex. Without Builder pattern Your constructor is become messy. <?php $car = new Car ( "Red" , "SUV" , true , true , false , "Auto" , ...

3_Abstract Factory Pattern

Abstract factory pattern provide concept of factory of factories.  Abstract design pattern is on of the creational design pattern that provide us the interface of creating related or independent objects without specifying the concrete classes and their specifications. In other words,  Abstract design pattern is the way of organizing, How we can create groups of things that are related to each other. Think of it, like factory of factories. Let walk through the practical example of car production system using abstract factory design pattern for getting better understanding. Scenario: we are building card of two different types , ElectricCars and PetrolCars. Each car have different types of  1) Engine 2)Tyres Goals: Using abstract design pattern we have to create families of Electric and Petrol car parts. What types of classes will be use in this design pattern. 1) Abstract Products (Interface). 2) Concrete Products: These are concrete classes which implement Abstract Produ...