🏭 Episode 3 – The LEGO Factory: One Command, Many Creations

LEGO Factory Fun in Friendopia

In Episode 2, Prisha and her friends experienced chaos while constructing their LEGO towers. With bricks crumbling and design failures, they learned the Builder Pattern — how to construct complex structures step by step from blueprints. Friendopia’s towers finally stood tall and proud! 

Friendopia was growing rapidly. Houses were constructed and towers erected, and now the city required transport.

“We need cars, boats, and helicopters!” Arijay exclaimed.

Every child enjoys constructing a different vehicle in LEGO:

Prisha enjoys constructing Cars.

Arijay is addicted to Helicopters.

Nimisha is into really cool Boats.

But every time they wanted to construct another LEGO vehicle, they had to repeat the same thing:

  • Select the correct bricks
  • Recall how to assemble

It was like re-inventing the wheel again!  

The sun beat down high over the race track of Friendopia. Prisha boasted to her friends about her fresh LEGO racing car.

But if she pushed hard, the wheels would not spin correctly.

Oh no!” Prisha wrinkled her face.

“I had forgotten to install axle pieces in the wheels. The car just slides!”

Arijay attempted to repair it, but they understood that they did not recall specifically what steps and bricks to undertake to construct a working wheel.

Nimisha constructed a LEGO boat close to the pond, excited to see if it would float and travel.

She glued bricks on progressively, but as soon as she placed them in water, they flipped over and sank a short distance.

“Oh no! I’ve done the bottom with roof bricks instead of flat, smooth pieces,” replied Nimisha. 

“And I didn’t put the propeller at the back!”

They didn’t use a list or a “formula” to build boats — each time they made one, it was a little bit different and didn’t always function.

On top of the hill, Arijay was building a LEGO helicopter to save a cat stuck in a tree.

He rapidly assembled the bricks but did not build the tiny tail rotor. Without the rotor, the helicopter could not level itself.

“Ah, no!” said Arijay, as his helicopter vibrated dangerously.

“I never do recall the tail rotor… But I don’t want to disassemble it again!”

The friends were frustrated — every vehicle was special, but building each one felt like a puzzle they kept solving from scratch.

The children line up around the unassembled vehicle.

Why does each LEGO vehicle have to be a fresh start?” Nimisha wondered.

“We need a more intelligent, faster way to build!

That’s when Mr. Kohli arrived with his smile and stated:

“Sounds like you would require a LEGO Factorysomething by which to program the factory as to what you’d like, and it just builds the car for you.”

Mr. Kohli stepped in again.

“Whenever you want to create something, you do the same thing again and again. Let us think we have a LEGO factory for every kind of vehicle.”

The Factory Pattern allows you to delegate the object creation to the subclasses. Rather than calling a new Car(), you request a “factory” to provide you with the product.

It answers:

🔹 “What should I make?”
🔹 “How do I choose what LEGO set to go back to?”

Note: “That’s the Factory Design pattern. You have lots of factories, each of which builds some specific thing, but all from the same blueprint!”

Just as a city has:

  • A car factory
  • A boat factory
  • A helicopter factory

Friendopia can too!

Each one of them has the same underlying LEGO template, but they build something different.

Factory Pattern Structure

Imagine a pizza shop:

  • You don’t build the pizza yourself.
  • You walk in and say, “I’d like a pepperoni pizza.”
  • The kitchen (or factory) receives the message to produce it and gives you the completed product.

In our LEGO world:

  • You say, “I require a car.”
  • The CarFactory gives you a pre-assembled LEGO car.
RoleResponsibilityLEGO Example
ProductAbstract representation of what’s being createdLegoVehicle
Concrete ProductActual implementationCar, Boat, Helicopter
Creator (Factory)Abstract factory that defines CreateVehicle()LegoFactory (abstract)
Concrete CreatorSpecific factories like CarFactory, BoatFactoryCarFactory, BoatFactory, HelicopterFactory
ClientThe one requesting the productThe kids / OrderCenter

