Free interfaces and leaky abstractions

What is a free interface? I cannot find a good definition of this, but all I get are examples of long code in a language that I am not very familiar with (for example, C ++).

Also, what is fuzzy abstraction?

thank

+13
oop fluent-interface leaky-abstraction
Jan 11 '09 at 19:11
source share
9 answers

A free interface is an API that allows you to write code that reads more or less like plain English. For example:

Find.All.Questions(Where.IsAnswered == true); 

The chaining method is usually used as part of an implementation, but there is more to it than that. To quote Fowler :

I also noticed a common misconception - many people seem to equate smooth interfaces using the Chaining method. Of course, chaining is a common technique for use with smooth interfaces, but true fluency is much greater.

It is also often called internal DSL , because the syntax resembles DSL, but it is implemented inside the main language instead of being processed by the parser.

+24
Jan 11 '09 at 19:57
source share

Flowing abstraction is an abstraction where the details of the underlying reality often โ€œleak outโ€.

All abstractions are more or less, but sometimes abstractions are so poorly suited to the underlying reality that they do more harm than it helps.

A simple example of a โ€œleakโ€ in an abstraction might be a regular float. It seems to represent common real numbers, and you can use it to perform basic calculations. But at some point you come across a scenario where 1/3 * 3! = 1 or 1 + 10 ^ -20 = 1. That is, when the actual implementation data leaks out and the abstraction breaks.

+17
Jan 11 '09 at 20:22
source share

The free interface is Eric Evans, and it's just another name for the chain of methods. Martin Fowler has written a couple of articles on this subject, but it looks something like this:

 m_Window = window::with() .width(l_Width) .height(l_Height) .title("default window") .left(200) .top(200) .create(); 

A free interface is usually used to create named parameters in a language that does not support them (for example, an idiom of a named parameter in C ++) or in domain languages โ€‹โ€‹to make the code smoother.

I saw how they were used for everything from image processing libraries, regular expression libraries, three-dimensional libraries. Other examples include building tree structures, lists, or other data structures. Anything that requires building complex objects (loading parameters) can use Fluent Interfaces to make it more readable. For example, compare the previous example with a call to the CreateWindow function:

  ::CreateWindow( "Window class", "Window title", dwStyle, X, Y, nWidth, nHeight, hWndPant, hMenu, hInstance, NULL ); 
+9
Jan 11 '09 at 19:24
source share

Here's the usual daily interface:

 public interface NotFluent { void DoA(); void DoB(); void DoC(); } 

And here is the free interface:

 public interface Fluent { Fluent DoA(); Fluent DoB(); Fluent DoC(); } 

The most obvious difference is that when we return the void, we instead return an instance of the interface type. It is understood that the returned interface is a CURRENT INSTANCE, not a new instance of the same type. Of course, this is not enforceable, and in the case of immutable objects (for example, a string) it is a different instance, but can be considered as the same instance, only updated.

Here are examples of their use:

 NotFluent foo = new NotFluentImpl(); foo.DoA(); foo.DoB(); foo.DoC(); Fluent bar = new FluentImpl(); bar.DoA().DoB().DoC(); 

Please note that a free interface is easier to use when connecting different calls. IRL, check out the Linq extension methods and how each call is designed to flow into another. None of the methods return void, even if it will be a valid result.

+5
Jan 11 '09 at 19:27
source share

The object-oriented interface is free if the methods executed to return side effects are self , so that such methods can be connected together.

In 1990, I first came across smooth interfaces when the Modula-3 Interface Police (I don't do this) demanded that all initialization methods return an initialized object. I believe that this usage precedes the term "free interface" coin.

+3
Jan 11 '09 at 20:14
source share

In the free interface, object methods return a reference to the object, so that you can combine method calls.

For example, in NValidate, I did this to simplify parameter checking:

 public City GetCity(string zipCode) { zipCode.Assert("zipCode").IsNotNullOrEmpty().HasLength(5,10).Matches("\\d[5]-\\d[4]"); // Continue processing } 

I can't talk with leaking abstractions.

+1
Jan 11 '09 at 19:28
source share

Thanks guys.

Great description.

My thought is about runaway interfaces, where they were for readability. I could always read the method chain and how one of them relates to the previous / next method.

eg. like a poster that posted an example of verification (I already wrote code similar to the previous one).

+1
Jan 11 '09 at 21:41
source share

Neal Ford explains and gives examples of Fluent Interface in his book Productive Programmer.

Traditional object or "bean" with getters / setters:

 Car car = new CarImpl(); MarketingDescription des = new MarketingDescriptionImpl(); desc.setType("Box"); desc.setSubtype("Insulated"); desc.setAttribute("length", "50.5"); desc.setAttribute("ladder", "yes"); desc.setAttribute("lining type", "cork"); car.setDescription(desc); 

Meet the same need for a free interface:

 Car car = Car.describedAs() .box() .length(50.5) .type(Type.INSULATED) .includes(Equipment.LADDER) .lining(Lining.CORK); 
+1
Jul 07 '09 at 1:57
source share

You can find a good definition and basic concepts of the free interface of this post:

Recommendations for a free interface design in C # 1

I hope this helps.

0
Aug 05 '10 at 1:17
source share



All Articles