Skip to content

Instantly share code, notes, and snippets.

@freekman
Created January 8, 2015 14:24
Show Gist options
  • Save freekman/3b384d901db0d6b28668 to your computer and use it in GitHub Desktop.
Save freekman/3b384d901db0d6b28668 to your computer and use it in GitHub Desktop.
Proxy Example
package com.clouway.patterns.proxytodell;
/**
* @author Ivan Genchev ([email protected])
*/
//Клиента работи с обект от клас А, но реално вижда че работи само с клас Б.
// Реален клас <- ->Proxy
// Integer class <- ->IntegerProxy
// Да се направи IntegerProxy. Има 1 мениджър (IntegerFactory), който създава инстанции чрез метода си createInstance().
// Клиентите работят само с IntegerProxy, а не с реалния Integer.
interface IntegerValue {
int intValue();
}
class RealIntegerValue implements IntegerValue {
private int value;
public RealIntegerValue(int value) {
this.value = value;
}
@Override
public int intValue() {
return value;
}
}
class Proxy implements IntegerValue {
private IntegerValue original;
public Proxy(IntegerValue original) {
this.original = original;
}
@Override
public int intValue() {
return original.intValue() * 2;
}
}
class Client {
private IntegerValue integerValue;
public Client(IntegerValue integerValue) {
this.integerValue = integerValue;
}
public void start() {
System.out.println(integerValue.intValue());
}
}
public class Main {
public static void main(String[] args) {
Proxy proxy = new Proxy(new RealIntegerValue(5));
Client client = new Client(proxy);
client.start();
// ProxyCar proxyCar =new ProxyCar(new BMW("BMW"));
//
// Car proxyBMW2=new ProxyBMW("318");
// proxyBMW2.startEngine();
// proxyBMW2.startEngine();
// proxyBMW2.startEngine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment