工厂方法和CDI:可分配和执行的重量级指令

发布于 2025-02-05 11:01:37 字数 1403 浏览 2 评论 0原文

我需要实现不知道必须创建哪个对象的工厂/建造者属性:

public interface Voter {
     boolean canVoteFor(Object);
     {ABSTAIN, GRANTED, DENIED} vote(Object)
}

public HeavyVoter implements Voter {
     @Inject
     SomeService someService1

     @Inject
     SomeService someService2

     someVeryHeavyProcessing1() { someService1() };
     someVeryHeavyProcessing2() { someVeryHeavyProcessing1(); 
               someService2();
               someOtherProcessing()}
     canVoteFor(Object) { someVeryHeavyProcessing1(); }
     {ABSTAIN, GRANTED, DENIED} vote(Object) { someVeryHeavyProcessing1(); }
}

public class VoterCounter { 
     Instance<Voter> voters;
     for (Voter voter: voters) if canVoteFor(Object) then vote(Object) ....
}

但是我对这种设计不满意,因为非常重的过程重复了两次(现在就忘记了缓存),并且强调了应用程序。

一种替代方法是将选民#canvotefor转换为选民#getVoter,#getVoter返回一个不使用对象作为参数的接口(构建对象

public interface VoterCreator {
    NewVoter getVoter(Object) {
}

public interface NewVoter {
     void assign(Object);
     boolean canVoteFor();
     {ABSTAIN, GRANTED, DENIED} vote()
}

public class VoterCounter { 
     Instance<VoterCreator> voters;
     for (Voter voter: voters) voter.assign(Object) ; if vote.canVoteFor() then vote() ....
}
    
}

,但是,只能因为“分配(对象)”而构建选民2。太多的复杂性,除了将对象伪“创建”分配给一个方法,

我很困惑,因为我需要一个工厂,它返回可以注入属性的对象,而无状态的bean也不是一个好选择。 。

哪个是更好的设计? J6EE环境。

I have a requirement to implement a Factory/Builder that does not know which object it has to create, so it delegates for each object to say if they are responsible for handling a parameter, this is a CDI enviroment, where Voter can have @Inject properties:

public interface Voter {
     boolean canVoteFor(Object);
     {ABSTAIN, GRANTED, DENIED} vote(Object)
}

public HeavyVoter implements Voter {
     @Inject
     SomeService someService1

     @Inject
     SomeService someService2

     someVeryHeavyProcessing1() { someService1() };
     someVeryHeavyProcessing2() { someVeryHeavyProcessing1(); 
               someService2();
               someOtherProcessing()}
     canVoteFor(Object) { someVeryHeavyProcessing1(); }
     {ABSTAIN, GRANTED, DENIED} vote(Object) { someVeryHeavyProcessing1(); }
}

public class VoterCounter { 
     Instance<Voter> voters;
     for (Voter voter: voters) if canVoteFor(Object) then vote(Object) ....
}

But I am not happy with this design because the very heavy process is repeated twice (forget about caching right now), and that stress the application.

One alternative would be to switch Voter#canVoteFor to Voter#getVoter, which returns a interface that does not use Object as parameter (the Object would be built

public interface VoterCreator {
    NewVoter getVoter(Object) {
}

public interface NewVoter {
     void assign(Object);
     boolean canVoteFor();
     {ABSTAIN, GRANTED, DENIED} vote()
}

public class VoterCounter { 
     Instance<VoterCreator> voters;
     for (Voter voter: voters) voter.assign(Object) ; if vote.canVoteFor() then vote() ....
}
    
}

However, Voter2 can only be built because of "assign(Object)". This looks like too much complexity, besides it assigns object pseudo "creation" to a method.

At the end I am confused because I need a factory, which returns objects that can have Injected properties, and stateless bean is not a good option because of the heavy processing.
.

Which would be a better design for this? J6EE environment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文