Skip to content

Latest commit

 

History

History
31 lines (27 loc) · 889 Bytes

README.md

File metadata and controls

31 lines (27 loc) · 889 Bytes

pox-proxyer-thingy

A quick "how is this done" thing to understand dynamic proxying.

I wanted to understand how to go from manually writing a wrapper to dynamically creating a wrapper from an interface and a wrapped implementation. So instead of having this:

public class FoodShop {
  Pastry getFood(Pound money) {
      Euro euro = new Euro();
      euro.setValue(money.getValue() * 0.8);
      
      StrudelShop strudelShop = new StrudelShop();
      Strudel strudel = strudelShop.getFood(euro);
      
      Pastry pastry = new Pastry();
      pastry.setName(strudel.getName());
      pastry.setPrice(strudel.getPrice());
      return pastry;
  }
}

to this;

public interface FoodShop {
  Pastry getFood(Pound money);
}

used with:
  FoodShop proxyedFoodStore = proxyer.getInstance(FoodShop.class);
  Pastry pastry = proxyedFoodStore.getFood(new Pound(1.35));