我有一个案例,其中包含三个类,分别是SourceFactory、Source和SourceTypeI。我希望只在SourceFactory中创建SourceTypeI的实例。换句话说,在SourceFactory之外,其他类都不能创建SourceTypeI的实例。我该如何防止其他类可以创建SourceTypeI呢?
预期使用方式:
SourceFactory sF = new SourceFactory();
Source source = sF.createSource();
// from there, I should reach methods of SourceTypeI via source
source.whoIs();
类
|-------------| |-----------------------|
|SourceTypeI | |SourceFactory |
|-------------| |-----------------------|
|+whoIs():void| |+createSource():Source |
| | | |
|-------------| |-----------------------|
|-----------------------------|
| Source | <- Source cannot be instantiated, it is used just a
|-----------------------------| for referencing instance of SourceTypeI
| |
|-----------------------------|