Posts

Showing posts with the label C# | Factory Design Pattern

C# | Factory Design Pattern | Creational Design Pattern

Image
Defines an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. This structural code demonstrates the Factory method offering great flexibility in creating different objects. The Abstract class may provide a default object, but each subclass can instantiate an extended version of the object. Code Syntax - public interface ICar {     void Start(); } public class SixSeater : ICar {     public void Start()     {         throw new NotImplementedException();     } } public class FourSeater : ICar {     public void Start()     {         throw new NotImplementedException();     } } public class CarFactory {     public ICar GetCar(string carType)     {          switch(carType)         {           ...