Use of Interface for data hiding
I have an interface I wit开发者_JAVA技巧h two methods func A and func B and a class C with the implementation of the interface, I have two users U1 and U2. I want the functionality so that if u1 accesses class C, func A should be called and if u2 accesses class C func B should be called. How do i implement this using OOPs ?
I think that what you really want is:
- an inteface I with just 1 method
- 2 classes implementing it in different way: func A and func B
- Factory for that interface that take the user as parameter
Example
Let's say:
- U1 is Barney, U2 is Fred.
- func A is printing "I love you Betty"
- func B is printing "Where's my club Wilma?!"
- interface Quote is defined with a method emitQuote()
- classes C and D will implement I with A and B respectively
- define a factory (factory class or factory method doesn't matter) and put the User switch there.
In this way you can call:
Quote q = myFactory.getQuoteFor(u);
q.emitQuote();
This is pure OOP and I think it's pretty simple to write in a TDD fashion.
I will focus on what you've asked. Why you would want to do this and whether it's actually a good idea with respect to OO design principles is another question.
First, your requirement would look similar to the following: (note)
interface I
{
void A();
void B();
}
// class C : I { ... }
Now, let's create a User
role and two implementations for user 1 and user 2:
public interface User
{
void Access(I x);
}
class User1 : User
{
public void Access(I x) { x.A(); }
}
class User2 : User
{
public void Access(I x) { x.B(); }
}
Finally, instantiate your users 1 and 2 as follows:
User u1 = new User1();
User u2 = new User2();
And "access" your C as follows:
I c = new C();
u1.Access(c); // will call c.A()
u2.Access(c); // will call c.B()
Note that this code almost completely leaves class C
out of the game and instead focuses on the interface (I
). If, however, you want your code to work specifically with C
, simply replace I
with C
in the appropriate place (namely, with the parameter of method Access
).
(note:) I've chosen C# for the examples, but translating to the language of your choice should be easy.
More clarification is definitely needed to determine exactly what you're trying to do, but...
Rather than having the logic in the User class, thinking OO-like, maybe a factory, like:
abstract class C : I
{
public void A() { }
public void B() { }
public abstract void CallMethod();
}
class C1 : C
{
public override void CallMethod() { A(); }
}
class C2 : C
{
public override void CallMethod() { B(); }
}
static class Factory
{
public static I GetI(User user)
{
// This is where your if blocks will go,
// and it will return either a new C1 or a new C2
}
}
This has the benefit of allowing the user to remain simple with no business code in it. The factory has to only be smart enough to know which C it wants to create based on the user. All of the actual work of calling the real method is done in the C itself.
EDIT: You may have to fiddle with the interface and the C class a little - in this case, you'd need the CallMethod() method to be in the interface. Or your factory could return a C instead of an I. Of course, without knowing the whole story, it's hard to say which is correct.
精彩评论