M
M
MIK Ek2017-01-26 16:38:14
OOP
MIK Ek, 2017-01-26 16:38:14

How to implement access control from the point of view of OOP?

There are 2 classes (Man and dog). There are 2 human specimens and 1 dog. Man A is the owner of the dog. Person B is a trainer. A dog can have many owners, but a person can have many dogs.
Condition: The dog can only be stroked by its owner. A trainer can stroke any dog.
How to build classes correctly, what checks should be where from the point of view of OOP?
Specific question: Should the dog know that the handler can stroke it, or should it ask the person if he can stroke it?
Provided that then cats can be added, which can be stroked by both owners and children. And then grandmothers will be added who can stroke everyone.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2017-01-26
@saboteur_kiev

"Specific question: Should the dog know that the handler can stroke it, or should it ask the person if he can stroke it?"
This is a question for you, not for the code.
Add an array of "owners", "trainers" to the dog class and add the owners and trainers there.
Give the dog a "pat" method, because it is the dog that can be petted, and check who is trying to call this method - if he is on the list of "owners".
An exception can be added to the method that if "grandmother" strokes, she is not required to be in the list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question