Created
June 8, 2012 15:21
-
-
Save JuanjoFuchs/2896132 to your computer and use it in GitHub Desktop.
Ejemplo del principio de Inversión de Dependencias (DIP)
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 DIP_HolaMundo.Refactorizado | |
{ | |
public class HolaMundo | |
{ | |
private DateTime _horaDelSaludo; | |
public HolaMundo(DateTime horaDelSaludo) | |
{ | |
_horaDelSaludo = horaDelSaludo; | |
} | |
public string Saludar(string nombre) | |
{ | |
if (_horaDelSaludo.Hour < 12) | |
return "Buenos dias " + nombre; | |
if (_horaDelSaludo.Hour < 19) | |
return "Buenas tardes " + nombre; | |
return "Buenas noches " + nombre; | |
} | |
} | |
} |
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 DIP_HolaMundo.SinRefactorizar | |
{ | |
public class HolaMundo | |
{ | |
public string Saludar(string nombre) | |
{ | |
if (DateTime.Now.Hour < 12) | |
return "Buenos dias " + nombre; | |
if (DateTime.Now.Hour < 19) | |
return "Buenas tardes " + nombre; | |
return "Buenas noches " + nombre; | |
} | |
} | |
} |
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
namespace DIP_Volvo.Refactorizado | |
{ | |
public interface IMotor | |
{ | |
} | |
public class NuevoMotorV12 : IMotor | |
{ | |
} | |
public class Volvo | |
{ | |
private IMotor _motor; | |
public Volvo(IMotor motor) | |
{ | |
_motor = motor; | |
} | |
} | |
public class CarrosServicio | |
{ | |
public void CrearCarro() | |
{ | |
var miVolvo = new Volvo(new NuevoMotorV12()); | |
} | |
} | |
} |
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
namespace DIP_Volvo.SinRefactorizar | |
{ | |
public class B20 | |
{ | |
} | |
public class Volvo | |
{ | |
private B20 _motor; | |
public Volvo() | |
{ | |
_motor = new B20(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment