简体   繁体   English

如何避免两个对象之间的交叉引用?

[英]How can I avoid a cross-reference between 2 Objects?

I have a DataAccess Class and it has a reference to a an interface. 我有一个DataAccess类,它有一个接口的引用。 The interface represents the caching strategy to use. 界面表示要使用的缓存策略。

I would like that the caching strategy has something like an initalize() method that fills the cache on startup. 我希望缓存策略有类似于initalize()方法,它在启动时填充缓存。 However that method would then require the DataAccess class to get access to the data and I end up with a cross-reference. 但是,该方法需要DataAccess类才能访问数据,最终得到交叉引用。

I also don't want to "pollute" the interface with setter methods because not all implementations would need it. 我也不想用setter方法“污染”接口,因为并非所有实现都需要它。 Also preferably no setter in DataAccess class as i want all fields to be final. 最好在DataAccess类中没有setter,因为我希望所有字段都是final。

I could create an additional layer that calls into dataaccess? 我可以创建一个调用dataaccess的附加层吗?

Other ideas? 其他想法?

You should wrap the DataAccess class with a Cache lookup class which only accesses the DataAccess class if it cannot get the data from its internal cache. 您应该使用Cache查找类包装DataAccess类,如果它无法从其内部缓存中获取数据,则只访问DataAccess类。 Like you've mentioned, this would consist of an extra layer, but depending on the way you implement it, it can be transparent to the application code (for example, by the use of AOP). 就像你提到的那样,这将包含一个额外的层,但是根据你实现它的方式,它可以对应用程序代码透明(例如,通过使用AOP)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM