GRUPO 1
Cada clase o componente debería tener una única responsabilidad.
class ServicioUsuario
{
public bool PagarDeudas(int userID)
{
var pagadas = false;
if (HayDineroEnElBanco(userID))
{
pagadas = true;
}
return pagadas;
}
public bool HayDineroEnElBanco(int userID)
{
return true;
}
}
class ServicioUsuario
{
private readonly ServicioBanco _servicioBanco;
public servicioUsuario(ServicioBanco servicioBanco)
{
_servicioBanco = servicioBanco;
}
public bool PagarDeudas(int userID)
{
var pagadas = false;
if (_servicioBanco.HayDineroEnElBanco(userID))
{
pagadas = true;
}
return pagadas;
}
}
class ServicioBanco
{
public bool HayDineroEnElBanco(int userID)
{
return true;
}
}
Las entidades de software deben estar abiertas para su extensión, pero cerradas para su modificación.
class Admin
{
public int Id { get; set; }
public string Name { get; set; }
}
class User
{
public int Id { get; set; }
public string Name { get; set; }
}
class UserFeatures
{
public bool UserCanMakeAPayment(User user)
{
return true;
}
public bool AdminCanMakeAPayment(Admin user)
{
return true;
}
}
abstract class BaseUser
{
public int Id { get; set; }
public string Name { get; set; }
}
class Admin : BaseUser { }
class User : BaseUser { }
class UserFeatures
{
public bool CanMakeAPayment(BaseUser user)
{
return true;
}
}
Los objetos de un programa deberían ser reemplazables por instancias de sus subtipos sin alterar el correcto funcionamiento del programa.
public class Rectangle {
private int width;
private int height;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int calculateArea() {
return width * height;
}
}
public class Square extends Rectangle {
@Override public void setWidth(int width) {
super.setWidth(width);
super.setHeight(width);
}
@Override public void setHeight(int height) {
super.setHeight(height);
super.setWidth(height);
}
}
@Test
public void testArea() {
Rectangle r = new Rectangle();
r.setWidth(5);
r.setHeight(4);
assertEquals(20, r.calculateArea());
}
public class Rectangle {
public final int width;
public final int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
}
public class Square extends Rectangle {
public Square(int side) {
super(side, side);
}
}
Muchas interfaces cliente específicas son mejores que una interfaz de propósito general
interface SpecialPower
{
void FireAttack();
void SummonAMonster();
void TimeStop();
}
class FireMage : SpecialPower
{
public void FireAttack()
{
}
public void SummonAMonster()
{
throw new Exception("Can't use that");
}
public void TimeStop()
{
throw new Exception("Can't use that");
}
}
class Summoner : SpecialPower
{
public void FireAttack()
{
throw new Exception("Can't use that");
}
public void SummonAMonster()
{
}
public void TimeStop()
{
throw new Exception("Can't use that");
}
}
class GrandMage : SpecialPower
{
public void FireAttack()
{
}
public void SummonAMonster()
{
}
public void TimeStop()
{
}
}
interface IFireSpell
{
void FireAttack();
}
interface ISummonSpell
{
void SummonAMonster();
}
interface ITimeStopSpell
{
void TimeStop();
}
class FireMage : IFireSpell
{
public void FireAttack()
{
}
}
class Summoner : ISummonSpell
{
public void SummonAMonster()
{
}
}
class GrandMage : IFireSpell, ISummonSpell, ITimeStopSpell
{
public void FireAttack()
{
}
public void SummonAMonster()
{
}
public void TimeStop()
{
}
}
Se debe depender de abstracciones, no depender de implementaciones
class EmailProvider1
{
public void Send()
{
}
}
class EmailProvider2
{
public void Send()
{
}
}
class EmailService
{
public void SendAnEmailUsingEmailProvider1(EmailProvider1 emailProvider)
{
emailProvider.Send();
}
public void SendAnEmailUsingEmailProvider2(EmailProvider2 emailProvider)
{
emailProvider.Send();
}
}
interface IEmailProvider
{
void Send();
}
class EmailProvider1 : IEmailProvider
{
public void Send()
{
}
}
class EmailProvider2 : IEmailProvider
{
public void Send()
{
}
}
class EmailService
{
private readonly IEmailProvider _emailProvider;
public EmailService(IEmailProvider emailProvider)
{
_emailProvider = emailProvider;
}
public void Send()
{
_emailProvider.Send();
}
}