如何编写链接到其总工厂的子工厂(避免这些子工厂链接到另一个工厂)?
How write subfactories linked to their general factory (avoiding these subfactories to be linked to another factories)?
我使用 Spring 框架在 Java 中编程。
但是我有一个关于设计模式的问题。
我有一个 class X 有 3 个部分,3 个“大”部分:
一种
乙
C
class X {
private A a;
private B b;
private C c;
// getters and setters
}
我为此 class 创建了一个工厂,其中包含 3 个主要的私有方法来构造 X 类型对象的 3 个部分。
@Service
class XFactoryImpl implements XFactory {
@Override
X createX(InputData inputData) {
X x = new X();
x.setA(createA(inputData);
x.setB(createB(inputData);
x.setC(createC(inputData);
return x;
}
private A createA(InputData inputData) {
A a = new A();
// lot of code lines
return a;
}
private B createB(InputData inputData) {
B b = new B();
// lot of code lines
return b;
}
private C createC(InputData inputData) {
C c = new C();
// lot of code lines
return c;
}
}
问题如下:
因为 3 个私有方法 createA、createB 和 createC 是大方法,
工厂实现 class 非常大。
一个解决方案可能是创建 3 个“子工厂”并将这 3 个子工厂注入 class FactoryX,一个用于 class A 对象,一个用于 class B 对象,一个用于C 对象。
但是 class A 在 class X 之外没有任何意义:A 是 class X 对象的类型属性所必需的。
class B 和 class C 同上。
我 做 不 想要 to create a Factory for objects of class A without linking this factory 到 工厂 X class X , and especially forbid the 调用 of this factoryA outside from the factoryX (因为调用这个 FactoryA 不是为了创建 class X 的对象完全没有意义,没有意义,因为 A 仅在我的项目中是 class X 的一种属性类型)。
我想禁止从 FactoryX 外部注入 FactoryAImpl 实例 class 但我现在找不到解决方案。
If i create a factory for class A, 是 它 可能 到 link 它 绝对 with class X ?
如果 是, 如何 请 ?
If not, which design 模式 可以 i 使用 ?
提前谢谢你,
托马斯
如果要禁止FactoryA在FactoryX外实例化,封装成package-private即可。
class FactoryA{ // don't use public modifier so it only be accessed from the same package.
FactoryA(){ } // don't use public modifier, so it only be instantiated from class in the same package
}
确保将 FactoryA 和 FactoryX 放在同一个包目录中。
我使用 Spring 框架在 Java 中编程。 但是我有一个关于设计模式的问题。
我有一个 class X 有 3 个部分,3 个“大”部分: 一种 乙 C
class X {
private A a;
private B b;
private C c;
// getters and setters
}
我为此 class 创建了一个工厂,其中包含 3 个主要的私有方法来构造 X 类型对象的 3 个部分。
@Service
class XFactoryImpl implements XFactory {
@Override
X createX(InputData inputData) {
X x = new X();
x.setA(createA(inputData);
x.setB(createB(inputData);
x.setC(createC(inputData);
return x;
}
private A createA(InputData inputData) {
A a = new A();
// lot of code lines
return a;
}
private B createB(InputData inputData) {
B b = new B();
// lot of code lines
return b;
}
private C createC(InputData inputData) {
C c = new C();
// lot of code lines
return c;
}
}
问题如下: 因为 3 个私有方法 createA、createB 和 createC 是大方法, 工厂实现 class 非常大。
一个解决方案可能是创建 3 个“子工厂”并将这 3 个子工厂注入 class FactoryX,一个用于 class A 对象,一个用于 class B 对象,一个用于C 对象。
但是 class A 在 class X 之外没有任何意义:A 是 class X 对象的类型属性所必需的。 class B 和 class C 同上。
我 做 不 想要 to create a Factory for objects of class A without linking this factory 到 工厂 X class X , and especially forbid the 调用 of this factoryA outside from the factoryX (因为调用这个 FactoryA 不是为了创建 class X 的对象完全没有意义,没有意义,因为 A 仅在我的项目中是 class X 的一种属性类型)。
我想禁止从 FactoryX 外部注入 FactoryAImpl 实例 class 但我现在找不到解决方案。
If i create a factory for class A, 是 它 可能 到 link 它 绝对 with class X ? 如果 是, 如何 请 ? If not, which design 模式 可以 i 使用 ?
提前谢谢你,
托马斯
如果要禁止FactoryA在FactoryX外实例化,封装成package-private即可。
class FactoryA{ // don't use public modifier so it only be accessed from the same package.
FactoryA(){ } // don't use public modifier, so it only be instantiated from class in the same package
}
确保将 FactoryA 和 FactoryX 放在同一个包目录中。