0

I am new to OOP and am writing a small tool in Python that checks Bitcoin prices using a JSON load from the web Bitcoin() class, it monitors the prices Monitor(), notifies the user when thresholds are met Notify() and uses a console-interface Interface() for now to do so.

I have created a Bitcoin() class that can read the prices and volumes from the JSON load. The __init__ definition connects to the web using socket. Since every instance of this class would result in a new socket, I would only need/want one instance of this class running.

  1. Is a class still the best way to approach this?
  2. What is the best way to get other classes and instances to interact with my Bitcoin() instance?
  3. Should I global a Bitcoin() instance? Pass the instance as an argument to every class that needs it?
pedram
  • 2,931
  • 3
  • 27
  • 43

6 Answers6

2

The first thing which concerns me is the SRP violation, your Bitcoin class probably shouldn't be responsible for:

  • opening socket,
  • parsing results,
  • rendering output.

I don't know the details but from my point of view you should split that functionality to smaller classes/functions (in case of using only modules), and one of them will be responsible for retrieving data from web. Please also keep in mind that global state is evil (singletons in some contexts could be described as global state).

Another thing which is a smell from my point of view is opening a socket inside the constructor. This isn't testable, of course you could mock/stub socket, but from my point of view it's better when class requires all it's dependencies as a constructor parameter. By doing it that way you could also notice some classes with to wide responsibility (if your constructor requires more that 3,4 parameters it definitely could be simplified).

Marcin Pietraszek
  • 3,134
  • 1
  • 19
  • 31
  • Thanks, splitting the class may be the way to go. I hate globals too. What would be the preferred way of storing price and volumes so that they are available to other classes as needed? – pedram Apr 04 '13 at 17:41
  • 1
    As always.. it depends (: I know that TDD isn't that good at all times, but in most of them with it you are able to create testable code with decent design (: – Marcin Pietraszek Apr 04 '13 at 17:50
1

http://www.youtube.com/watch?v=o9pEzgHorH0

I'm not sure how relevant this video is for your project (no code to actually read). But maybe you'll pick up the answer to your question. At least you'll learn something new and that's what were here for.

Bryce
  • 320
  • 2
  • 9
  • Thanks, that was worth the time for watching it. It might be a bit complicated to understand for a beginner though, because it requires some programming experience. – Ulrich Eckhardt Apr 06 '13 at 11:48
1

If I were you my code would be something like:
( a class for every set of jobs, which is not what you are doing )

class Interface:
    ''' Handle UI '''
    ...

class Connect:
    ''' Handle web interface '''
    ...

class Bitcoin:
    ''' Handle the calculations '''
    ...

class Notify:
    ''' Notifier '''
    ...

In short, split your classes into smaller simpler classes.

Now for your question:

  1. Yes, because you have a "complex-ish" problem at hand and you're using Python, so it's definitely easier to create a OOP version than a non-OOP one. So, unless you have a good reason not to, Stick to OOP.
  2. In your case, it might as well be passing the instance as an argument.
  3. This is a good idea. This eliminates the problems caused by scopes if you don't have a very good understanding of them.
    But remember you pass the reference, not the value, so manipulating the instance, can and will affect other classes the instance is passed to.

Note: Opening a socket in the constructor of the class is not a good idea. It might be better if you have it in a method.

pradyunsg
  • 18,287
  • 11
  • 43
  • 96
0

The answer is maybe. Depends upon you whole architecture, You should look at the singleton pattern, because you description yells Singleton all over.

http://de.wikipedia.org/wiki/Singleton_%28Entwurfsmuster%29

If you don't find any good reason against creating a class in your given architecture, then just go for it.

Benjamin
  • 609
  • 3
  • 8
0

OOP is a tool, not a goal, you can make a decision whether to use it or not. If you use a Python module, you can achieve encapsulation without ever writing "class".

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • My goal is to learn OOP, so it can be a tool that I have access to. I was hoping to use this project since it seemed small enough to be feasible in that aim. – pedram Apr 04 '13 at 17:38
  • Well, a Python module is an object. It has properties and methods. It provides encapsulation for its internals. The only thing you can't do is to create more than one instance thereof. Also, deriving from a module isn't really supported, although you can well create a module that extends or overrides another module. Suggestion: Try to apply OOP principles and see how it works out, keeping in mind that there might be other ways. – Ulrich Eckhardt Apr 06 '13 at 11:25
0

Sure, you can use python classes for this purpose. You can use module-level instances as well(no global keyword or explicit passing as arguments needed). It is a matter of taste IMHO.

Basically you're asking about Singleton pattern python-specific implementation, it has been answered here: Python and the Singleton Pattern

Description of pattern itself can be found here: http://en.wikipedia.org/wiki/Singleton_pattern

Community
  • 1
  • 1
dredozubov
  • 715
  • 1
  • 6
  • 11