metaclass & constructors - objective-c

It is my understanding that it is difficult to create constructors of classes in Smalltalk and Objective-C. This is because the constructor can't be a message of a class instance because the class Class is not yet defined.
As far as I can tell, the solution is to create a new class whose only instance is itself a class. But how does the constructor work in this situation? I don't understand the process.

I'm talking in terms of Smalltalk. There are two types of things that you could reasonably call constructors here. One is the method that initializes a new instance of a class. The other is the things that initializes a class. Neither of them is difficult.
For instance initialization, the convention is that you implement the "new" method on the class as
new
^super new initialize
(do the superclass's implementation of new, and then send the "initialize" message to the result and return it)
Lots of classes may already inherit this implementation, so you just need to write an initialize method as
initialize
super initialize.
foo := 1.
etc.
To initialize a new class, the mechanism is similar. You implement a class method called "initialize", and it will automatically get sent when the class is loaded into a new Smalltalk environment (image).

The solution is to create a method on the class which does all the necessary initialisation of the instance.

Related

How Cloning Breaks singleton

1) Singleton means the class have one instance. Having private constructer. No way to create object except reflection. No subclassing.
If i want to clone my singleton classthen class must and should implement Cloneable and override clone() right.
am not going to implement Cloneable interface in my Singleton class.
Then how cloning breaks my singleton. is this correct. Please clarify some one. if am wrong.
what is the need of throwing clonenotsupported exception.
No reason to implement Cloneable and override clone() to throw CloneNotSupportedException. Object. Clone, throws expectation when the Cloneable interface is absent.
The correct way to create singleton class using enums can be referred in my favorite java book "Effective Java" . Please read that.

When to use singletons in OOP?

When reading about singletons, I have found this explanation as a reason to use singleton:
since these object methods are not changing the internal class state, we
can create this class as a singleton.
What does this really mean ? When you consider that some method is not changing internal class state ? If it is a getter ? Can someone provide code examples for class that uses methods that are not changing its internal state, and therefore can be used as a singleton, and class that should not be a singleton ?
Usually, when people are explaining singleton pattern, they use DB connection class as an example. And that makes sense to me, because I know that I want to have only one db connection during one application instance. But what if I want to provide an option to force using the new connection when I instantiate DB connection class? If I have some setter method, or constructor parameter that forces my class to open new connection, is that class still a subject to be a singleton ?
I am using PHP, but may understand examples written in JAVA, C#...
This is the article reference. You can ctrl+f search for "internal". Basically, autor is explaining why FileStorage class is a good candidate to be a singleton. I do not understand this sentance
"These operations do not change the internal class state, so we can
create its instance once and use it multiple times."
and therefore I do not understand when to use singletons.
In their example, they have some FileStorage class :
class FileStorage
{
public function __contruct($root) {
// whatever
}
public function read() {
// whatever
}
public function write($content) {
// whatever
}
}
And they say that this class can be a singleton since its methods read() and write() do not chage internal class structure. What does that mean ? They are not setters and class is automatically singleton ?
The quote reads:
These operations do not change the internal class state, so we can create its instance once and use it multiple times.
This means that the object in question has no interesting internal state that could be changed; it’s just a collection of methods (that could probably be static). If the object has no internal state, you don’t have to create multiple instances of it, you can keep reusing a single one. Therefore you can configure the dependency injection container to treat the object as a singleton.
This is a performance optimization only. You could create a fresh instance of the class each time it’s needed. And it would be better – until the object creation becomes a measurable bottleneck.

new and initialize in smalltalk - how to pass parameters to initialize

In smalltalk, when we create a object by calling new which calls initialize . I want to initialize but with my own parameters(passed at run time). How can I do that.
e.g. Myobjcet new
but how do I pass parameters to this so they get passed to initialize.
I am using Pharo.
If I recall, reimplementing class methods new and initialize should be avoided.
Instead, you can create your own class method (other than new or initialize) that takes parameters, and use those when creating your new instance. For example in Squeak look at the class method with: for class Collection. It first creates a collection instance, and then adds to the instance the object passed as an argument.
with: anObject
"Answer an instance of me containing anObject."
^ self new
add: anObject;
yourself
Your Pharo maybe based on Squeak, so you should find the same, or a similar class method for Collection in your image.
Correctly writing the instantiation and initialization code of complex object hierarchies is tricky in Smalltalk. What is more, the default initialization logic as implemented in Object is different across different Smalltalk dialects (i.e. Pharo decided to introduce a default initializer, making things worse).
To avoid confusion and to have clear and consistent rules the Seaside team decided to apply the following rules for all their code:
Object-Initialization at Seaside
Also the Seaside code includes Code Critic rules that check for mistakes in the use of the proposed initialization pattern.

Proper use of private constructors

I was reading about private constructor and found a few points that I couldn't understand. It said, if you declare a constructor as private:
That class cannot be explicitly instantiated from another class
That class cannot be inherited
Should be used in classes containing only static utility methods
My first question: Point 2 says the class cannot be inherited. Well, if you declare a class private then it would still satisfy this property. Is it because, if a class is private, it can still be explicitly instantiated from outside by another class?
My second question: I don't understand point 3. If I have a helper class which is full of static methods, I would never have to instantiate that class to use the methods. So, what is the purpose of a constructor in that class which you are never going to instantiate?
Answer for Java
Question 1 You're confusing a private class, with a class that has a private constructor. Private constructors are used mainly for static classes that are not meant to be instatiated (i.e. they just have a bunch of static methods on them).
Question 2 Exactly there is no need for a constructor so you have to explicitly create a private constructor so that it does not get a default constructer that the JVM will provide if none is defined
An empty class with no methods defined will always be given a no argument constructor by the JVM by default
I take java and c++ as an examples (not the best OO languages known, but very popular) - since you are not defining which languge do you mean.
Ad.2. In these languages you must either call superclass constructor explicitly or it is implicitly called for you. From a subclass you cannot call private methods (only public and protected) - this rule applies to constructors as well. This means if the class has only private constructors, there is no way to call one in subclass constructor. So you cannot subclass such class.
Ad. 3. It is just to avoid confusion - since this class is only a container for utility methods, there is no point in instantiating it. This way you can enforce this rule at compile time.

Getting list of class methods

I'm looking for a way to get a static methods list for a certain class. I only get a list of instance methods with the runtime function class_copyMethodList().
Is there a way to list static methods?
Each Class is itself an Objective-C object, and in turn has an object which is (sort of) its class. You need to get this metaclass object (see also: "[objc explain]: Classes and Metaclasses"), and then ask that for its methods (which will be the class methods* you are after).
From the class_copyMethodList docs:
###Discussion
To get the class methods of a class, use class_copyMethodList(object_getClass(cls), &count)
*There's no such thing as static methods in Obj-C.