Skip to content

Instantly share code, notes, and snippets.

@JuanjoFuchs
Created June 8, 2012 15:21
Show Gist options
  • Save JuanjoFuchs/2896132 to your computer and use it in GitHub Desktop.
Save JuanjoFuchs/2896132 to your computer and use it in GitHub Desktop.
Ejemplo del principio de Inversión de Dependencias (DIP)
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;
}
}
}
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;
}
}
}
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());
}
}
}
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