I'm a computer science student and on one of my recent projects I have been dealing with constructing a simple application that is maybe an example of large/medium-scale programming. It's first implementation will be in C language and we will then reconstruct it in Java.
This has made me question the OOP principles about encapsulation and how to achieve it and also how to design a system through the MVC architecture.
Without further ado, here's my problem in a very simplified way:
Let's say I have the following two classes:
a.h
typedef struct a *A;
void insert_into_a(A a, something_to_insert s);
void do1(A a);
stuff do2(A a);
b.h
typedef struct b *B;
void insert_into_b(B b, something_to_insert s);
void do3(B b);
some_value do4(B b);
Note: I am aware that hiding pointers might be criticizable but it was requested to be done so, please, ignore that fact.
Those two classes are pieces of the same final goal but don't directly have a relation. So my interest is on how to build a third, C class, that will interact with those two, without breaking encapsulation. This C class must be able to load the subclasses (A and B) and perform queries on them (it kinda acts like a database) and this was my approach:
c.c
struct c{
A a;
B b;
};
void insert_into_a_through_c(C c, something_to_insert s){
insert_into_a(c->a, s);
}
void insert_into_b_through_c(C c, something_to_insert s){
insert_into_b(c->b, s);
}
something query1(C c){
/* Get some info from subclass */ stuff s = do2(c->a);
/* Get some info from subclass */ some_value sv = do4(c->b);
/* do some other things with s and sv */ something some = something_with_s_and_sv(s, sv);
return some;
}
So, I'm never breaking encapsulation because I do not allow the user to ever have anything on their hands. If you want to put something in A you do it through C. And this was all decently fine until I tried to move to MVC architecture and then I realized the following...
It's not C who should be making queries on himself, that's the job of the controller who should parse, retrieve data, act on it and pass it to the view. So, I thought about fixing this but then it hitted me. This would need me to have yet another function in C to use each of the subclasses functions. It does means all implementation is private and allows completely to change the implementation of A and B but it requires for every action to be "duplicated" in C.
( This is what I want to get an answer for! )
But, as is, it can't fit MVC. I won't be able to rollback to the typical OOP way (get-set-clone) so I just wanted to ask if it's alright to make a controller that receives data already processed (how you see above) or if it would make more sense to move query1 (for example) out of c.c and pass the inner logic to the controller, adding do2_through_C(C c) and do do4_through_C(C c) to the C API.