Object oriented design suggestion

前端 未结 9 2178
余生分开走
余生分开走 2021-02-13 04:43

Here is my code:

class Soldier {
public:
   Soldier(const string &name, const Gun &gun);
   string getName();
private:
   Gun gun;
   string name;
};

cl         


        
9条回答
  •  一向
    一向 (楼主)
    2021-02-13 05:27

    The Law of Demeter would say to encapsulate the functions.

    http://en.wikipedia.org/wiki/Law_of_Demeter

    This way, if you want some type of interaction between the soldier and the gun, you have a space to insert the code.

    Edit: Found the relevant article from the Wikipedia link: http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/paper-boy/demeter.pdf The paperboy example is very, very similar to the soldier example you post.

提交回复
热议问题