Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Factory Method Design Pattern
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 //https://www.dotnettricks.com/learn/designpatterns/factory-method-design-pattern-dotnet using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Your code goes here VehicaleFactory objVehicale = new Vehicale(); IProduct objScooter = objVehicale.getProduct("Scooter"); objScooter.drive(50); IProduct objBike = objVehicale.getProduct("Bike"); objBike.drive(50); //IProduct objTruck = objVehical.getProduct("Truck"); } } //Product Interface public interface IProduct { void drive(int km); } //Concrete Product class public class Scooter : IProduct { public void drive(int km) { Console.WriteLine("Scooter drive "+km+" per hour"); } } //Concrete Product class public class Bike : IProduct { public void drive(int km) { Console.WriteLine("Bike drive "+km+" per hour"); } } //Creater class public abstract class VehicaleFactory { public abstract IProduct getProduct(string productType); } //Concrete creater class public class Vehicale : VehicaleFactory { public override IProduct getProduct(string productType) { switch(productType) { case "Scooter" : return new Scooter(); case "Bike" : return new Bike(); default : throw new ArgumentException("Vehicale {0} can not be created", productType); } } } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
30272 Program Ex1
MockTest
Cotton Farm v: (0.0.0.002)
c# abstract
WithoutPortNo
D11
Math 5.83b
clientdata.cs
ASCII -> Braille ASCII
Fórum ➡ What values have the greatest frequency? ♦
Please log in to post a comment.