Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET4
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 14-Mar-13 1:11am
Edited 14-Mar-13 1:21am
v4

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 14 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100