Created
September 10, 2015 15:07
-
-
Save lopezjurip/3bcf0c6158681fdc3b2e to your computer and use it in GitHub Desktop.
[C# Psuedo-código] Actividad para Diseño detallado de Software
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace Namespace { | |
class Bebida { | |
} | |
class Fanta: Bebida { | |
} | |
class Coca: Bebida { | |
} | |
class Sprite: Bebida { | |
} | |
class IncaCola: Bebida { | |
} | |
class DispensadorDeBebida { | |
public Bebida Dispensar(string tipo) { | |
switch (tipo) { | |
case 'Fanta': { | |
return new Fanta(); | |
} | |
case 'Coca': { | |
return new Coca(); | |
} | |
case 'Sprite': { | |
return new Sprite(); | |
} | |
case 'IncaCola': { | |
return new IncaCola() | |
} | |
default: { | |
return null; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solución
switch
y en vez de recibir unstring
para identificar la bebida, simplemente usar Generics para recibir el tipo de la bebida a usar (Fanta
,IncaCola
, etc), y luego usar el constructor de la clase genéricamente. Para esto, también las bebidas deben implementar una interfazIBebida
que permita que todas las bebidas deban implementar cierto método instanciador.DispensadorBebida
para agregar nuevas bebidas, o bien eliminar bebidas del dispensador. Esto tampoco genera problemas puesto que reduce el acoplamiento y mantiene la cohesión anterior del modelo.