Thread: Commands as an enum

Page 4 of 7 FirstFirst ... 23456 ... LastLast
Results 31 to 40 of 64
  1. #31  
    Registered Member
    Join Date
    Apr 2018
    Posts
    102
    Thanks given
    27
    Thanks received
    9
    Rep Power
    71
    Quote Originally Posted by hc747 View Post
    Building off of the previous post, your design could be as follows:

    Code:
    interface Command {
    
    
     void execute(Player player);
    
    
     default boolean canUse(Player player) {
      return true;
     }
    
    
    }
    
    
    enum PlayerCommands implements Command {
    
    
     HOME {
      @Override
      public void execute(Player player) {
       //TODO: implement
      }
     },
     YELL {
      @Override
      public void execute(Player player) {
       //TODO: implement
      }
     };
    
    
    }
    
    
    ...
    
    
    enum ModeratorCommands implements Command {
     BAN {
      @Override
      public void execute(Player player) {
       //TODO: implement
      }
     };
    
    
     @Override
     public boolean canUser(Player player) {
      return player.getRights() >= MODERATOR;
     }
    }
    
    
    ...
    
    
    class MiscellaniousCommand implements Command {
    
    
     @Override
     public void execute(Player player) {
      //TODO: implement
     }
    
    
    }
    Just noticed you may know C#, I’ve effectively used these as models
    Reply With Quote  
     

  2. #32  
    Respected Member


    Polar's Avatar
    Join Date
    Sep 2015
    Age
    28
    Posts
    420
    Thanks given
    0
    Thanks received
    418
    Rep Power
    3098
    Please don't use enums for this.
    Reply With Quote  
     

  3. Thankful users:


  4. #33  
    Registered Member
    Join Date
    Apr 2018
    Posts
    102
    Thanks given
    27
    Thanks received
    9
    Rep Power
    71
    Quote Originally Posted by Polar View Post
    Please don't use enums for this.
    Please give reasoning
    Reply With Quote  
     

  5. #34  
    Respected Member


    Polar's Avatar
    Join Date
    Sep 2015
    Age
    28
    Posts
    420
    Thanks given
    0
    Thanks received
    418
    Rep Power
    3098
    Quote Originally Posted by CodeForScape View Post
    Please give reasoning
    There are design patterns built for this kind of thing (e.g. Command pattern or even Observer pattern). Using an enum in this case seems like misuse of enums like Spooky said on page 1.
    Reply With Quote  
     

  6. Thankful users:


  7. #35  
    Registered Member
    Join Date
    Apr 2018
    Posts
    102
    Thanks given
    27
    Thanks received
    9
    Rep Power
    71
    Quote Originally Posted by Polar View Post
    There are design patterns built for this kind of thing (e.g. Command pattern or even Observer pattern). Using an enum in this case seems like misuse of enums like Spooky said on page 1.
    Thanks very much, just wanted some more information as it's always good to see other people's POV. Thanks for the design patterns!
    Reply With Quote  
     

  8. #36  
    Registered Member Versatile's Avatar
    Join Date
    Dec 2014
    Age
    26
    Posts
    433
    Thanks given
    247
    Thanks received
    9
    Rep Power
    9
    Quote Originally Posted by CodeForScape View Post
    DISCLAIMER: I capitalise my methods. I know. I’m sorry.

    I converted my commands into an enum

    Attached image

    I suppose the only real benefits are it looks quite nice and I can query my commands, so I could return a list of donator commands to display etc
    I'm not trying to bring shame to you or anything, but wouldn't this one be more sufficient and easier to do?

    Attached image
    Spoiler for Don't be a deadfool:
    Reply With Quote  
     

  9. #37  
    Renown Programmer
    Greg's Avatar
    Join Date
    Jun 2010
    Posts
    1,179
    Thanks given
    260
    Thanks received
    1,012
    Rep Power
    2003
    Quote Originally Posted by Versatile View Post
    I'm not trying to bring shame to you or anything
    Likewise, try making a command with another "int img" or "String answer" it can become quite a pain and isn't scaleable
    Attached imageAttached image
    Reply With Quote  
     

  10. Thankful users:


  11. #38  
    Donator


    Join Date
    Aug 2012
    Posts
    2,462
    Thanks given
    312
    Thanks received
    459
    Rep Power
    457
    Thanks for creating this thread. Got a good deal of education from your post and some of the replies giving good feedback and alternatives.
    Attached image

    Attached image
    Reply With Quote  
     

  12. #39  
    kierandevvs
    Guest
    Quote Originally Posted by CodeForScape View Post
    DISCLAIMER: I capitalise my methods. I know. I’m sorry.

    I converted my commands into an enum

    Attached image

    I suppose the only real benefits are it looks quite nice and I can query my commands, so I could return a list of donator commands to display etc
    This breaks the SOLID principle: open close, use the strategy pattern to segregate responsibilities and ensure code change isn't required for code extension.
    Reply With Quote  
     

  13. #40  
    Donator

    Join Date
    Feb 2017
    Posts
    104
    Thanks given
    0
    Thanks received
    14
    Rep Power
    46
    Quote Originally Posted by CodeForScape View Post
    Do you use regex a lot? Could you give some examples of common use cases? I’ve only ever used regex for things like validating a postcode etc
    Fairly commonly myself. Nothing quite so extensively complicated as that guy how on earth did you learn that? It's like its own programming language lol. I use it a lot for converting the format of files for instance a tabbed prices file to json which I would never actually do because that would be huge.
    Reply With Quote  
     

Page 4 of 7 FirstFirst ... 23456 ... LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 6
    Last Post: 08-12-2011, 03:41 AM
  2. Replies: 1
    Last Post: 01-15-2011, 01:05 AM
  3. Replies: 12
    Last Post: 10-31-2010, 10:22 PM
  4. Replies: 1
    Last Post: 04-27-2010, 01:34 AM
  5. Replies: 10
    Last Post: 05-31-2009, 01:42 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •