Created
September 23, 2015 01:49
-
-
Save Charlyzzz/24b92b466f0b78d47739 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
// Tengo que asignarle los valores a los atributos del builder, y cuando digo | |
// ejecutar o contruir o build me devuelve una instancia con los valores que le puse | |
val builderDePerros = new PerroBuilder | |
builderDePerros.raza = "Perro Trompeta" | |
builderDePerros.edad = 2 | |
val nuevoPerro = builderDePerros.build | |
println(nuevoPerro) |
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
@Accessors | |
class Perro { | |
String raza | |
int edad | |
String duenio | |
} |
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
@Accessors | |
class PerroBuilder { | |
// Pueden ser de clase o de instancia | |
String raza | |
int edad | |
def Perro build() { | |
var perro = new Perro | |
perro.raza = raza | |
perro.edad = edad | |
perro.duenio = "Cesar Milán" | |
perro | |
} | |
} |
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
class FactoryPerro { | |
def static Perro nuevoFoxTerrier(int edad2) { | |
var perro = new Perro | |
perro.duenio = "Cesar Milán" | |
perro.edad = edad2 | |
perro.raza = "Fox Terrier" | |
perro | |
} | |
def static Perro nuevoLabrador(int edad2) { | |
var perro = new Perro | |
perro.duenio = "Cesar Milán" | |
perro.edad = edad2 | |
perro.raza = "Labrador" | |
perro | |
} | |
} |
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
// Puedo tener muchos métodos que me devuelvan instancias con distintos | |
// valores que me sirvan. Ej: perro alemán, perro salchicha. | |
class Programa { | |
def static void main(String[] args) { | |
var perro = FactoryPerro.nuevoFoxTerrier(2) | |
var perro2 = FactoryPerro.nuevoLabrador(10) | |
println(perro) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment