Design Pattern----07.Structural.Adapter.Pattern (Delphi Sample)

Intent

  • Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
  • Wrap an existing class with a new interface.
  • Impedance match an old component to a new system

Problem

An “off the shelf” component offers compelling functionality that you would like to reuse, but its “view of the world” is not compatible with the philosophy and architecture of the system currently being developed.

Discussion

Reuse has always been painful and elusive. One reason has been the tribulation of designing something new, while reusing something old. There is always something not quite right between the old and the new. It may be physical dimensions or misalignment. It may be timing or synchronization. It may be unfortunate assumptions or competing standards.

It is like the problem of inserting a new three-prong electrical plug in an old two-prong wall outlet – some kind of adapter or intermediary is necessary.

Adapter is about creating an intermediary abstraction that translates, or maps, the old component to the new system. Clients call methods on the Adapter object which redirects them into calls to the legacy component. This strategy can be implemented either with inheritance or with aggregation.

Adapter functions as a wrapper or modifier of an existing class. It provides a different or translated view of that class.

Structure

Below, a legacy Rectangle component’s display() method expects to receive “x, y, w, h” parameters. But the client wants to pass “upper left x and y” and “lower right x and y”. This incongruity can be reconciled by adding an additional level of indirection – i.e. an Adapter object.

The Adapter could also be thought of as a “wrapper”.

Example

The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients. Socket wrenches provide an example of the Adapter. A socket attaches to a ratchet, provided that the size of the drive is the same. Typical drive sizes in the United States are 1/2” and 1/4”. Obviously, a 1/2” drive ratchet will not fit into a 1/4” drive socket unless an adapter is used. A 1/2” to 1/4” adapter has a 1/2” female connection to fit on the 1/2” drive ratchet, and a 1/4” male connection to fit in the 1/4” drive socket.

Check list

  1. Identify the players: the component(s) that want to be accommodated (i.e. the client), and the component that needs to adapt (i.e. the adaptee).
  2. Identify the interface that the client requires.
  3. Design a “wrapper” class that can “impedance match” the adaptee to the client.
  4. The adapter/wrapper class “has a” instance of the adaptee class.
  5. The adapter/wrapper class “maps” the client interface to the adaptee interface.
  6. The client uses (is coupled to) the new interface

Rules of thumb

  • Adapter makes things work after they’re designed; Bridge makes them work before they are.
  • Bridge is designed up-front to let the abstraction and the implementation vary independently. Adapter is retrofitted to make unrelated classes work together.
  • Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.
  • Adapter is meant to change the interface of an existing object. Decorator enhances another object without changing its interface. Decorator is thus more transparent to the application than an adapter is. As a consequence, Decorator supports recursive composition, which isn’t possible with pure Adapters.
  • Facade defines a new interface, whereas Adapter reuses an old interface. Remember that Adapter makes two existing interfaces work together as opposed to defining an entirely new one.

delphi Sample

UML(Class Diagram)

image

source

