[英]Java Cyclic Generics
當存在循環關系時,如何為一組類獲得類型安全性。 我有3個類,路由器,交互器和組件這樣
abstract class Router<C extends Component, I extends Interactor>
abstract class Interactor<R extends Router>
abstract class Component<I extends Interactor>
我想確保特定路由器綁定到特定組件和特定交互器。
編輯應用程序的體系結構可確保我們為1個組件的1個交互器准確配置1個路由器。 有些重用是可能的,但如果路由器A有交互器A和組件A,它將始終是那樣,否則我們將定義路由器B,例如交互器A和組件B.
編輯2一個更具體的例子:我們可以擁有登錄屏幕,其中包含loginscreenrouter,loginscreeninteractor和loginscreencomponent,然后是加載屏幕,該屏幕在同一結構中還有3個類。 但我們不想要的是開發人員意外地將loadingscreenintector傳遞給loginscreenrouter
每種類型都需要一個參數,包括每個類型本身。
abstract class Router<
R extends Router<R,C,I>, C extends Component<R,C,I>, I extends Interactor<R,C,I>
> { }
abstract class Interactor<
R extends Router<R,C,I>, C extends Component<R,C,I>, I extends Interactor<R,C,I>
> { }
abstract class Component<
R extends Router<R,C,I>, C extends Component<R,C,I>, I extends Interactor<R,C,I>
> { }
我想另一種方式,我以前沒有見過,是將所有的交互推送到一種類型。 角度較小,但感覺不是很OO,也許會導致更多的工作。
import java.util.Set;
interface System<R extends Router, C extends Component, I extends Interactor> {
Set<R> routers(I interactor);
Set<R> routers(C component);
Set<I> interactors(R router);
Set<I> interactors(C component);
Set<C> components(R router);
Set<C> components(I interactor);
}
abstract class Router {}
abstract class Interactor {}
abstract class Component { }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.