Thursday 30 October 2014

Proxy Design Pattern


Proxy Design pattern is one of the Structural design pattern. It is useful when you want to provide controlled access to a resource.

Let’s say I have a news application, where,

User can able to
1.   Read news items
2.   Comment on news items
3.   Share news item

Admin can able to
1.   Read News items
2.   Comment on News items
3.   Add news category
4.   Remove news category
5.   Add news item
6.   Remove news item
7.   Share news item

I want to restrict non-admin users from adding/removing news items, categories. By using Proxy design pattern we can restrict non-admin users.

NewsApp.java
public interface NewsApp {
    String readNewsItem = "READ_NEWS_ITEM";
    String commentNewsItem = "COMMENT_NEWS_ITEM";
    String shareNewsItem = "SHARE_NEWS_ITEM";
    String addNewsCategory = "ADD_NEWS_CATEGORY";
    String removeNewsCategory = "REMOVE_NEWS_CATEGORY";
    String addNewsItem = "ADD_NEWS_ITEM";
    String removeNewsItem = "REMOVE_NEWS_ITEM";
    
    void readNewsItem();
    void commentNewsItem();
    void shareNewsItem();
    void addNewsCategory();
    void removeNewsCategory();
    void addNewsItem();
    void removeNewsItem();
}


NewsAppActions.java
public class NewsAppActions implements NewsApp{

    @Override
    public void readNewsItem() {
        System.out.println("Reading NewsItems");
    }

    @Override
    public void commentNewsItem() {
        System.out.println("Comment on news item");
    }

    @Override
    public void shareNewsItem() {
        System.out.println("share news item");
    }

    @Override
    public void addNewsCategory() {
        System.out.println("add news category");
    }

    @Override
    public void removeNewsCategory() {
        System.out.println("Remove news category");
    }

    @Override
    public void addNewsItem() {
        System.out.println("Add news item");
    }

    @Override
    public void removeNewsItem() {
        System.out.println("remove newsitem");
    }

}


ProxyNewsAppAction.java
public class ProxyNewsAppAction {
    private NewsAppActions actions = new NewsAppActions();
    boolean isAdmin = false;

    ProxyNewsAppAction(String user, String password){
        if(user.equals("Krishna") && password.equals("Krishna")){
            isAdmin = true;
        }
    }

    public void runCommand(String cmd){
        if(cmd.equals(NewsApp.addNewsCategory) ||
            cmd.equals(NewsApp.removeNewsCategory)||
            cmd.equals(NewsApp.removeNewsItem)||
            cmd.equals(NewsApp.addNewsItem)){
            if(isAdmin == true){
                if(cmd.equals(NewsApp.addNewsCategory)){
                    actions.addNewsCategory();
                }
                else if(cmd.equals(NewsApp.removeNewsCategory)){
                    actions.removeNewsCategory();
                }
                else if(cmd.equals(NewsApp.removeNewsItem)){
                    actions.removeNewsItem();
                }
                else{
                    actions.addNewsItem();
                }
            }
             else{
                System.out.println("You don't have permission");
             }
        }
        else{
            if(cmd.equals(NewsApp.readNewsItem)){
                actions.readNewsItem();
            }
            else if(cmd.equals(NewsApp.commentNewsItem)){
                actions.commentNewsItem();
            }
            else if(cmd.equals(NewsApp.shareNewsItem)){
                actions.shareNewsItem();
            }
        }
    }
}


Client.java
public class Client {
    public static void main(String args[]){
        ProxyNewsAppAction admin = new ProxyNewsAppAction("Krishna", "Krishna");
        ProxyNewsAppAction user = new ProxyNewsAppAction("user", "abcd");

        System.out.println("For Admin");
        System.out.println("*****************************");
        admin.runCommand(NewsApp.addNewsCategory);
        admin.runCommand(NewsApp.addNewsItem);
        admin.runCommand(NewsApp.commentNewsItem);
        admin.runCommand(NewsApp.readNewsItem);
        admin.runCommand(NewsApp.removeNewsCategory);
        admin.runCommand(NewsApp.removeNewsItem);
        admin.runCommand(NewsApp.shareNewsItem);
        System.out.println("*****************************");

        System.out.println("For non Admin");
        System.out.println("*****************************");
        user.runCommand(NewsApp.addNewsCategory);
        user.runCommand(NewsApp.addNewsItem);
        user.runCommand(NewsApp.commentNewsItem);
        user.runCommand(NewsApp.readNewsItem);
        user.runCommand(NewsApp.removeNewsCategory);
        user.runCommand(NewsApp.removeNewsItem);
        user.runCommand(NewsApp.shareNewsItem);
        System.out.println("*****************************");
    }
}


Output
For Admin
*****************************
add news category
Add news item
Comment on news item
Reading NewsItems
Remove news category
remove newsitem
share news item
*****************************
For non Admin
*****************************
You don't have permission
You don't have permission
Comment on news item
Reading NewsItems
You don't have permission
You don't have permission
share news item

No comments:

Post a Comment