Exposiciones

En esta sección se resolverán dudas relacionadas con el lenguaje de programación de Oracle Java

Moderator: julianmartinez16

User avatar
ccpc
Posts: 60
Joined: Fri Jan 22, 2016 8:08 am

Re: Exposiciones

Post by ccpc » Wed Mar 15, 2017 7:17 pm



http://www.slideshare.net/CristianPalac ... -singleton

Code: Select all

public class Main
{
    public static void main(String[] args)
    {
        for(int num=1; num<=5; num++)
        {
            Coche.getInstancia();
        }
    }
}

Code: Select all

public class Coche
{
     private static Coche instancia;
    // ----------------------------------------
    private Coche() {
    }
    // ----------------------------------------
     public static Coche getInstancia()
    {
         if (instancia == null) {
             instancia = new Coche();
            System.out.println("El objeto ha sido creado");
        }
        else {
            System.out.println("Ya existe el objeto");
        }
         return instancia;
    }
}
Nombre: Cristian Camilo Palacio Cuartas.
Materia: Logica de programación.

User avatar
julianreco1
Posts: 46
Joined: Fri Jan 22, 2016 8:09 am

Re: Exposiciones

Post by julianreco1 » Tue Mar 21, 2017 1:47 am


https://es.slideshare.net/julianrestrep ... d-73394892

Interface IServer

Code: Select all

public interface IServer {

	public void apagate();
	public void prendete();
	public void conectate();
	public void verificaConexion();
	public void guardaLog();
	public void cerraConexion();
	
}
Server Medellin

Code: Select all

public class MedellinServer implements IServer {

	public void apagate() {
		System.out.println("apagando el servidor medellin");
	}

	public void cierraConexion() {
		System.out.println("cerrando conexion con el server medellin");
	}

	public void conectate() {
		System.out.println("conectando al server medellin");
	}

	public void guardaLog() {
		System.out.println("Guardar Log medellin");
	}

	public void prendete() {
		System.out.println("prendiendo el server de medellin");
	}

}
Server Bogota

Code: Select all

public class BogotaServer implements IServer {

	public void apagate() {
		System.out.println("apagando el servidor bogota");
	}

	public void cierraConexion() {
		System.out.println("cerrando conexion con el server bogota");
	}

	public void conectate() {
		System.out.println("conectando al server bogota");
	}

	public void guardaLog() {
		System.out.println("Guardar Log bogota");
	}

	public void prendete() {
		System.out.println("prendiendo el server de bogota");
	}

}
Server USA

Code: Select all

public class USAServer implements IServer {

	public void apagate() {
		System.out.println("apagando el servidor USA");
	}

	public void cierraConexion() {
		System.out.println("cerrando conexion con el server USA");
	}

	public void conectate() {
		System.out.println("conectando al server USA");
	}

	public void guardaLog() {
		System.out.println("Guardar Log USA");
	}

	public void prendete() {
		System.out.println("prendiendo el server de USA");
	}

}
Interface Command

Code: Select all

public interface Command {

	public void execute();

}
Apagar server

Code: Select all

public class ApagarServer implements Command {
	private IServer servidor;

	public ApagarServer(IServer servidor) {
		this.servidor = servidor;
	}

	public void execute() {
		servidor.conectate();
		servidor.verificaConexion();
		servidor.guardaLog();
		servidor.apagate();
		servidor.cierraConexion();
	}
}
Prender Server

Code: Select all

public class PrenderServer implements Command {
	private IServer servidor;

	public PrendeServer(IServer servidor) {
		this.servidor = servidor;
	}

	public void execute() {
		servidor.conectate();
		servidor.verificaConexion();
		servidor.guardaLog();
		servidor.prendete();
		servidor.cierraConexion();
	}
}
Reset server

Code: Select all

public class ResetServer implements Command {
	private IServer servidor;

	public ResetServer(IServer servidor) {
		this.servidor = servidor;
	}

	public void execute() {
		servidor.conectate();
		servidor.verificaConexion();
		servidor.guardaLog();
		servidor.prendete();
		servidor.apagate();
		servidor.cierraConexion();
	}
}
Invocador

Code: Select all

public class Invoker {
	private Command command;

	public Invoker(Command command) {
		this.command = command;
	}

	public void run() {
		command.execute();
	}
}
Programa

Code: Select all

public class Programa {

	public static void main(String[] args) {
		
		Command command = new PrendeServer(new MedellinServer());
		Invoker serverAdmin = new Invoker(command);
		serverAdmin.run();
	}

}
Julian Restrepo
Programacion Orientada a Objetos - POO
хотят знать, что Ха-ха привет :?

User avatar
sebasvallejoroldan
Posts: 13
Joined: Tue Jan 17, 2017 9:02 am

Re: Exposiciones

Post by sebasvallejoroldan » Tue Mar 28, 2017 1:44 am

Presentación:

https://slides.com/sebastianvallejorold ... gn-pattern

Ejemplo:

Interfaz Pizza:

Code: Select all

public interface Pizza {

	public String getDesc();
	public double getPrice();
}
Clase Pizza simple vegetariana:

Code: Select all

public class SimplyVegPizza implements Pizza{

	@Override
	public String getDesc() {
		return "SimplyVegPizza (230)";
	}
	@Override
	public double getPrice() {
		return 230;
	}
}
Clase Pizza simple no vegetariana:

Code: Select all

public class SimplyNonVegPizza implements Pizza{
	@Override
	public String getDesc() {
		return "SimplyNonVegPizza (350)";
	}
	@Override
	public double getPrice() {
		return 350;
	}
}
Clase Docorator abstracta que extiende todos los ConcreteDecorator:

