The Adapter design pattern (adapter)

  Abstract: Model Adapter (adapter) 

  Definition: 
  Will be two incompatible types used to gather together, which in practice often. 

  Why? 
  We often encountered no relationship to the two categories combined use, the first solution is: to amend the interface in each category, but if we do not have the source code, or, we do not want an application to amend their respective access mouth.    How do? 

  Adapter used in the interface between the two to create a hybrid interface (mixed). 

  How to use it? 
  Implementation Adapter, In fact, the "think in Java," "renewable" in an already mentioned, there are two ways: portfolios (composition) and inheritance (inheritance). 


  We must assume that piling, there are two categories: square piles circular pile. 
  (Public class SquarePeg 
  Public void insert (String str) ( 
  System.out.println ( "SquarePeg insert ():"+ str); 
  ) 

  ) 

  (Public class RoundPeg 
  Public void insertIntohole (String msg) ( 
  System.out.println ( "RoundPeg insertIntoHole ():"+ msg); 
  ) 
  ) 

  Now have an application has been achieved SquarePeg inheritance, but also in achieving RoundPeg insert () action, 

  Public class PegAdapter extends SquarePeg ( 

  Private RoundPeg roundPeg; 

  Public PegAdapter (RoundPeg peg) (this.roundPeg = peg;) 

  Public void insert (String str) (roundPeg.insertIntoHole (str);) 

  ) 

  PegAdapter in, the use of combinations, RoundPeg object, then Heavy insert () method.    See here, if you some Java experience, it was found that frequent use of this model. 

  Above PegAdapter SquarePeg is inherited, if we need on both sides of the succession, inheritance and succession RoundPeg SquarePeg, because Java does not allow multiple inheritance in, but we can achieve (implements) 2 interface (interface) 

  (Public interface IRoundPeg 
  Public void insertIntoHole (String msg); 

  ) 

  (Public interface ISquarePeg 
  Public void insert (String str); 

  ) 

  Below are the new RoundPeg SquarePeg and, in addition to interface to achieve this distinction, and no different from the above. 
  Public class SquarePeg implements IRoundPeg ( 
  Public void insert (String str) ( 
  System.out.println ( "SquarePeg insert ():"+ str); 
  ) 

  ) 

  Public class RoundPeg implements ISquarePeg ( 
  Public void insertIntohole (String msg) ( 
  System.out.println ( "RoundPeg insertIntoHole ():"+ msg); 
  ) 
  ) 

  Below are the new PegAdapter, called the two-way adapter: 

  Public class PegAdapter implements IRoundPeg, ISquarePeg ( 

  Private RoundPeg roundPeg; 
  Private SquarePeg squarePeg; 

  / / Constructor public PegAdapter (RoundPeg peg) = (this.roundPeg peg;) 
  / / Constructor public PegAdapter (SquarePeg peg) (this.squarePeg = peg;) 

  Public void insert (String str) (roundPeg.insertIntoHole (str);) 

  ) 

  There is also a call Pluggable Adapters, can dynamically access in a few adapters.    Reflection use technology, the dynamic can be found in the category of Public method. 




  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply