views:

234

answers:

3

Hello!

Sadly, I can't remember where I read it, but...
...in C++ you can derive a class from a template parameter. Im pretty sure it was called
Feature Oriented Programming (FOP) and meant to be somehow useful.

It was something like:

template <class T>
class my_class : T {
  // some very useful stuff goes here ;) 
}

My questions about this:

  1. What is the sense of such pattern?

  2. Since this it not possible in Java / C#, how this pattern is achieved in these languages?

  3. Can it be expected to be implemented in Java / C# one day?
    (Well, first Java would need to get rid of type erasure)

EDIT:
I'm really not talking about generics in Java / C#
(where you can't derive a class from a generic type parameter)

+2  A: 

So, the place I see that pattern most in c++ is the behavior of mixins (link), which I guess is an implementation of FOP. The article I've linked goes into an example of Aspect Oriented Programming to try to make a similar effect in java.

I doubt you'll see features like templates (which are essential to the mixin approach) in other languages, though they could develop better patterns for AOP. Personally, I think the easiest method I've seen for this is languages like python and ruby, which allow manipulation of the interface, but it's a runtime mechanism to c++'s compile time metaprogramming facilities, so it is like comparing apples and oranges.

Todd Gardner
A: 

I've seen this pattern before, but never knew it as Feature Oriented Programming. I looked up FOP here: http://wwwiti.cs.uni-magdeburg.de/iti_db/forschung/fop/featurec/ and it doesn't look similar.

The pattern I know that does something very much like your describing is called Policy Based Design. It is thoroughly discussed in Modern C++ Design by Andrei Alexandrescu. The previous poster mentioned Aspect Oriented Programming, which I think Policy Based Design is a small subset of (basically it's 1-dimentional AOP instead of N-dimentional).

I don't imagine Policy Based Design will be implemented in Java or C#, but AOP kind of has been for Java with AspectJ http://www.eclipse.org/aspectj/. It looks like there have been some attempts with C# but I didn't notice anything worth mentioning.

coombez
A: 

AHEAD is a methodology for doing feature-oriented programming, and there is a java composer. There is also a Eclipse IDE for doing fop applications. You can even choose AHEAD composer (java) or featurec++ for c++. All these are implemented as source-to-source compilers

http://wwwiti.cs.uni-magdeburg.de/iti_db/research/featureide/

the meaning of this approach is that you implement features as layers, each layer is exactly one feature. So in this layer you keep code that refines some of the code in lower layers (layers/features are ordered) and you also encode how the feature interacts with other features. In a layer there might be refinements to multiple classes (exactly what you would do if you had to add a certain feature to your existing program), but with FOP you don't really lose the previous version. The idea is that if you disable a certain feature from a given build, that means that the code that interacts with that feature is not injected but the rest of the features remain unaltered.

Think of it as an additional dimension of polymorphism and how you organize functionality

lurscher