----------------------------------------------------------------------------------------------------------------------------------

  1: unit Adapter; 

  2: 

  3: interface 

  4: 

  5: uses SysUtils, Classes; 

  6: 

  7: type 

  8:   //The old class 

  9: 

 10:   TOldDOB = record 

 11:     Day: 0..31; 

 12:     Month: 1..12; 

 13:     Year: 0..99; 

 14:   end; 

 15: 

 16:   TOldCustomer = class 

 17:     FCustomerID: Integer; 

 18:     FName: string; 

 19:     FDOB: TOldDOB; 

 20:   public 

 21:     constructor Create(CustID: Integer); 

 22:     property CustomerID: Integer read FCustomerID; 

 23:     property Name: string read FName; 

 24:     property DOB: TOldDOB read FDOB; 

 25:   end; 

 26: 

 27:   //The new class 

 28:   TNewCustomer = class 

 29:   private 

 30:     FCustomerID: Longint; 

 31:     FFirstName: string; 

 32:     FLastName: string; 

 33:     FDOB: TDateTime; 

 34:   protected 

 35:     function GetCustomerID: Longint; virtual; 

 36:     function GetFirstName: string; virtual; 

 37:     function GetLastName: string; virtual; 

 38:     function GetDOB: TDateTime; virtual; 

 39:   public 

 40:     constructor Create(CustID: Longint); virtual; 

 41:     property CustomerID: Longint read GetCustomerID; 

 42:     property FirstName: string read GetFirstName; 

 43:     property LastName: string read GetLastName; 

 44:     property DOB: TDateTime read GetDOB; 

 45:   end; 

 46: 

 47:   //The Adapter class 

 48:   TAdaptedCustomer = class(TNewCustomer) 

 49:   private 

 50:     FOldCustomer: TOldCustomer; 

 51:   protected 

 52:     function GetCustomerID: Longint; override; 

 53:     function GetFirstName: string; override; 

 54:     function GetLastName: string; override; 

 55:     function GetDOB: TDateTime; override; 

 56:   public 

 57:     constructor Create(CustID: Longint); override; 

 58:     destructor Destroy; override; 

 59:   end; 

 60: 

 61:   //An interface method 

 62: 

 63:   //Lets us hide details of TOldCustomer from the client 

 64: 

 65: function GetCustomer(CustomerID: Longint): TNewCustomer; 

 66: 

 67: implementation 

 68: 

 69: const 

 70:   Last_OldCustomer_At_Year_2000 = 15722; 

 71:   Last_OldCustomer_In_Database = 30000; 

 72: 

 73: //The new class 

 74: 

 75: constructor TNewCustomer.Create(CustID: Longint); 

 76: begin 

 77:   FCustomerID := CustID; 

 78:   FFirstName := 'A'; 

 79:   FLastName := 'New_Customer'; 

 80:   FDOB := Now; 

 81: end; 

 82: 

 83: function TNewCustomer.GetCustomerID: Longint; 

 84: begin 

 85:   Result := FCustomerID; 

 86: end; 

 87: 

 88: function TNewCustomer.GetFirstName: string; 

 89: begin 

 90:   Result := FFirstName; 

 91: end; 

 92: 

 93: function TNewCustomer.GetLastName: string; 

 94: begin 

 95:   Result := FLastName; 

 96: end; 

 97: 

 98: function TNewCustomer.GetDOB: TDateTime; 

 99: begin 

100:   Result := FDOB; 

101: end; 

102: 

103: constructor TOldCustomer.Create(CustID: Integer); 

104: begin 

105:   FCustomerID := CustomerID; 

106:   FName := 'An Old_Customer'; 

107:   with FDOB do begin 

108:     Day := 1; 

109:     Month := 1; 

110:     Year := 1; 

111:   end; 

112: end; 

113: 

114: //The Adapter class 

115: 

116: constructor TAdaptedCustomer.Create(CustID: Longint); 

117: begin 

118:   inherited Create(CustID); 

119:   FOldCustomer := TOldCustomer.Create(CustID); 

120: end; 

121: 

122: destructor TAdaptedCustomer.Destroy; 

123: begin 

124:   FOldCustomer.Free; 

125:   inherited Destroy; 

126: end; 

127: 

128: function TAdaptedCustomer.GetCustomerID: Longint; 

129: begin 

130:   Result := FOldCustomer.CustomerID; 

131: end; 

132: 

133: function TAdaptedCustomer.GetFirstName: string; 

134: var 

135:   SpacePos: integer; 

136: begin 

137:   SpacePos := Pos(' ', FOldCustomer.Name); 

138:   if SpacePos = 0 then 

139:     Result := '' 

140:   else 

141:     Result := Copy(FOldCustomer.Name,1,SpacePos-1); 

142: end; 

143: 

144: function TAdaptedCustomer.GetLastName: string; 

145: var 

146:   SpacePos: integer; 

147: begin 

148:   SpacePos := Pos(' ', FOldCustomer.Name); 

149:   if SpacePos = 0 then 

150:     Result := FOldCustomer.Name 

151:   else 

152:     Result := Copy(FOldCustomer.Name,SpacePos+1,255); 

153: end; 

154: 

155: function TAdaptedCustomer.GetDOB: TDateTime; 

156: var 

157:   FullYear: Word; 

158: begin 

159:   if CustomerID > Last_OldCustomer_At_Year_2000 then 

160:     FullYear := 2000 + FOldCustomer.DOB.Year 

161:   else 

162:     FullYear := 1900 + FOldCustomer.DOB.Year; 

163:   Result := EncodeDate(FullYear, FOldCustomer.DOB.Month, FOldCustomer.DOB.Day); 

164: end; 

165: 

166: function GetCustomer(CustomerID: Longint): TNewCustomer; 

167: begin 

168:   if CustomerID > Last_OldCustomer_In_Database then 

169:     Result := TNewCustomer.Create(CustomerID) 

170:   else 

171:     Result := TAdaptedCustomer.Create(CustomerID) as TNewCustomer; 

172: end; 

173: 

174: end.

175: 

你可能感兴趣的:(design pattern)