Mr. Kohli provided them with three little boxes labelled:

🏭 CarFactory

🏭 BoatFactory

🏭 HelicopterFactory

Each box is a factory that can build a particular product.

All they had to do was call the factory, and out came a perfectly built vehicle! 

Factory Pattern Example Structure

Mr. Kohli opened his laptop and demonstrated how the same logic works. .net.

🏙️ Product

public abstract class LegoVehicle
{
	public abstract string Name { get; }
            public abstract void Assemble();
}

Concrete Products

public class Car : LegoVehicle
{
    public override string Name => "LEGO Car";
    public override void Assemble() => Console.WriteLine("Attaching wheels and body...");
}

public class Boat : LegoVehicle
{
    public override string Name => "LEGO Boat";
    public override void Assemble() => Console.WriteLine("Snapping hull and sails...");
}

public class Helicopter : LegoVehicle
{
    public override string Name => "LEGO Helicopter";
    public override void Assemble() => Console.WriteLine("Building rotors and cockpit...");
}

Factory

public abstract class LegoFactory
{
    public abstract LegoVehicle CreateVehicle();

    public LegoVehicle Build()
    {
        var vehicle = CreateVehicle();
        vehicle.Assemble();
        Console.WriteLine($"{vehicle.Name} is ready!");
        return vehicle;
    }
}

Concrete Factories

public class CarFactory : LegoFactory
{
    public override LegoVehicle CreateVehicle() => new Car();
}

public class BoatFactory : LegoFactory
{
    public override LegoVehicle CreateVehicle() => new Boat();
}

public class HelicopterFactory : LegoFactory
{
    public override LegoVehicle CreateVehicle() => new Helicopter();
}

Client

public class VehicleOrderCenter
{
    public void ProcessOrder(LegoFactory factory)
    {
       var factories = new List<LegoFactory>
	{
    		new CarFactory(),
    		new BoatFactory(),
    		new HelicopterFactory()
	};

	foreach (var factory in factories)
	{
    		var product = factory.Build();
	} 
     }
}

Prisha and her friends used their Vehicle Factory to build cars, boats, and helicopters.

“Where will the people live?” asked Nimisha.
“And where will they shop?” Arijay added.
Prisha clapped her hands. “We need buildings!”

Just then, Mr. Kohli walked in with a rolled-up blueprint in one hand and a steaming cup of tea in the other.

“Ah, you’ve reached the next level,” he smiled. “Let’s open… the Building Factory!”

Now Friendopia has:

  • 🚗 A Vehicle Factory (CarFactory, BoatFactory, HelicopterFactory)
  • 🏠 A Building Factory (HouseFactory, ShopFactory)

Both use the Factory Method pattern independently. They don’t return families of objects, so they do not form an Abstract Factory.

public class GenericFactory<T> where T : LegoVehicle, new()
{
    public T Create() => new T();
}

You can inject factories using DI containers like Autofac or .NET’s built-in container.

services.AddTransient<CarFactory>();
services.AddTransient<BoatFactory>(); 

If factory classes manage shared resources (like caching), ensure:

  • Singleton factories are thread-safe
  • Products are stateless or properly scoped
  • You need to decouple product creation from client code
  • You are expecting new kinds of products in the future
  • You desire flexibility in object creation logic
  • You have only one type of object to create
  • Object creation is straightforward and will not undergo significant changes.
  • Factories are overkill for tiny objects 

🎁 Wraps object creation logic

🚪Adheres to the Open/Closed Principle

Simple to add new types

🔀 Reduces conditional logic

PatternPurpose
BuilderStep-by-step construction
Factory MethodChooses and returns the right object

🧱 Episode 4 – Abstract LEGO Factories: Building Product Families

DON’T MISS ANY OPPORTUNITY

Scroll to Top