Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i restrict my clients with selected methods from the class?

Let's say I have 1 complete class with around 20 methods which provide different functionalities.

Now we have multiple clients using this class, but we want them to have restricted access.

For e.g. -

Client 1 - Gets access to method1/m3/m5/m7/m9/m11

Client 2 - Gets access to method2/m4/m6/m8/m10/m12

Is there any way I can restrict this access?

One solution which I thought:

Create 2 new classes extending Parent class and override methods which are not accessible and throw Exception from them. But then if 3rd client with different requirement, we have to create new subclass for them.

Is there any other way to do this?

like image 231
Thinker Avatar asked Sep 27 '19 12:09

Thinker


People also ask

How do you restrict your class from being used by your client?

Create 2 new classes extending Parent class and override methods which are not accessible and throw Exception from them. But then if 3rd client with different requirement, we have to create new subclass for them.

How do you limit access to a method of a public class to members of the same class?

At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected . The private modifier specifies that the member can only be accessed in its own class.

Which method is used to control and restrict external access to attributes of a class?

Using the private modifier object encapsulates itself from the outside world and restrict the access to it.


2 Answers

Create 2 new classes extending Parent class and override methods which are not accessible and throw Exception from them. But then if 3rd client with different requirement, we have to create new subclass for them.

It is a bad solution because it violates Polymorphism and the Liskov Substitution Principle. This way will make your code less clear.

At first, you should think about your class, are you sure that it isn't overloaded by methods? Are you sure that all of those methods relate to one abstraction? Perhaps, there is a sense to separate methods to different abstractions and classes?

If there is a point in the existence of those methods in the class then you should use different interfaces to different clients. For example, you can make two interfaces for each client

interface InterfaceForClient1 {
  public void m1();
  public void m3();
  public void m5();
  public void m7();
  public void m9();
  public void m11();
}

interface InterfaceForClient2 {
  public void m2();
  public void m4();
  public void m6();
  public void m8();
  public void m10();
  public void m12();
}

And implement them in your class

class MyClass implements InterfaceForClient1, InterfaceForClient2 {
}

After it, clients must use those interfaces instead of the concrete implementation of the class to implement own logic.

like image 60
Maksym Fedorov Avatar answered Oct 24 '22 00:10

Maksym Fedorov


You can create an Interface1 which defines methods only for Client1, and an Interface2 which defines methods only for Client2. Then, your class implements Interface1 and Interface2.

When you declare Client1 you can do something like: Interface1 client1. With this approach, client1 can accesses only methods of this interface.

I hope this will help you.

like image 15
Texx Avatar answered Oct 24 '22 01:10

Texx