He creado un repositorio con ésta primera parte en Github para que lo descarguen si les interesa: Java design patterns
Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.
Mostrar Mensajes Menúpackage net.elhacker.adapter;
public interface Car {
void fillTank();
default void start() {
System.out.println("Encendiendo auto...");
}
}
package net.elhacker.adapter;
public class GasolineCar implements Car {
public GasolineCar() {
super(); // llamada al constructor padre
System.out.println("Creando un auto a gasolina...");
}
@Override
public void fillTank() {
System.out.println("Colocando gasolina...");
}
}
package net.elhacker.adapter;
public class GasCar implements Car {
public GasCar() {
super(); // llamada al constructor padre
System.out.println("Creando un auto a gas...");
}
@Override
public void fillTank() {
System.out.println("Colocando gas...");
}
}
package net.elhacker.adapter;
public class ElectricCar {
public ElectricCar() {
super(); // llamada al constructor padre
System.out.println("Creando un auto eléctrico...");
}
public void connect() {
System.out.println("Conectando motor a generador de electricidad...");
}
}
Citar
Las entidades de software deben ser abiertas para ser extendidas y cerradas para no ser modificadas.
package net.elhacker.adapter;
public class ElectricCarAdapter implements Car {
ElectricCar electricCar;
public ElectricCarAdapter() {
electricCar = new ElectricCar();
}
@Override
public void fillTank() {
electricCar.connect();
}
}
package net.elhacker.adapter;
public class AdapterTest {
public static void main(String[] args) {
Car gasolineCar = new GasolineCar();
gasolineCar.fillTank();
gasolineCar.start();
System.out.println();
Car gasCar = new GasCar();
gasCar.fillTank();
gasCar.start();
System.out.println();
Car electricCar = new ElectricCarAdapter();
electricCar.fillTank();
electricCar.start();
}
}
Creando un auto a gasolina...
Colocando gasolina...
Encendiendo auto...
Creando un auto a gas...
Colocando gas...
Encendiendo auto...
Creando un auto eléctrico...
Conectando motor a generador de electricidad...
Encendiendo auto...
package net.elhacker.facade;
public class Bank {
public Bank() {
}
public void createAccount(String account) {
System.out.println("Creando cuenta N° "+account);
}
}
package net.elhacker.facade;
public class Deposit {
public Deposit() {
}
public void makeDeposit(double amount, String account) {
System.out.println("Se ha depositado $"+amount+" a la cuenta "+account);
}
}
package net.elhacker.facade;
public class Withdrawal {
public Withdrawal() {
}
public void makeWidthdrawal(double amount, String account) {
System.out.println("Se ha retirado $"+amount+" de la cuenta "+account);
}
}
package net.elhacker.facade;
public class Facade {
public static void main(String[] args) {
Bank bank = new Bank();
Deposit deposit = new Deposit();
Withdrawal withdrawal = new Withdrawal();
bank.createAccount("9343435093");
deposit.makeDeposit(2599.90, "9343435093");
withdrawal.makeWidthdrawal(699.90, "9343435093");
}
}
package net.elhacker.facade;
public class OperationsFacade {
public OperationsFacade() {
}
public void createAccount(String account) {
new Bank().createAccount(account);
}
public void makeDeposit(double amount, String account) {
new Deposit().makeDeposit(amount, account);
}
public void makeWithdrawal(double amount, String account) {
new Withdrawal().makeWidthdrawal(amount, account);
}
}
package net.elhacker.facade;
public class FacadeTest {
public static void main(String[] args) {
OperationsFacade facade = new OperationsFacade();
facade.createAccount("9343435093");
facade.makeDeposit(2599.90, "9343435093");
facade.makeWithdrawal(699.90, "9343435093");
}
}
Creando cuenta N° 9343435093
Se ha depositado $2599.9 a la cuenta 9343435093
Se ha retirado $699.9 de la cuenta 9343435093
package net.elhacker.factory;
public abstract class Animal {
protected String type;
protected String family;
protected String habitat;
public Animal(String type, String family, String habitat) {
this.type = type;
this.family = family;
this.habitat = habitat;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getFamily() {
return family;
}
public void setFamily(String family) {
this.family = family;
}
public String getHabitat() {
return habitat;
}
public void setHabitat(String habitat) {
this.habitat = habitat;
}
@Override
public String toString() {
return "Tipo de animal: "+getType()+"\nFamilia: "+getFamily()+
"\nHábitat: "+getHabitat();
}
}
package net.elhacker.factory;
public class Dog extends Animal {
public Dog(String type, String family, String habitat) {
super(type, family, habitat);
}
}
package net.elhacker.factory;
public class Shark extends Animal {
public Shark(String type, String family, String habitat) {
super(type, family, habitat);
}
}
package net.elhacker.factory;
public class Lion extends Animal {
public Lion(String type, String family, String habitat) {
super(type, family, habitat);
}
}
package net.elhacker.factory;
public interface AbstractAnimalFactory {
Animal createAnimal();
}
package net.elhacker.factory;
public class DogFactory implements AbstractAnimalFactory {
protected String type;
protected String family;
protected String habitat;
public DogFactory(String type, String family, String habitat) {
this.type = type;
this.family = family;
this.habitat = habitat;
}
@Override
public Animal createAnimal() {
return new Dog(type, family, habitat);
}
}
package net.elhacker.factory;
public class SharkFactory implements AbstractAnimalFactory {
protected String type;
protected String family;
protected String habitat;
public SharkFactory(String type, String family, String habitat) {
this.type = type;
this.family = family;
this.habitat = habitat;
}
@Override
public Animal createAnimal() {
return new Shark(type, family, habitat);
}
}
package net.elhacker.factory;
public class LionFactory implements AbstractAnimalFactory {
protected String type;
protected String family;
protected String habitat;
public LionFactory(String type, String family, String habitat) {
this.type = type;
this.family = family;
this.habitat = habitat;
}
@Override
public Animal createAnimal() {
return new Lion(type, family, habitat);
}
}
public LionFactory(String type, String family, String habitat)
@Override
public Animal createAnimal() {
return new Lion(type, family, habitat);
}
package net.elhacker.factory;
public abstract class AnimalFactory {
public static Animal create(AbstractAnimalFactory factory) {
return factory.createAnimal();
}
}
package net.elhacker.factory;
public class FactoryTest {
public static void main(String[] args) {
Animal dog = AnimalFactory.create(new DogFactory("Perro","Caninos","Doméstico"));
Animal shark = AnimalFactory.create(new SharkFactory("Tiburón", "Lámnidos", "Mar"));
Animal lion = AnimalFactory.create(new LionFactory("León", "Felinos", "Selva"));
System.out.println(dog.toString());
System.out.println();
System.out.println(shark.toString());
System.out.println();
System.out.println(lion.toString());
}
}
Tipo de animal: Perro
Familia: Caninos
Hábitat: Doméstico
Tipo de animal: Tiburón
Familia: Lámnidos
Hábitat: Mar
Tipo de animal: León
Familia: Felinos
Hábitat: Selva
package net.elhacker.singleton;
import java.util.HashMap;
import java.util.Map;
public class Configuration {
private Map<String,Object> appOptions = null;
private static Configuration config;
private Configuration() {
}
public static Configuration getConfiguration() {
if(config == null) {
config = new Configuration();
}
return config;
}
public Map<String,Object> getAppOptions() {
if(appOptions == null) {
appOptions = new HashMap<>();
appOptions.put("theme", "dark");
appOptions.put("show_hidde_files", true);
}
return appOptions;
}
public void setAppOptions(Map<String,Object> appOptions) {
this.appOptions = appOptions;
}
}
package net.elhacker.singleton;
import java.util.Map;
public class SingletonTest {
public static void main(String[] args) {
Configuration cfg = Configuration.getConfiguration();
// recorre el hashmap para leer las claves y valores
for(Map.Entry<String, Object> entry: cfg.getAppOptions().entrySet()) {
System.out.println(entry.getKey()+": "+entry.getValue());
}
}
}
show_hidde_files: true
theme: dark
Cita de: Oblivi0n en 30 Enero 2015, 13:49 PM
C# y Python, Java es un horror una vez lo conoces bien y javascript simplementes me parece infame
select
(select count(rev_state) from revisions where rev_state = 'inapropiado') as 'Mensajes inapropiados',
(select count(message_state) from revisions where message_state = 'enviado') as 'Mensajes enviados'
SELECT COUNT(r.id_revision) AS 'Revisiones inapropiadas',
COUNT(m.id_mensaje) AS 'Mensajes enviados'
FROM Revision r, Mensaje m
WHERE r.revision = 'Inapropiado' AND m.mensaje = 'enviado';