开发者

Is there any design pattern that allow only a specific class to new a other specific class

开发者 https://www.devze.com 2023-02-28 11:47 出处:网络
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 chan

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消