I want to design a solution, but facing a problem because of limited knowledge of OOPs concept. My requirement is as follows
Assume I have 4 classes
Class A
Class B
Class C
Class D
I want object of class D can only be allowed to create, or class can only be accessible in Class C. Same Class C object and class itself can be allowed access and created in Class B. Class D should not have access to class B.
Same Class A should not have access to C, D. It only have access to B, and it can create object of B.
How we can implement with best possible architecture/design pattern in C#.
With Best Regards.
Take a look at this question: Ensure that only class A can call class B
After you have read that, you will know you have to use a combination of package and private classes.
The only solution of your problem can be solved by the use of private inner classes.
make class D class inner to class C and class C inner to class B and so on.
but remember the use of inner classes is highly deprecated.
implementing this in C#, I leave up to you.
Related
I am a java developer and learnt scala recently. Can anybody help to understand this confusion.
suppose i am having the below code structure.
abstract class A {
val message: String
}
class B extends A {
val message = "I'm an instance of class B"
}
trait C extends A {
def loudMessage = {message.toUpperCase()}
}
class D extends B with C
now the question is about last line of code : when we are extending class B in D, B is already containing the members of class A. Why we should write like this ? Is there any difference ?
Thanks a lot for even reading my question. For the person who is thinking and answered, hats off.
There's no difference, since extending class B, you got the message part, further on mixing in trait C which also needed message, but we already have it from B and now we have the loudMessage from C as well.
Copied from Wikipedia-
a class can only inherit from a single class, but can mix-in as many
traits as desired. Scala resolves method names using a right-first
depth-first search of extended 'traits', before eliminating all but
the last occurrence of each module in the resulting list.
this resolves the diamond problem (multiple inheritance).
It's a general practice to write in traits and mix them in your classes whenever necessary, you'll find them interestingly useful when writing unit tests, create a lot a of fake data/ mocks in traits and mix them in whenever.
Say I have an abstract class A. Now I want to define a final class ABot such that for any class B that extends A (except Nothing), ABot is a subclass of B. It's just like Nothing, but with respect to the hierarchy where A is the top.
Answer is No.
A class will not be a subtype of X unless you explicitly extend X.
scala.Nothing is an exception because Scala compiler does the magic for you.
If there are two classes B and C, which extend A but neither of them extends the other, you can't have a class (or a trait) which extends both.
We are having a UML course. The teacher said:
Every class should be declared as abstract if it serves as base class for
its derived classes.
In the following figure suppose that we want to derive class german shepherd and class labrador from class chien (Dog woof woof). Is it an obligation for class chien to become an abstract class or not?
Not necessarily.
That statement isn't necessarily true. A more correct statement would be:
Every class should be declared as abstract if it cannot be instantiated without referring to a concrete derived class.
In your example, it makes sense that Dog and Animal would be abstract, because you have more specific classes that likely fill out details that the base classes do not.
However, it is certainly possible to have a class which is concrete and can be instantiated, (and therefore not abstract), but still serve as the base for another class.
It should be abstract if it's a generalization and cannot exist on it's own.
Look at this situation:
In the image above Relation is abstract. It can't exist by it's own. Customer and Employee are normal classes who extend Relation. But Trainee is a Employee.
You could create a Employee, but also a trainee which is a Employee as well.
In PowerMockito, we can use the pattern "whenNew(MyClass).thenReturn(mockMyClass)" when someone wants to new an instance of MyClass, it will receive mockMyClass instead of the real instance.
Can we do similar things in ScalaMock or EasyMock? I spent whole day for this issue.
This is my scenario, I have two classes A and B. Inside class A, it will new an instance of class B.
Now I want to test class A, and when class A creates a new instance of class B, what I want is to return a mock object of class B (not the real class B).
In Java, I can handle this issue easily with PowerMock and JUnit, but I cannot do it in Scala.
For EasyMock, it is not directly possible. You need to use PowerMock. See here
I have a clue about Object Oriented Programming:
I need to have a parent class HandlerException which needs to define the sign of three methods (MethodA, MethodB, MethodC).
Then, I have a child class BusinessHandler which inherits from HandlerException and defines ONLY the MethodA of its parent class.
Then, I have a child class DataHandler which inherits from HandlerException and defines ONLY MethodC of its parent class.
Then, I have a class named CustomerDAO which inherits from DataHandler and consumes the MethodC written on its parent class. (consumes it like: DataHandler.MethodC).
As you can see, its a typical object oriented programming problem; I need to have some static methods (MethodC) to access it directly without any instance of the class. The parent class HandlerException could be abstract? and its 3 methods (A, B and C) could be ???? (that's my question, how is the RIGHT way to write this parent class: abstract with abstract members, or virtual, or maybe an interface?)
I hope you got the idea of my question and that I made myself clear. Thanks in advance.
I forgot: I'm using C#, and to mention: MethodB would be implemented on the next release of the app.
Depends on the language you are using, but it sounds like the HandlerException class would be abstract and all three methods would be virtual.
If the HandlerException class has absolutely no implementation whatsoever (only defines those three methods) then it would probably make sense to make it an interface rather than an abstract class.
Also, where is MethodB implemented? If it isn't implemented by any of those classes, then all the classes would need to be abstract.