Friday, May 27, 2022

C++ simple example for Factory code to complex example.

 Simple factory method code:

#include <iostream>
#include <string>

using namespace std;

/*
 * Define an interface for creating an object, but let 
 * subclasses decide which class to instantiate. 
 */
 
class Shape {
    public:
       virtual void Draw() = 0;
       static Shape* ShapeFactory(string type);
};

class Circle : public Shape {
    public:
       void Draw() { cout << "I am circle" << endl; }
       friend class Shape;
};

class Square : public Shape {
    public:
       void Draw() { cout << "I am square" << endl; }
       friend class Shape;
};

class Rectangle : public Shape {
    public:
       void Draw() { cout << "I am rectangle " << endl; }
       friend class Shape;
};

Shape* Shape::ShapeFactory(string type) {
    if ( type == "circle" ) return new Circle();
    if ( type == "square" ) return new Square();
    if ( type == "rectangle" ) return new Rectangle();
    return NULL;
}

int main(){
   Shape* obj1 = Shape::ShapeFactory("circle");
   Shape* obj2 = Shape::ShapeFactory("square");
   Shape* obj3 = Shape::ShapeFactory("rectangle");
   obj1->Draw();
   obj2->Draw();
   obj3->Draw();
}
Abstract Factory:










using AbstractFactoryPattern;
 
AbstractFactory factoryI = new ConcreteFactoryI();
Client clientI = new Client(factoryI);
clientI.Run();
 
AbstractFactory factoryII = new ConcreteFactoryII();
Client clientII = new Client(factoryII);
clientII.Run();
public class VehicleBuilderProvider : IVehicleBuilderProvider
	{
		private readonly Dictionary<VehicleType , IVehicleBuilder> _vehicleSet;

		public VehicleBuilderProvider(CarBuilder car , BykeBuilder byke)
		{
			_vehicleSet = new Dictionary<VehicleType, IVehicleBuilderProvider>()
			{
				{ VehicleType.Car, car},
				{ VehicleType.Byke, byke}
			};
		}

		public IIVehicleBuilderProvider Get(VehicleType vehicleType)
		{
			return _vehicleSet[VehicleType];
		}
	}
Dot net.: 
Controller and factory :
public class StreamController : Controller
{
private readonly StreamFactory streamFactory;
public StreamController(StreamFactory streamFactory)
{
this.streamFactory = streamFactory;
}
[HttpGet("movies/{userSelection}")]
public IEnumerable<string> GetMovies(string userSelection)
{
return streamFactory.GetStreamService(userSelection).ShowMovies();
}
}
public class StreamFactory
{
private readonly IServiceProvider serviceProvider;
public StreamFactory(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
}
public IStreamService GetStreamService(string userSelection)
{
if(userSelection =="Netflix")
return (IStreamService)serviceProvider.GetService(typeof(NetflixStreamService));
return (IStreamService)serviceProvider.GetService(typeof(AmazonStreamService));
}

No comments:

Post a Comment

React Profiler: A Step by step guide to measuring app performance

  As react applications are becoming more and more complex, measuring application performance becomes a necessary task for developers. React...