0:00 / 0:00 Our point factory example is actually looking pretty good.
In fact, it’s so good that we can finally get rid of this pesky enum for the coordinate system because
it’s no longer required because the name of the coordinate system is actually embedded in the factory
methods of the factory.
However, there is still one problem that we need to address, and this problem is the problem of the
public constructor.
Now, remember, I cannot simply make the constructor private because then Point Factory is trying to
access a private member which is not allowed.
So the question is how can you mitigate this situation?
Now if you’re writing a library intended to be consumed by other users in their own assemblies, then
the solution is very simple.
You simply make the constructor internal and that way it kind of solves the problem because it allows
the use of the constructor within the current assembly, but doesn’t allow the use of the constructor
within anybody who’s actually consuming that assembly from the outside.
Now, if you want to have this particular constraint right inside your own assembly, then it’s a bit
of a problem because in this case you have to do something completely different and that’s what we’re
going to do right now.
So the approach is to allow the point factory to somehow access a private constructor, and the only
way point factory can access the private constructor is if Point Factory is in fact an inner class of
point.
So instead of being at the top level, we can put it here.
Okay, now I’m going to rename it a little bit because we already know that it’s part of a point, so
it doesn’t need point in its name.
So I’m going to rename it to just factory like so and well, we are essentially done.
So now we have a private constructor for the point, but we can use this constructor because we are
an inner class of point.
So we have those special privileges and as a result, instead of writing point Factory dot new polo
point, you simply say point dot factory dot new polo point.
Now the reason we’re able to do this is, of course because the factory is static and all of its members
are static.
If you don’t want them static, then that’s fine as well.
Just, you know, remove the static keyword here.
Call it something like Point Factory, for example, and then make some sort of public static property
so I can make a public static factory with factory without the point.
So I can have public static point factory called factory, which actually returns, let’s say, a newly
instantiated point factory whenever somebody wants one.
And this keeps the whole thing legal as well.
Although now we have this problem of being unable to access static methods in a non static content,
so then you would have to get rid of the static on the factory methods as well.
But this is an alternative solution.
If for some reason you want to instantiate a factory explicitly instead of just keeping everything static
and it’s completely okay to keep things static because, well, you’re just constructing objects, so
you’re not mutating any state.
If your point factory actually mutates state, which is kind of dubious design, to be honest, then
yes, you would have to get rid of static everywhere.
But the best approach is, of course, to just have just have a static class with static members.
That’s probably the simplest.
So public static class, you don’t need the point here if you’re exposing it directly.
And then you call point dot factory dot new polo point.
And this is in fact what the dot net framework does.
So when you have the task class, for example, you have task dot factory dot and then you have things
like start new.
So that’s how it works in the TPL, for example.
Now, one thing I wanted to mention, which isn’t really related to what we’re discussing right now,
and that is the idea of actually having not factory methods, but factory properties, because remember,
a property is just a combination of methods.
So for example, let’s suppose we want to have a point indicating the origin.
So there are different ways of indicating the origin of the coordinate system.
So one thing I could write is something like public static point origin, and I can have it return a
new point with zero zero or of course, the other alternative is I can have a singleton field of some
kind so I can have public static point origin equals new .00.
This actually highlights some of the confusion that C sharp can bring to the table because the above
having the arrow makes it a property.
So any time where you try to access the point origin, you’re going to be getting a new point with coordinates
zero zero and you would access it as follows.
So you would say var origin equals point dot origin.
And of course the other approach is that you just have a field and that field gets initialized once.
Of course this one is better, this one is better because you just initialize a static field once and
that field is available to be used everywhere that you want.
But if you do need to instantiate a new object, anytime somebody asks for something, then a property
is a good alternative to a factory method in actual fact.
So those two are pretty much interchangeable because a factory method and a property are really both
methods or properties, really just a combination of methods.
And here we’re just specifying the getter without the setter.
So this is an approach to yielding fixed values.
Play Play Play Play Play Play Stop Play Play Start Play