Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to build a class library project.
The structure of the project has to be something like:

Class_Library solution:
Objects(folder):
|_ Obj (class)

Classes(folder):
|_ ClassA (class)
|___ MethodA (creates object of type Objects.Obj,
populates it with data A and returns it )

|_ ClassB (class)
|___ MethodB (creates object of type Objects.Obj,
populates it with data B and returns it )

|_ ClassC (class)
|___ MethodC (creates object of type Objects.Obj,
populates it with data C and returns it )

|_ ClassD (class)
|___ MethodD (creates object of type Objects.Obj,
populates it with data D and returns it )


static FactoryF (public class. This is not under Classes directory.
| It lies at the same level as Objects & Classes folders.)
|___ MethodCreateClassObj (creates object of type objA, objB, objC or objD
of classes A, B, C, D respectively) based on parameter
passed to it and returns it. The returned object then
can be used to call Methods A, B, C or D.)


Only FactoryF should be able to create objects from classes A, B, C and D.
ie, when using this dll in another project, one should not be able to directly create objects of Class A,B,C,D, rather, one must always go through FactoryF.

FactoryF should return any ONE object ie. objA, objB, objC or objD (of classes A, B, C, D respectively) at a time.

Now, what I would like is, refer this dll in another project, say projP.
I call dll.FactoryF.MethodCreateClassObj() with an argument, and it returns one object based on the argument. say it returns objA.
then, I use objA.MethodA() to get a populated copy of Object.Obj.
But, I SHOULD NOT BE ABLE to directly create objects from projP like below:
Objects.Obj o = new Objects.Obj();
ClassA a = new ClassA(); a.MethodA();

My problem is the access modifier settings for the ClassA, ClassB... as well as for Objects.Obj and for MethodA, MethodB...

When I set Objects.Obj, Class A, B, C, D and Methods A, B, C, D to private, protected or internal,
then I cannot call Methods A, B, C, D even though I can call FactoryF and it returns an object of Class A/B/C/D successfully.

setting them public would allow direct creation of these objects...
Posted
Updated 14-Mar-13 0:21am
v4

1 solution

You should be coding to an Interface - have a look at this article Design Patterns Part 2 - The Factory Pattern[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900