Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 115 116 [117] 118 119 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 882528 times)

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1740 on: February 27, 2012, 06:20:00 am »

Well yes, you can get away without using them, and there are times when I wouldn't use them, but that is far from
Essentially, singletons are just procedural programming wrapped into a complicated objectified interface. You don't have any real need for it, but people still cling to it, because A, it's OOP, and B, it's easy.

I mean both static instances and singleton pattern both have their place, although suggesting that data encapsulation is best done with static classes is a bit far fetched, it would take a lot to argue that.

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1741 on: February 27, 2012, 06:45:44 am »

I never said anything about static classes were best for anything.

But yes, I still stand by what I said in the quote, because it is essentially what singletons are.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1742 on: February 27, 2012, 07:22:00 am »

You are still not thinking in objects.
Take for example if you had to have your program connect to a database, however the database was not hosted locally. You can't ensure you will always be able to make a connection. So a solution is decided that there will be local, temporary hosting, until the connection is restored. If I use static for my context, I will have a shitton of annoying if statements, while if I use singleton I can use inheritance.

Pseudocode.
Code: [Select]
class AbstractContext
public AbstractContext GetInstanct()
if current instance is not set
if connection is made
return new OnlineContext()
else
return new OfflineContext()
else
return instance

public abstract void SaveDataset()

class OnlineContext extends AbstractContext
public abstract void SaveDataset()
Save data to network database

class OfflineContext extends AbstractContext
public abstract void SaveDataset()
Save data to local machine

Insanely easy, keeps logic for the two different types of persistence separate, and keeps the rest of the program agnostic as to what is going on. Best practise.

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1743 on: February 27, 2012, 07:50:22 am »

Oh, so you were talking about singletons in java?
Well, I can't say much about that, as I have no experience in Java and good design there.
In c++ they're still redundant however.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1744 on: February 27, 2012, 07:56:56 am »

You do understand that polymorphism is a thing in c++ too? That wasn't Java, that was pseudocode, or rather pseudo-pseudocode, but semantics. The point is that it is relevant to any OOP language, be it C++, Java, C#, VB or Python.

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1745 on: February 27, 2012, 10:08:51 am »

I see. It looked like Java, so I was assuming you were taking about singletons in Java this whole time.

But, what you're doing here can just as easily be implemented using delegator objects in the context constructor.

Quote
class delegator{
      public:
      virtual void SaveDataset() = 0;
}
class OfflineContext : public delegator {
     void Save Dataset(){
          //Save data offline
     }
}
class OnlineContext : public delegator {
     void Save Dataset(){
          //Save data online
     }
}

class context : public delegator {
    delegator * I;

    public:
    context() {
         if(connection)
             I = new OnlineContext;
         else
             I = new OfflineContext;
    }

   
    void SaveDataset(){
         I -> SaveDataset();
    }
}

I suppose what you find easiest to use is up to you. I personally just despise how singletons interface with the rest of the program. It's easier to use a global object if you need a global object.
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1746 on: February 27, 2012, 10:45:12 am »

You'd think setting up an apache install with php 5.3 on windows would be a breeze.

I assure you, it is not.

Edit: Oh hey, it works. About time, that new job assessment should be coming in any minute.
« Last Edit: February 27, 2012, 11:08:16 am by Siquo »
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

MorleyDev

  • Bay Watcher
  • "It is not enough for it to just work."
    • View Profile
    • MorleyDev
Re: if self.isCoder(): post() #Programming Thread
« Reply #1747 on: February 27, 2012, 10:58:34 am »

Globals should really only be used for something rather low-level anyway imho. Otherwise there's just no need for them compared to passing a reference around. Only global I have so far in my current pet project is a thread pool object, which is hidden behind a bunch of functions so you never use it directly. It's one of those few things which interacts with the code at so deep and fundamental a level that a global is a viable solution for me.

Take your database example. Does the database variable need to be exposed to the elements like that? It's more convenient perhaps, but that would come at the expense of re-usability and maintainability.
« Last Edit: February 27, 2012, 11:05:11 am by MorleyDev »
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1748 on: February 27, 2012, 11:07:28 am »

In my experience, most globals phase to non-global as soon as you scope up. Making stuff global is just to make things easy for now, but once the scope of your project increases, you might need multiple [databases|displays|worlds|engines|cores|whatevers] instead of one. A singleton pattern can ease the change from global to non-global a bit, so I prefer them over "real" globals, but I've learned that "global" is often a temporary state in a project :)
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

malloc

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1749 on: February 27, 2012, 11:51:53 am »

Well Sique, it makes sense that you are not making objects that should be local state, global a state, that is just bad design. Then it honestly does not matter whether it's s singleton or a global.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1750 on: February 27, 2012, 05:13:07 pm »

I must admit I'm a little amused right now... Malloc, perhaps you should go study programming a little with the professionals before you critique a design patten, especially in the way you have described.

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1751 on: February 27, 2012, 06:28:30 pm »

Well Sique, it makes sense that you are not making objects that should be local state, global a state, that is just bad design. Then it honestly does not matter whether it's s singleton or a global.
Design? Whoever works with a design?!? ;)

Just saying that it allows for a bit more flexibility, in the inevitable case the design was changed/inadequate/wrong/smelt funny/etc.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1752 on: February 27, 2012, 07:24:21 pm »

Why is it so hard to wrap my head around ooc!?
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

Willfor

  • Bay Watcher
  • The great magmaman adventurer. I do it for hugs.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1753 on: February 27, 2012, 07:25:31 pm »

Spoiler: LARGE IMAGE (click to show/hide)

So it's coming along.
Logged
In the wells of livestock vans with shells and garden sands /
Iron mixed with oxygen as per the laws of chemistry and chance /
A shape was roughly human, it was only roughly human /
Apparition eyes / Apparition eyes / Knock, apparition, knock / Eyes, apparition eyes /

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #1754 on: February 27, 2012, 07:26:03 pm »

Spoiler: LARGE IMAGE (click to show/hide)

So it's coming along.
Neat.

...what is it?
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting
Pages: 1 ... 115 116 [117] 118 119 ... 796