Improve your UI code with Strategy Pattern

This article is intended to provide a brief introduction to the Strategy Design Pattern, and how can help us to improve our User Interface code.

The scenario – A simple blogging application

Blogging application - showing actions by role

You have a blog application and you want to limit user actions based on their roles.
You have the following roles:

  • User
  • Writer
  • Editor

and the following actions:

  • New Post – Creates a new blog post
  • Edit Post – Edits an existing blog post
  • Submit Post – Submits a post for approval
  • Reject Post – Rejects a previously submitted blog post
  • Publish Post – Publishes a previously submitted blog post

The table below shows the actions per role type

Actions

User

Writer

Editor

New post

x

Edit post

x

Submit post

x

Reject post

x

Publish post

x

Role type is represented by an enumeration:

public enum RoleType
{
    User,
    Writer,
    Editor
}

What’s the best solution for our problem?

Continue reading