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

Hello world!

My name is Rui Jarimba, I’m portuguese and I live in Lisbon, Portugal.

I’ve been developing software professionally since 2005, and I think that it’s time to share my knowledge with the world. I’ll be blogging about software development with particular focus on .NET framework and web development.

Some posts will be added soon.

Rui Jarimba