mardi 30 mars 2021

Design Pattern for different users

I have 2 different types of users in my app: LoggedInUser and GuestUser They both have same functionalities such as createNewOrder and cancelOrder (the implementation should be different) but LoggedInUser can do much more such as WriteReview,FavouriteProduct etc.

I want a design pattern for these types of users, so that I don't check which type of user my application has at the moment and I just call the method without knowing the actual type of the user.

Is it even possible to do this? Which design pattern is the most suitable?

What I have done so far is creating an abstract class called User which has the similar functionalities:

abstract class User{
  void createOrder()
  void cancelOrder()
}

and also a class for LoggedInUser and a class for GuestUser:


class LoggedInUser extends User{
  //some instance variables
  void favouriteProduct(String id){
   //...
}

 void writeReview(Review review){
 //...
 }
  @override
  void cancelOrder(){
 //...
 }
  @override
  void cancelOrder(){
 //...
 }
}

class GuestUser extends User{
 //some instance variables
 
 @override
  void cancelOrder(){
 //...
 }
  @override
  void cancelOrder(){
 //...
 }
}

Thanks

Aucun commentaire:

Enregistrer un commentaire