در مهندسی نرم افزار یک رابط فصیح (به انگلیسی: Fluent interface) (که توسط اریک ایوانز و مارتین فاولر) راهی است برای پیاده سازی رابط برنامه نویسی (به انگلیسی: Application Programming Interface یا API) که کدهایی با خوانایی بهتر را پشتیبانی کند.
unit FluentInterface;interfacetype IConfiguration = interface procedure SetColor(Color: string); procedure SetHeight(height: integer); procedure SetLength(length: integer); procedure SetDepth(depth: integer); end; IConfigurationFluent = interface function SetColor(Color: string): IConfigurationFluent; function SetHeight(height: integer): IConfigurationFluent; function SetLength(length: integer): IConfigurationFluent; function SetDepth(depth: integer): IConfigurationFluent; end; TConfiguration = class(TInterfacedObject, IConfiguration) private FColor: string; FHeight: integer; FLength: integer; FDepth: integer; protected procedure SetColor(Color: string); procedure SetHeight(height: integer); procedure SetLength(length: integer); procedure SetDepth(depth: integer); end; TConfigurationFluent = class(TInterfacedObject, IConfigurationFluent) private FColor: string; FHeight: integer; FLength: integer; FDepth: integer; protected function SetColor(Color: string): IConfigurationFluent; function SetHeight(height: integer): IConfigurationFluent; function SetLength(length: integer): IConfigurationFluent; function SetDepth(depth: integer): IConfigurationFluent; public class function New: IConfigurationFluent; end;implementationprocedure TConfiguration.SetColor(Color: string);begin FColor := Color;end;procedure TConfiguration.SetDepth(depth: integer);begin FDepth := depth;end;procedure TConfiguration.SetHeight(height: integer);begin FHeight := height;end;procedure TConfiguration.SetLength(length: integer);begin FLength := length;end;class function TConfigurationFluent.New: IConfigurationFluent;begin Result := Create;end;function TConfigurationFluent.SetColor(Color: string): IConfigurationFluent;begin FColor := Color; Result := Self;end;function TConfigurationFluent.SetDepth(depth: integer): IConfigurationFluent;begin FDepth := depth; Result := Self;end;function TConfigurationFluent.SetHeight(height: integer): IConfigurationFluent;begin FHeight := height; Result := Self;end;function TConfigurationFluent.SetLength(length: integer): IConfigurationFluent;begin FLength := length; Result := Self;end;end.Basic Usage:
GridBagLayout gl = new GridBagLayout(); JPanel p = new JPanel(); p.setLayout( gl ); JLabel l = new JLabel("Name:"); JTextField nm = new JTextField(10); GridBagConstraints gc = new GridBagConstraints(); gc.gridx = 0; gc.gridy = 0; gc.fill = GridBagConstraints.NONE; p.add( l, gc ); gc.gridx = 1; gc.fill = GridBagConstraints.HORIZONTAL; gc.weightx = 1; p.add( nm, gc );This creates a lot of code and makes it difficult to see what exactly is happening here. The Packer class, visible at http://packer.dev.java.net, provides a Fluent mechanism for using this class so that you would instead write:
unit FluentInterface;interfacetype IConfiguration = interface procedure SetColor(Color: string); procedure SetHeight(height: integer); procedure SetLength(length: integer); procedure SetDepth(depth: integer); end; IConfigurationFluent = interface function SetColor(Color: string): IConfigurationFluent; function SetHeight(height: integer): IConfigurationFluent; function SetLength(length: integer): IConfigurationFluent; function SetDepth(depth: integer): IConfigurationFluent; end; TConfiguration = class(TInterfacedObject, IConfiguration) private FColor: string; FHeight: integer; FLength: integer; FDepth: integer; protected procedure SetColor(Color: string); procedure SetHeight(height: integer); procedure SetLength(length: integer); procedure SetDepth(depth: integer); end; TConfigurationFluent = class(TInterfacedObject, IConfigurationFluent) private FColor: string; FHeight: integer; FLength: integer; FDepth: integer; protected function SetColor(Color: string): IConfigurationFluent; function SetHeight(height: integer): IConfigurationFluent; function SetLength(length: integer): IConfigurationFluent; function SetDepth(depth: integer): IConfigurationFluent; public class function New: IConfigurationFluent; end;implementationprocedure TConfiguration.SetColor(Color: string);begin FColor := Color;end;procedure TConfiguration.SetDepth(depth: integer);begin FDepth := depth;end;procedure TConfiguration.SetHeight(height: integer);begin FHeight := height;end;procedure TConfiguration.SetLength(length: integer);begin FLength := length;end;class function TConfigurationFluent.New: IConfigurationFluent;begin Result := Create;end;function TConfigurationFluent.SetColor(Color: string): IConfigurationFluent;begin FColor := Color; Result := Self;end;function TConfigurationFluent.SetDepth(depth: integer): IConfigurationFluent;begin FDepth := depth; Result := Self;end;function TConfigurationFluent.SetHeight(height: integer): IConfigurationFluent;begin FHeight := height; Result := Self;end;function TConfigurationFluent.SetLength(length: integer): IConfigurationFluent;begin FLength := length; Result := Self;end;end.Basic Usage:
GridBagLayout gl = new GridBagLayout(); JPanel p = new JPanel(); p.setLayout( gl ); JLabel l = new JLabel("Name:"); JTextField nm = new JTextField(10); GridBagConstraints gc = new GridBagConstraints(); gc.gridx = 0; gc.gridy = 0; gc.fill = GridBagConstraints.NONE; p.add( l, gc ); gc.gridx = 1; gc.fill = GridBagConstraints.HORIZONTAL; gc.weightx = 1; p.add( nm, gc );This creates a lot of code and makes it difficult to see what exactly is happening here. The Packer class, visible at http://packer.dev.java.net, provides a Fluent mechanism for using this class so that you would instead write:
wiki: رابط فلوئنت