Is there any design pattern that allow only a specific class to new a other specific class
without using Inner Class
i want only class A to have the right to new Class B but i don't want to use inner class, as there is a arra开发者_运维技巧y of class A in other class which i can not change it
There is another class holding the reference of Array of class B which will cause error while i change class B in to innerClass, as the namespace is changed. i can not change this class
If Class A and B are the only classes in the same package then making the constructor of Class B package private would achieve what you want.
The typical way to control construction is to make B's constructor private and add a static factory method, perhaps one that takes in an instance of A?
Changing B to an interface and having A create inner classes that implement B is another option.
精彩评论