Advertisement
zoltanvi

SOLID + OOP principles

Jan 7th, 2023
920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace InterviewPreparation
  6. {
  7.     class Solution
  8.     {
  9.         public interface IForward
  10.         {
  11.             void GoForward();
  12.         }
  13.         public interface IBackward
  14.         {
  15.             void GoBackward();
  16.         }
  17.  
  18.         public interface ICar : IForward, IBackward
  19.         {
  20.         }
  21.  
  22.         public class Car : ICar
  23.         {
  24.             public void GoBackward() { }
  25.             public void GoForward() { }
  26.             public virtual void OpenDoor()
  27.             {
  28.                 Console.WriteLine("Open car door");
  29.             }
  30.         }
  31.  
  32.         public class Motor : IForward
  33.         {
  34.             public void GoForward() { }
  35.         }
  36.  
  37.         static void Main(string[] args)
  38.         {
  39.             List<IForward> cars = new List<IForward>();
  40.  
  41.             cars.Add(new Car());
  42.             cars.Add(new Car());
  43.             cars.Add(new Motor());
  44.  
  45.             // Liskov subsitution: the "car" in the loop can even be a motor now,
  46.             // because we handle it through abstraction
  47.             foreach (IForward car in cars)
  48.             {
  49.                 car.GoForward();
  50.             }
  51.  
  52.             // ------------------------------------------------
  53.  
  54.             List<int> aList = new List<int> { 1, 2, 3 };
  55.  
  56.             IEnumerable<int> filtered = Enumerable.Empty<int>();
  57.  
  58.             try
  59.             {
  60.                 // These 2 are the same:
  61.                 filtered = Enumerable.Where<int>(aList, a => throw new Exception());
  62.                 filtered = aList.Where(a => throw new Exception());
  63.             }
  64.             catch (Exception)
  65.             {
  66.                 Console.WriteLine("Catched");
  67.             }
  68.  
  69.             // Throws an exception because here the predicate gets executed.
  70.             List<int> bList = filtered.ToList();
  71.         }
  72.  
  73.  
  74.         // Polimorphism: overloading
  75.         public static void LezDuit() { }
  76.         public static void LezDuit(int a) { }
  77.  
  78.         // Polimorphism: overriding
  79.         public class RedCar : Car
  80.         {
  81.             public override void OpenDoor()
  82.             {
  83.                 base.OpenDoor();
  84.             }
  85.         }
  86.  
  87.         // ------------------------------------------------
  88.  
  89.         public class ComputerFactory
  90.         {
  91.             public void CreateComputer()
  92.             {
  93.                 //var monitor = new LEDMonitor();
  94.                 var monitor = new KatodsugarcsovesMonitor();
  95.                 var keyboard = new MechanicalKeyboard();
  96.  
  97.                 var pc = new Windows98Machine(monitor, keyboard);
  98.             }
  99.         }
  100.  
  101.         public class Windows98Machine
  102.         {
  103.             private MechanicalKeyboard m_Keyboard;
  104.             private IMonitor m_Monitor;
  105.  
  106.             // Dependency inversion: The dependency is injected, and it is changed to abstraction
  107.             public Windows98Machine(IMonitor monitor, MechanicalKeyboard keyboard)
  108.             {
  109.                 m_Monitor = monitor;
  110.                 m_Keyboard = keyboard;
  111.  
  112.                 // Tightly coupled dependency --------
  113.                 //m_Monitor = new Monitor();
  114.                 //m_Keyboard = new MechanicalKeyboard();
  115.             }
  116.         }
  117.  
  118.         public class MechanicalKeyboard { }
  119.         public class LEDMonitor : IMonitor { }
  120.         public class KatodsugarcsovesMonitor : IMonitor { }
  121.         public interface IMonitor { }
  122.     }
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement