java - How to combine State design pattern and Depedancy injection -


I'm trying to combine them in this Java 1.7 code (if possible): < Pre> view class {private state _state; } Intact Class State {View protected _view; } The class extends to the UserState state {} class extends the AdminState state {}

I have tried to understand them better by using these articles: and But I'm just more confused.

My questions are:

  • Can I inject some of the states in view class and avoid the state instance of?

  • What is there to get in my code?

    Thanks in advance.

    The problem is that you depend on injection injection for beginners. You are trying to inject circular dependency. I believe. Read that article, it does a better job than I am doing.

    However, in a nutshell: you are trying to do so (stolen image from the article):

      + --------- + - -------- + | A. & Lt; ----- | B | | | | | | ----- & gt; | | + --------- + + --------- +   

    Which class do you inject for the first time? If you are trying to create B , then you need a A but you a without b Can not do it further and even more.

    You can do an alternate solution like this:

      Public Sector A {Private Final BB; A (BB) {this.b = b; }} Public Sector B {Private / * No - Last * / AA; Public Zero Set A (AA) {this.a = A; }}   

    Better instead:

      + --------- + + --------- + | B | A. & Lt; ------------- | | | | | | | | + --- - | | | | --- & gt; | C. & Lt; ---- | | | | + --- + + --------- + --------- +   



Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -