Creational Design Patterns in C#: Building Flexible and Scalable Software by Vaskaran Sarcar

Creational Design Patterns in C#: Building Flexible and Scalable Software by Vaskaran Sarcar

Author:Vaskaran Sarcar
Language: eng
Format: mobi
ISBN: 9798868815676
Publisher: Apress
Published: 2025-07-15T00:00:00+00:00


PRIMARY CONSTRUCTOR MAKES THE CODE CONCISE

Using the Primary constructor (introduced in C#12) and removing the _type field, you can make the Car class concise as follows:

#region The code segment that uses a primary constructor

class Car(string type)

{

/*

* You can use any data structure that you prefer.

* I have used LinkedList<String> in this case.

*/

private LinkedList<string> _parts = new();

// There are no other changes in the remaining code

#endregion

You can make similar changes in equivalent code as well. I showed you the code with and without primary constructors to make you familiar with both approaches. Sometimes I avoid writing code that is very specific to the C# programming language because many C# developers are Java developers as well. In fact, the preface of the classical GoF book says that the design patterns require neither unusual language features nor amazing programming tricks with which to astound your friends and managers. However, you can have a different thought. So, you can choose the approach that serves you best.

Typically, a builder interface is needed for creating parts of a Product object. In our example, Builder is an abstract class that plays this role. It contains the methods to build different parts of a product.

In this example, you see an empty method, called AddExhaustSystem, in the Builder class. Why? Ideally, this class should define all possible methods to build various parts of a product (in our example, a car). We can assume that an electric car does not need an exhaust emission system, whereas a traditional car must have such a component. I could make other methods in the Builder class empty as well; however, I forced the concrete builders to implement those methods.

There is another method, called GetCar, in this class. This method retrieves the product once the parts are assembled. Let’s have a look at this class now:

abstract class Builder

{

public abstract void SetMotor();

public abstract void InsertWheels();

public abstract void SetHeadlights();

// The exhaust emissions system is needed for regular cars

public virtual void AddExhaustSystem() { }

// To retrieve the constructed product.

public abstract Car GetCar();

}

In the upcoming examples, the TraditionalCarBuilder and ElectricCarBuilder classes make the traditional cars and electric cars, respectively. In other words, these are the concrete builders that build the internal representations of a Car instance. First, see the ElectricCarBuilder class:

// The ElectricCarBuilder builds electric cars.

class ElectricCarBuilder : Builder

{

private readonly Car _car;

public ElectricCarBuilder()

{

_car = new("electric");

}

public override void SetMotor()

{

_car.Motor = "electric motors and batteries";

_car.Add($"The {_car.Motor} are added.");

}

public override void InsertWheels()

{

_car.Wheels = 4;

_car.Add($"{_car.Wheels} wheels are added to the car.");

}

public override void SetHeadlights()

{

_car.HeadLights = 2;

_car.Add($"{_car.HeadLights} headlights are set.");

}

// NOTE: The electric car does not need the exhaust

// emission system. So, we do not need to override the base

// class method.

// Retrieve the constructed car

public override Car GetCar()

{

return _car;

}

}

Now see the TraditionalCarBuilder class (notice that it overrides the AddExhaustSystem method as well):

// The TraditionalCarBuilder builds traditional cars.

class TraditionalCarBuilder : Builder

{

private readonly Car _car;

public TraditionalCarBuilder()

{

_car = new("traditional");

}

public override void SetMotor()

{

_car.Motor = "internal combustion engine (fueled by diesel)";

_car.Add($"The {_car.Motor} is added.");

}

public override void InsertWheels()

{

_car.Wheels = 6;

_car.Add($"{_car.Wheels} wheels are added to the car.");

}

public override void SetHeadlights()

{

_car.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(56073)
What's Done in Darkness by Kayla Perrin(26615)
The Fifty Shades Trilogy & Grey by E L James(19095)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19079)
Shot Through the Heart by Mercy Celeste(18952)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17131)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(17018)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16893)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16840)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16466)
The Subtle Art of Not Giving a F*ck by Mark Manson(14375)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14157)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13669)
Scorched Earth by Nick Kyme(12785)
Drei Generationen auf dem Jakobsweg by Stein Pia(10980)
Suna by Ziefle Pia(10902)
Scythe by Neal Shusterman(10365)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(10191)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(10147)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(10144)