Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Abstract Method Design Pattern in C#
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//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); } } } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0,17 sec, absolute running time: 0,11 sec, cpu time: 0,11 sec, average memory usage: 12 Mb, average nr of threads: 2, absolute service time: 0,32 sec
edit mode
|
history
|
discussion
Scooter drive 50 per hour Bike drive 50 per hour