Code: Select all

public abstract class PizzaDecorator implements Pizza {
	@Override
	public String getDesc() {
		return "Toppings";
	}
}
Clases ConcreteDecorator:

Clase Broccoli:

Code: Select all

public class Broccoli extends PizzaDecorator{
	private final Pizza pizza;
	public Broccoli(Pizza pizza){
		this.pizza = pizza;
	}
	@Override
	public String getDesc() {
		return pizza.getDesc()+", Broccoli (9.25)";
	}
	@Override
	public double getPrice() {
		return pizza.getPrice()+9.25;
	}
}
Clase Cheese:

Code: Select all

public class Cheese extends PizzaDecorator{
	private final Pizza pizza;
	public Cheese(Pizza pizza){
		this.pizza = pizza;
	}
	@Override
	public String getDesc() {
		return pizza.getDesc()+", Cheese (20.72)";
	}
	@Override
	public double getPrice() {
		return pizza.getPrice()+20.72;
	}
}
Clase Chicken:

Code: Select all

public class Chicken extends PizzaDecorator{
	private final Pizza pizza;
	public Chicken(Pizza pizza){
		this.pizza = pizza;
	}
	@Override
	public String getDesc() {
		return pizza.getDesc()+", Chicken (12.75)";
	}
	@Override
	public double getPrice() {
		return pizza.getPrice()+12.75;
	}
}
Clase FetaCheese:

Code: Select all

	public class FetaCheese extends PizzaDecorator{
	private final Pizza pizza;
	public FetaCheese(Pizza pizza){
		this.pizza = pizza;
	}
	@Override
	public String getDesc() {
		return pizza.getDesc()+", Feta Cheese (25.88)";
	}
	@Override
	public double getPrice() {
		return pizza.getPrice()+25.88;
	}
}
Calse GreenOlives:

Code: Select all

public class GreenOlives extends PizzaDecorator{
	private final Pizza pizza;
	public GreenOlives(Pizza pizza){
		this.pizza = pizza;
	}
	@Override
	public String getDesc() {
		return pizza.getDesc()+", Green Olives (5.47)";
	}
	@Override
	public double getPrice() {
		return pizza.getPrice()+5.47;
	}
}
Calse Ham:

Code: Select all

public class Ham extends PizzaDecorator{
	private final Pizza pizza;
	public Ham(Pizza pizza){
		this.pizza = pizza;
	}
	@Override
	public String getDesc() {
		return pizza.getDesc()+", Ham (18.12)";
	}
	@Override
	public double getPrice() {
		return pizza.getPrice()+18.12;
	}
}
Clase Meat:

Code: Select all

public class Meat extends PizzaDecorator{
	private final Pizza pizza;
	public Meat(Pizza pizza){
		this.pizza = pizza;
	}
	@Override
	public String getDesc() {
		return pizza.getDesc()+", Meat (14.25)";
	}
	@Override
	public double getPrice() {
		return pizza.getPrice()+14.25;
	}
}
Clase RedOnions:

Code: Select all

public class RedOnions extends PizzaDecorator{
	private final Pizza pizza;
	public RedOnions(Pizza pizza){
		this.pizza = pizza;
	}
	@Override
	public String getDesc() {
		return pizza.getDesc()+", Red Onions (3.75)";
	}
	@Override
	public double getPrice() {
		return pizza.getPrice()+3.75;
	}
}
Clase RomaTomtoes:

Code: Select all

public class RomaTomatoes extends PizzaDecorator{
	private final Pizza pizza;
	public RomaTomatoes(Pizza pizza){
		this.pizza = pizza;
	}
	@Override
	public String getDesc() {
		return pizza.getDesc()+", Roma Tomatoes (5.20)";
	}
	@Override
	public double getPrice() {
		return pizza.getPrice()+5.20;
	}
}
Clase Spinach:

Code: Select all

public class Spinach extends PizzaDecorator{
	private final Pizza pizza;
	public Spinach(Pizza pizza){
		this.pizza = pizza;
	}
	@Override
	public String getDesc() {
		return pizza.getDesc()+", Spinach (7.92)";
	}
	@Override
	public double getPrice() {
		return pizza.getPrice()+7.92;
	}
}
Las clases anteriores contienen la referencia al objeto Pizza que se necesita para agregarle el ingrediente. El Decorator cumple su función al ser llamado.

Clase TestDecoratorPattern (Programa):

Code: Select all

import java.text.DecimalFormat;
public class TestDecoratorPattern {
	public static void main(String[] args) {
		DecimalFormat dformat = new DecimalFormat("#.##");
		Pizza pizza = new SimplyVegPizza();
		pizza = new RomaTomatoes(pizza);
		pizza = new GreenOlives(pizza);
		pizza = new Spinach(pizza);
		System.out.println("Desc: "+pizza.getDesc());
		System.out.println("Price: "+dformat.format(pizza.getPrice()));
		pizza = new SimplyNonVegPizza();
		pizza = new Meat(pizza);
		pizza = new Cheese(pizza);
		pizza = new Cheese(pizza);
		pizza = new Ham(pizza);
		System.out.println("Desc: "+pizza.getDesc());
		System.out.println("Price: "+dformat.format(pizza.getPrice()));
	}
}
El output del código anterior será:

Code: Select all

Desc: SimplyVegPizza (230), Roma Tomatoes (5.20), Green Olives (5.47), Spinach (7.92)
Price: 248.59
Desc: SimplyNonVegPizza (350), Meat (14.25), Cheese (20.72), Cheese (20.72), Ham (18.12)
Price: 423.81

Post Reply