Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Design Patterns - Proxy & Composite

Design Patterns - Proxy & Composite

Explains foundations of proxy and composite patterns.

Sarath C

June 19, 2014
Tweet

More Decks by Sarath C

Other Decks in Programming

Transcript

  1. Structural design patterns ease the design by identifying a simple

    way to realize relationships between entities
  2. Compose objects into tree structures to represent part-whole hierarchies. !

    describes that a group of objects are to be treated in the same way as a single instance of an object
  3. Leaf (primitive element) • represents leaf objects in the composition.

    • A leaf has no children. • defines behavior for primitive objects in the composition
  4. Composite • defines behavior for components having children. • stores

    child components. • implements child-related operations in the Component interface.
  5. class CompositeGraphic implements Graphic { //Collection of child graphics. private

    List<Graphic> childGraphics = new ArrayList<Graphic>();
  6. class CompositeGraphic implements Graphic { //Collection of child graphics. private

    List<Graphic> childGraphics = new ArrayList<Graphic>(); ! //Prints the graphic. public void print() { for (Graphic graphic : childGraphics) { graphic.print(); } }
  7. class CompositeGraphic implements Graphic { //Collection of child graphics. private

    List<Graphic> childGraphics = new ArrayList<Graphic>(); ! //Prints the graphic. public void print() { for (Graphic graphic : childGraphics) { graphic.print(); } } ! //Adds the graphic to the composition. public void add(Graphic graphic) { childGraphics.add(graphic); } //Removes the graphic from the composition. public void remove(Graphic graphic) { childGraphics.remove(graphic); } }
  8. /** "Leaf" */ class Ellipse implements Graphic { //Prints the

    graphic. public void print() { System.out.println("Ellipse"); } }
  9. /** Client */ public class Program { public static void

    main(String[] args) { //Initialize four ellipses Ellipse ellipse1 = new Ellipse(); Ellipse ellipse2 = new Ellipse(); Ellipse ellipse3 = new Ellipse(); Ellipse ellipse4 = new Ellipse();
  10. /** Client */ public class Program { public static void

    main(String[] args) { //Initialize four ellipses Ellipse ellipse1 = new Ellipse(); Ellipse ellipse2 = new Ellipse(); Ellipse ellipse3 = new Ellipse(); Ellipse ellipse4 = new Ellipse(); ! //Initialize three composite graphics CompositeGraphic graphic = new CompositeGraphic(); CompositeGraphic graphic1 = new CompositeGraphic(); CompositeGraphic graphic2 = new CompositeGraphic();
  11. /** Client */ public class Program { public static void

    main(String[] args) { //Initialize four ellipses Ellipse ellipse1 = new Ellipse(); Ellipse ellipse2 = new Ellipse(); Ellipse ellipse3 = new Ellipse(); Ellipse ellipse4 = new Ellipse(); ! //Initialize three composite graphics CompositeGraphic graphic = new CompositeGraphic(); CompositeGraphic graphic1 = new CompositeGraphic(); CompositeGraphic graphic2 = new CompositeGraphic(); ! //Composes the graphics graphic1.add(ellipse1); graphic1.add(ellipse2); graphic1.add(ellipse3); graphic2.add(ellipse4);
  12. /** Client */ public class Program { public static void

    main(String[] args) { //Initialize four ellipses … ! //Initialize three composite graphics … ! //Composes the graphics graphic1.add(ellipse1); graphic1.add(ellipse2); graphic1.add(ellipse3); graphic2.add(ellipse4); graphic.add(graphic1); graphic.add(graphic2); ! graphic.add(graphic1); graphic.add(graphic2);
  13. /** Client */ public class Program { public static void

    main(String[] args) { //Initialize four ellipses … ! //Initialize three composite graphics … ! //Composes the graphics … … ! … ! //Prints the complete graphic (4 times "Ellipse"). graphic.print(); }
  14. it makes the clients of a component communicate with a

    representative rather than to the component Itself.
  15. Context • A client needs access to the services of

    another component • Direct access is technically possible, but may not be the best approach
  16. Problem • Often it’s inappropriate to access the component directly

    • Direct and unrestricted access can be insecure and inefficient
  17. Provide synchronized access to services in a multi-access or multi-

    threaded environment Synchronization Proxy