Link to home
Start Free TrialLog in
Avatar of krazykoder
krazykoder

asked on

GRASP Creator patern question

Hello,

I have a question about the GRASP creator pattern that is bothering me.
In a sample i have seen, class A aggregates class B and thus creates class B.
But class B had a public constructor, so my question is what prevents class B from being created by a different class?  anything?

For some reason i feel like class B should only be able to be created by class A, but i dont even think that is is possible is it?  Class B's constructor need to be public so class A can creat it.

Say you want to have a class that is public, but only want 1 class to be able to create it, can you do this?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Then, in another assembly this would not work:


        public Class1()
        {
            A a = new A();
            B b = new B();
        }

Open in new window

But this would. Also, on the first code snippet you need to add "public" before the class keyword. I forgot it :)

        public Class1()
        {
            A a = new A();
            B b = a.GetB();
        }

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of krazykoder
krazykoder

ASKER

ok - thnaks.

I was comparing it to the DataRow class which i cant instantiate without calling NewRow of a data table.  But I am using that namespace extrenally.
I guess DataTable is public and DataRow is internal, therefore i can of cource created a DataTable, but not a DataRow directly.

But in my circumstance i was creating the classes in a namespace that other developers would be working in as well from within the same project, and i wanted to prevent them from creating the class directly.  I guess i can either document the intent or maybe i can try to create my classes in a nested namespace that they wouldnt be working in.

But either way - I understand better now.
Thanks!