Thread: Traditional getter/setter vs content map

Results 1 to 9 of 9
  1. #1 Traditional getter/setter vs content map 
    Registered Member
    Join Date
    Mar 2018
    Posts
    14
    Thanks given
    1
    Thanks received
    0
    Rep Power
    11
    Method 1: traditional getter/setter

    Inventory class:

    Code:
        public class Inventory implements Serializable {
       
          private static final long serialVersionUID = 2011932556974180375L;
    
          private int items; // just example :/
    
          public void addItem(int item){  
            this.items = item;
          }
        
          public int getItems(){
            return items;
          }
    
        }
    Player class:

    Code:
        public class Player implements Serializable {
       
          private static final long serialVersionUID = 1748193556974180375L;
    
          private Inventory invy;
    
          public void setInvy(Inventory invy){  
            this.invy = invy;
          }
        
          public Inventory getInvy(){
            return invy;
          }
    
          public void startCharacter(){
            int ags = 11694;
            getInvy().addItem(ags);
          }
        
        }

    Method 2: "other"

    Inventory class: -same as above inventory class-

    Additional containerHandler class:

    Code:
        public enum HeroContentsContainer {
    
          INVY{
            @Override public Object getContainer(){
              return new Inventory();
            }
          },
    
          ARMOUR;
        
          public Object getContainer(){ //because cannot be static enum constant as every human has different items
          return null;
          }
        }
    Player class:

    Code:
        public class Player implements Serializable {
       
          private static final long serialVersionUID = 1748193556974180375L;
    
          private HashMap<HeroContentsContainer, Object> contents;
    
          public void setContents(){  
            for (HeroContentsContainer c : HeroContentsContainer.values()){
              contents.put(c, c.getContainer());
            }
          }
          
          public HashMap<HeroContentsContainer, Object> getContents(){
            return contents;
          }
        
          public void startCharacter(){
            int ags = 11694;
            //TODO easy replicate this: getInvy().addItem(ags);???
    
          }
    
        }
    So which method is more better/worse; why? I know storing everything in a map is unsafe.

    Can we use the getContents getter in player class to instead return instanceof Inventory instead of object?

    Code:
        player.getContentsXinstanceofY(HeroContentsContainer.INVY, Inventory).addItem(ags);

    This: (below code) seems kind of unproductive considering the objective is to move away from traditional getters/setters

    Code:
        public Inventory getInvy(){
          return (Inventory) contents.get(HeroContentsContainer.INVY);
        }
    Reply With Quote  
     

  2. #2  
    WVWVWVWVWVWVWVW

    _jordan's Avatar
    Join Date
    Nov 2012
    Posts
    3,046
    Thanks given
    111
    Thanks received
    1,848
    Rep Power
    5000
    method 2 is overly done to do the same thing as method 1
    Attached image
    Attached image
    Reply With Quote  
     

  3. Thankful user:


  4. #3  
    Contributor

    clem585's Avatar
    Join Date
    Sep 2013
    Posts
    3,788
    Thanks given
    706
    Thanks received
    702
    Rep Power
    570
    I don't really see the point in adding another layer of abstraction there. At the end you're still gonna have to access the Inventory one way or another:

    Code:
    Inventory inv = player.getInvy();
    Inventory playerInventory = (Inventory) player.getContents().get(HeroContentsContainer.INVY);
    ((Inventory) player.getContents().get(HeroContentsContainer.INVY).doStuff();
    As you can see, you still need to use a getter in every cases.

    If you really hate getters/setters you can always try lombok.
    Project thread
    Reply With Quote  
     

  5. Thankful users:


  6. #4  
    Registered Member
    hc747's Avatar
    Join Date
    Dec 2013
    Age
    26
    Posts
    1,474
    Thanks given
    3,312
    Thanks received
    691
    Rep Power
    1098
    Quote Originally Posted by clem585 View Post
    I don't really see the point in adding another layer of abstraction there. At the end you're still gonna have to access the Inventory one way or another:

    Code:
    Inventory inv = player.getInvy();
    Inventory playerInventory = (Inventory) player.getContents().get(HeroContentsContainer.INVY);
    ((Inventory) player.getContents().get(HeroContentsContainer.INVY).doStuff();
    As you can see, you still need to use a getter in every cases.

    If you really hate getters/setters you can always try lombok.
    Or use Kotlin

    OT: @op, use an EnumMap instead of HashMap when using Enum elements as a maps key.

    Edit: check out Netty's attribute map (link here).

    You could do something similar to:
    Code:
    public static final AttributeKey<Inventory> INVENTORY = AttributeKey.valueOf("inventory");
    
    private final DefaultAttributeMap attributes = ...;
    
    Inventory inventory = player.getAttributes().attr(INVENTORY).get();
    Reply With Quote  
     

  7. Thankful user:


  8. #5  
    Traditional getter/setter vs content map



    Scu11's Avatar
    Join Date
    Aug 2007
    Age
    30
    Posts
    16,307
    Thanks given
    7,215
    Thanks received
    12,308
    Rep Power
    5000
    kotlin extension functions woo

    Attached image
    Reply With Quote  
     

  9. Thankful users:


  10. #6  
    Respected Member


    Polar's Avatar
    Join Date
    Sep 2015
    Age
    28
    Posts
    420
    Thanks given
    0
    Thanks received
    418
    Rep Power
    3098
    As scu said you can do something like this with kotlin..
    Code:
    val Player.inventory by inv(id = 93)
    val Player.bank by inv(id = 95)
    Reply With Quote  
     

  11. #7  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Why is everybody suggesting to use a different language?

    OT: I really don't understand method 2. It looks verbose and if it's supposed to do the same thing as method 1 then it's bad programming.
    Reply With Quote  
     

  12. #8  
    rage against the dying of the light


    Join Date
    Sep 2016
    Posts
    293
    Thanks given
    103
    Thanks received
    92
    Rep Power
    197
    Quote Originally Posted by Kiissmyswagb View Post
    Why is everybody suggesting to use a different language?
    Kotlin is objectively more intuitive and can solve this issue natively rather than with a workaround.
    Reply With Quote  
     

  13. #9  
    Traditional getter/setter vs content map



    Scu11's Avatar
    Join Date
    Aug 2007
    Age
    30
    Posts
    16,307
    Thanks given
    7,215
    Thanks received
    12,308
    Rep Power
    5000
    Quote Originally Posted by Kiissmyswagb View Post
    Why is everybody suggesting to use a different language?

    OT: I really don't understand method 2. It looks verbose and if it's supposed to do the same thing as method 1 then it's bad programming.
    because these concepts are solved with language level paradigms that exist in kotlin and don't exist in java

    Attached image
    Reply With Quote  
     


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. [718] OSRS Content (Maps/NPCs/etc)
    By Ryan. in forum Buying
    Replies: 0
    Last Post: 09-02-2017, 08:51 PM
  2. Using private variables with getter/setters (encapsulation)
    By sadguy in forum Application Development
    Replies: 3
    Last Post: 07-18-2014, 10:15 PM
  3. Getters/Setters - Good or Bad
    By Omoshu in forum Application Development
    Replies: 42
    Last Post: 01-03-2012, 10:48 PM
  4. Content vs Features
    By WH:II:DOW in forum RS2 Server
    Replies: 8
    Last Post: 12-04-2010, 12:00 AM
  5. Getters & Setters - Why you should use them
    By PSNB in forum Tutorials
    Replies: 11
    Last Post: 12-15-2009, 01:51 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
  •