1

I have to create the following functions (to a command-line casino in Matlab):

function [wonAmount, noGuesses] = highLow(gambledAmount)
function [wonAmount, noPulls] = slotMachine(gambledAmount, betFactor)
function wonAmount = roulette(gambledAmount, typeOfBet)

This is a matter of the task I'm given, and it has to be fulfilled. I could just create simple functions, since all the games have some similar characteristics, calculation of wonAmount, etc. and generally that OOP is more structured, I would like to try it (OOP) out in Matlab.

I could create a handle class, but I have to fulfill the requirements of the task. Which a handle class with a method play - I am of the understanding that a handle class constructor HAS to return the object itself? I am looking for a class in which the constructor doesn't necessarily return the constructor - a static class/function of a kind?

How would you design this class?

2
  • not sure if this is what you are looking for, but what about the singleton pattern (private class constructor, with a static method to return the single instance) Commented Nov 21, 2012 at 22:12
  • looking at your other linked question, it looks to me you are creating classes for the wrong reason if you are only using them for static functions, where there is no "state" encapsulated.. Commented Nov 21, 2012 at 22:53

1 Answer 1

2

It sounds like you need the interface to your program to look like function calls, but internally you want to use OO programming. Is that right?

Assuming you NEED the interface to look like:

[wonAmount, noGuesses] = highLow(gambledAmount)

You could write code inside the highLow function that does:

function [wonAmount, noGuesses] = highLow(gambledAmount)
game = highLowGame; %instantiate the game, and run it:
[wonAmount, noGuesses] = highLowGame.run(gambledAmount);

Or you can use static methods:

function [wonAmount, noGuesses] = highLow(gambledAmount)
[wonAmount, noGuesses] = highLowGame.runGame(gambledAmount);

http://www.mathworks.com/help/matlab/matlab_oop/static-methods.html

Where I'm assuming highLowGame.m looks like this:

 classdef highLowGame < casinoGame

There's no good reason to use handle classes for this, unless you really want a specific calling syntax / handle behavior...

If for some reason you need this all to be in one M-file, then I'm afraid you're out of luck... but that seems like a silly restriction.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes. I would like internally to use OOP and just make sure I meet project requirements. I will talk with my lecturer on the subject tomorrow - how strict they are. But I think I'll go with your advice and to a static method, which is called from the function. Thanks for your input.
Is there a Matlab-equivalent to PHP self::callFunction() so I don't have to hardcode the object name inside a static class?
Not quite sure what you mean; can you give an example? This might be a reasonable separate stackoverflow question
I see what you mean now. On it over there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.