Last active
March 16, 2021 16:24
-
-
Save bbqchickenrobot/e9520c061f809c38f9bef1505e113a2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace WebApplication1 | |
{ | |
public interface IDoSomething | |
{ | |
void DoSomething(object param); | |
} | |
public class DoerOfSomething : IDoSomething | |
{ | |
public void DoSomething(object param) | |
{ | |
Console.WriteLine("I do something"); | |
} | |
} | |
public class DoerOfSomethingElse : IDoSomething | |
{ | |
public void DoSomething(object param) | |
{ | |
Console.WriteLine("I do something different"); | |
} | |
} | |
public class LooselyCoupled | |
{ | |
private IDoSomething op; // loosely coupled as it's tied to abstraction (interface) | |
public LooselyCoupled(IDoSomething op) | |
{ | |
// the op dependency can be ANY concrete type that inherits from IDoSomething | |
// i.e. - DoerOfSomething OR DoerOfSomethingElse can be passed in achieving polymorphism | |
this.op = op; | |
} | |
public void PerformNeededTask() => op.DoSomething(new object()); | |
} | |
public class TightlyCoupled | |
{ | |
private DoerOfSomething doer; // tightly linked to DoerOfSomething class (concrete implementation) | |
public TightlyCoupled(DoerOfSomething op) // tightly linked to DoerOfSomething | |
{ | |
// the op dependency can ONLY be of type DoerOfSomething | |
this.doer = op; | |
} | |
public void PerformNeededTask() => doer.DoSomething(new object()); | |
} | |
public class BadCodingExample | |
{ | |
IDoerOfSomething op; | |
public BadCodingExample() | |
{ | |
this.op = new DoerOfSomething(); // breaks polymorphism + SOLID | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment