OOP question
Monkey Forums/Monkey Programming/OOP question
| ||
I'm going through some of the code I've found on the forum and there's a construct that I don't understand (probably due to my limited OOP knowledge). What's the meaning of the function Create and why would I use it since it calls itself? Class Rectangle Extends Node Method New() Super.New() Self.anchorPointX = 0.5 Self.anchorPointY = 0.5 End Method Function Create : Rectangle() Local this : Rectangle = New Rectangle() Return this End Function Method Draw() SetColor( Self.r, Self.g, Self.b ) DrawRect -( Self.sizeX * Self.anchorPointX ), -( Self.sizeY * Self.anchorPointY ), Self.sizeX, Self.sizeY End Method End Class |
| ||
It's probably just someone who's still too used to BlitzMax to give up the need to use create to have arguments when creating a new object. It has absolutely nothing to do with OOP. |
| ||
It's a static Factory method on the class. As it doesn't allow parameterisation or do anything other than call the constructor I've no idea why you'd use that specific example but they are often used where you want to track instances of a class, use an internal pool, be able to swap out implementations or just provide a convenient default. You'd usually see them together with a private constructor. |
| ||
Code is from this thread. http://www.monkeycoder.co.nz/Community/posts.php?topic=1968&post=18798 You can see it uses it to draw different circles to the same variable |
| ||
The whole things doesn't shed any more light on what the point of the Create function specific example is. Perhaps you should ask Noodle. However, the general form of a class factory method does have uses as I mentioned above. |
| ||
muddy_shoes wrote: You'd usually see them together with a private constructor. How can you make a constructor private in Monkey? I can't get it to work: Class Rectangle Private Method New() End Public Function Create : Rectangle() Return New Rectangle() End Method Show() Print(">> Rect::Show") End End Function Main() Local rect1:Rectangle = Rectangle.Create() rect1.Show() Local rect2:Rectangle = New Rectangle rect2.Show() End |
| ||
Private is per module (file) not per class. That said, I don't think you can |
| ||
How can you make a constructor private in Monkey? I can't get it to work: Read the second part of this post: http://www.monkey-x.com/Community/posts.php?topic=7764&post=75811 |
| ||
Read the second part of this post: http://www.monkey-x.com/Community/posts.php?topic=7764&post=75811 Throwing a runtime error is not a professional solution to the problem in my opinion, so I'm still looking for a better way, if there is any. Calling a private constructor/function is usually a compile time error, like accessing a private variable from the outside. |
| ||
No, it's not a professional solution, but as far as Monkey is concerned, there's no other choice. There is literally no other way to stop this because Monkey adds a default empty constructor to every class you've not defined one for. |
| ||
Also, only fields are actually private. Not sure whether it is intended to extend this to methods. The other thing I would say is that throwing a runtime error to a compile error is untidy, but not too bad, at least it stays in the development cycle... |
| ||
Also, only fields are actually private. You are right, thanks. |