Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I thought singletons were "Evil"? [modified]

I thought singletons were "Evil"? [modified]

Scheduled Pinned Locked Moved C#
learninggame-devquestion
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    venomation
    wrote on last edited by
    #1

    I try not to use global values as I belive that dependancys can be designed in a way that it should be dependant on as few objects as possible howeever I am making a small game and require some behavioural structure and came up with this:

    public sealed class EnterMineAndDigForGold : IState<Miner>
    {
    static readonly EnterMineAndDigForGold _instance = new EnterMineAndDigForGold();

        public static IState<Miner> Instance
        {
            get
            {
                return \_instance;
            }
        }
    
        public  void Enter(Miner owner)
        {
    
            if (owner.Location != LocationType.Mine)
            {
                //Change location if minor is not in the mine
                owner.Location = LocationType.Mine;
                Console.WriteLine("Walked into the mine");
            }
        }
    
        public  void Execute(Miner owner)
        {
    
            owner.AddToGoldCarried(1);
            Console.WriteLine("Picked up some gold");
            owner.IncreaseFatigue(1);
            Console.WriteLine("Getting more fatigued");
    
            if (owner.PocketFull)
            {
                //change state to visit bank
            }
    
            if (owner.IsThirsty)
            {
                //change state to get a drink
            }
        }
    
        public  void Exit(Miner owner)
        {
            Console.WriteLine(owner.GetType().Name + " Is leaving the mine");
        }
    }
    

    Which I have been learning from an AI book have learnt so far. Is this ok to do?

    modified on Tuesday, June 15, 2010 6:10 AM

    L D P 3 Replies Last reply
    0
    • V venomation

      I try not to use global values as I belive that dependancys can be designed in a way that it should be dependant on as few objects as possible howeever I am making a small game and require some behavioural structure and came up with this:

      public sealed class EnterMineAndDigForGold : IState<Miner>
      {
      static readonly EnterMineAndDigForGold _instance = new EnterMineAndDigForGold();

          public static IState<Miner> Instance
          {
              get
              {
                  return \_instance;
              }
          }
      
          public  void Enter(Miner owner)
          {
      
              if (owner.Location != LocationType.Mine)
              {
                  //Change location if minor is not in the mine
                  owner.Location = LocationType.Mine;
                  Console.WriteLine("Walked into the mine");
              }
          }
      
          public  void Execute(Miner owner)
          {
      
              owner.AddToGoldCarried(1);
              Console.WriteLine("Picked up some gold");
              owner.IncreaseFatigue(1);
              Console.WriteLine("Getting more fatigued");
      
              if (owner.PocketFull)
              {
                  //change state to visit bank
              }
      
              if (owner.IsThirsty)
              {
                  //change state to get a drink
              }
          }
      
          public  void Exit(Miner owner)
          {
              Console.WriteLine(owner.GetType().Name + " Is leaving the mine");
          }
      }
      

      Which I have been learning from an AI book have learnt so far. Is this ok to do?

      modified on Tuesday, June 15, 2010 6:10 AM

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      "Global variables" were considered "evil", mainly because of the abuse - Singletons are not inherently evil. Imagine people setting boolean flags to simulate communication between parts of the application :)

      I are Troll :suss:

      L 1 Reply Last reply
      0
      • L Lost User

        "Global variables" were considered "evil", mainly because of the abuse - Singletons are not inherently evil. Imagine people setting boolean flags to simulate communication between parts of the application :)

        I are Troll :suss:

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Eddy Vluggen wrote:

        Singletons are not inherently evil.

        Unless there are too many of them. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Nil Volentibus Arduum


        L 1 Reply Last reply
        0
        • L Luc Pattyn

          Eddy Vluggen wrote:

          Singletons are not inherently evil.

          Unless there are too many of them. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          Nil Volentibus Arduum


          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Luc Pattyn wrote:

          Unless there are too many of them

          That would be confusing. It would become evil if you'd try to justify it by calling it obfuscation :)

          Quoad vos spirum, vos sperum? :suss:

          L 1 Reply Last reply
          0
          • V venomation

            I try not to use global values as I belive that dependancys can be designed in a way that it should be dependant on as few objects as possible howeever I am making a small game and require some behavioural structure and came up with this:

            public sealed class EnterMineAndDigForGold : IState<Miner>
            {
            static readonly EnterMineAndDigForGold _instance = new EnterMineAndDigForGold();

                public static IState<Miner> Instance
                {
                    get
                    {
                        return \_instance;
                    }
                }
            
                public  void Enter(Miner owner)
                {
            
                    if (owner.Location != LocationType.Mine)
                    {
                        //Change location if minor is not in the mine
                        owner.Location = LocationType.Mine;
                        Console.WriteLine("Walked into the mine");
                    }
                }
            
                public  void Execute(Miner owner)
                {
            
                    owner.AddToGoldCarried(1);
                    Console.WriteLine("Picked up some gold");
                    owner.IncreaseFatigue(1);
                    Console.WriteLine("Getting more fatigued");
            
                    if (owner.PocketFull)
                    {
                        //change state to visit bank
                    }
            
                    if (owner.IsThirsty)
                    {
                        //change state to get a drink
                    }
                }
            
                public  void Exit(Miner owner)
                {
                    Console.WriteLine(owner.GetType().Name + " Is leaving the mine");
                }
            }
            

            Which I have been learning from an AI book have learnt so far. Is this ok to do?

            modified on Tuesday, June 15, 2010 6:10 AM

            D Offline
            D Offline
            David Skelly
            wrote on last edited by
            #5

            One thing to note about this example is that it's not a singleton. It's a global property called Instance that is accessed via the EnterMineAndDigForGold class. The key point about a singleton is that there is only one instance at any point in time and there can never be more than one. There is nothing stopping me from ignoring the Instance property on EnterMineAndDigForGold and creating as many instances of EnterMineAndDigForGold as I want. In order to make this a singleton you would need to make sure that no-one can create any instances of EnterMineAndDigForGold other than the one created and held as the static variable _instance. The easiest way to do this is to make the constructor for EnterMineAndDigForGold private. That will stop any other class from creating one of these. To be honest, looking at your class I don't think it really matters because your EnterMineAndDigForGold class doesn't have any instance state so it doesn't matter whether you create a new one every time, or share one around, or make all the methods static so that you never need an instance at all.

            1 Reply Last reply
            0
            • L Lost User

              Luc Pattyn wrote:

              Unless there are too many of them

              That would be confusing. It would become evil if you'd try to justify it by calling it obfuscation :)

              Quoad vos spirum, vos sperum? :suss:

              L Offline
              L Offline
              LimitedAtonement
              wrote on last edited by
              #6

              I was thinking that would be the one case (obfuscation) of good justification for too many globals.

              In Christ, Aaron Laws http://ProCure.com

              1 Reply Last reply
              0
              • V venomation

                I try not to use global values as I belive that dependancys can be designed in a way that it should be dependant on as few objects as possible howeever I am making a small game and require some behavioural structure and came up with this:

                public sealed class EnterMineAndDigForGold : IState<Miner>
                {
                static readonly EnterMineAndDigForGold _instance = new EnterMineAndDigForGold();

                    public static IState<Miner> Instance
                    {
                        get
                        {
                            return \_instance;
                        }
                    }
                
                    public  void Enter(Miner owner)
                    {
                
                        if (owner.Location != LocationType.Mine)
                        {
                            //Change location if minor is not in the mine
                            owner.Location = LocationType.Mine;
                            Console.WriteLine("Walked into the mine");
                        }
                    }
                
                    public  void Execute(Miner owner)
                    {
                
                        owner.AddToGoldCarried(1);
                        Console.WriteLine("Picked up some gold");
                        owner.IncreaseFatigue(1);
                        Console.WriteLine("Getting more fatigued");
                
                        if (owner.PocketFull)
                        {
                            //change state to visit bank
                        }
                
                        if (owner.IsThirsty)
                        {
                            //change state to get a drink
                        }
                    }
                
                    public  void Exit(Miner owner)
                    {
                        Console.WriteLine(owner.GetType().Name + " Is leaving the mine");
                    }
                }
                

                Which I have been learning from an AI book have learnt so far. Is this ok to do?

                modified on Tuesday, June 15, 2010 6:10 AM

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Singletons are unnecessary (though not necessarily evil) in C#, especially since the advent of static classes. Only use singletons in lesser languages; like C++.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups