1
\$\begingroup\$

I would like to ask you for review of my application’s Status viewing logic implementation.
At first I have to close to you somewhat of my domain problem space. I’m building social application. Part of my application are user’s statuses. User should add private status.
Similarly, user should add public status. Private status is visible only to the Followers (It’s like on Instagram, user can follow other user to eg. get notification about followed user activities), and Public is visible to every user. Status owner can check information about who previewed his status.

When it comes to domain objects (I mean real world objects by this), I have following objects:

UserProfile (represents user. My approach is to not confusing actors with inner system objects. I mean, we have to consider two systems.

First is outer system, which is everything, that interacts with our system and second is inner system, generally system we are implementing. What is more we should have unambiguous terms in our namespace, so user is someone who interacts with our system by eg. asking it to send message and UserProfile is something which represents user in our system eg. Storing user’s data. Sorry for my bloated explanation. So that’s why I don’t have class user)

Status

Now we go to some of concrete implementation.

My approach is to delegate logic of giving preview of status to significant user in different class than Status. Status class has only flag, which indicates whether status is public or not.

class Status {

 private boolean privy;

 boolean isPublic() {

  return !privy;

 }

 static Status ofInaccessible() {
   return InaccessibleStatus.create();
 }

}

I also thought to use in some degree Null Object pattern for status, which can be accessed by user. I said in some degree, because the main goal is to return some text informing about blocked content ( I know I could return 404 status instead but...)

class InaccessibleStatus extends Status {

  // Overriding  methods omitted

}

Here we have UserProfile class, which status viewing logic is delegated to StatusPreviewer class:

class UserProfile {

 private final StatusPreviewer statusPreviewer;

 private UserProfile(...) {
  this.statusPreviewer = StatusPreviewer.create(this);
 }

 // Cheking if user is on followers list - omitted method definition
 boolean isFollowing( UserProfile UserProfile );

 Status preview( Status status ) {
  return statusPreviewer.preview( status )
 }

}

And now the time for status view logic has come. I thought for a while about encapsulating logic of status view inside Status. Mhmm.. Can Status know who can read him? Rather not. So I came up with specialized class StatusPreviewer which is only responsible for giving access to status, by returning Status or InaccessibleStatus object.

So here is my StatusPreviewer class:

class StatusPreviewer {

 private UserProfile previewedBy;

 private StatusPreviewer( UserProfile previewedBy ) {

  this.previewedBy = previewedBy;

 }

 preview( Status status ) {

   if (status.isPublic() || previewedBy.isFollowing( status.getOwner())) {
     status.addViewer( userProfile );
     return status;
   }
   else return Status.ofInaccessible();

 }

}

Of course, I won’t use domain object directly within presentation layer. StatusPreviewer is some kind of indirect object used by UserProfile as containment(composition). Someone could think that StatusPreviewer is also UserProfile, so it should be subclass of UserProfile. But the point is, that I don’t need all of even part of UserProfile interface, so StatusPreviewer is different type.

I always try to write readable, easy to maintain code, that benefit from SOLID principles, but I am still learning.

Thanks in advance for any review from more experienced users or even someone with better, fresh insight.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.