I hope the name does not seem too subjective; I absolutely do not want to start a discussion on OO as a whole. I just would like to discuss the main pros and cons for different ways to solve the following problems.
Take this minimal example: you want to express an abstract data type T with functions that can take T as input, output, or both:
- f1: takes T, returns int
- f2: takes a string, returns T
- f3: takes T and double, returns another T
I would like to avoid omitting any other dynamic input. I would also like to avoid mutations whenever possible.
1: abstract class based attempt
abstract class T {
abstract int f1();
abstract void f2(string s);
abstract T f3(double d);
}
2: Parametric polymorphism and auxiliary class
(all implementing TImpl classes will be singleton classes):
abstract class TImpl<T> {
abstract int f1(T t);
abstract T f2(string s);
abstract T f3(T t, double d);
}
, - - - Foo, TImpl. : , , - :
Bar bar<T>(TImpl<T> ti, T t);
3
class TDict<T> {
readonly Func<T,int> f1;
readonly Func<string,T> f2;
readonly Func<T,double,T> f3;
TDict( ... ) {
this.f1 = f1;
this.f2 = f2;
this.f3 = f3;
}
}
Bar bar<T>(TDict<T> td; T t);
# 2 # 3.
class MyT {
}
static readonly TDict<MyT> MyTDict
= new TDict<MyT>(
(t) => ,
(s) => ,
(t,d) =>
);
? № 3 , . , - . , , , . №2 ?