Results 1 to 4 of 4

Thread: What is the best architecture for this case?

  1. #1
    Join Date
    Aug 2011
    Posts
    7

    Default What is the best architecture for this case?

    I have a controller C. In that controller i injected a Service class S.
    like:

    class C extends SimpleFormController{
    private S service;

    onSubmit{
    service.doSomeOperation();
    service.anotherOperation();

    }

    }

    NOw i have got a requirement that, depending on a condition at the the time of context startup, i have to call another implementation of the service.anotherOperation().

    So nw i have created an interface and Service S implements it. And also created one more Service P where i have changed the implementation of anotherOperation().

    Now My question is : for the remaining methods whose implemenation is same as that of class S, should i make P as subclass of S as well.
    LIke:

    Interface A;
    S implements A;
    P extends S implements A;
    class C{
    private A a;
    }
    ------OR--------
    class S;
    P extends S;
    class C{
    private S s;
    }

    Which design is the best ?

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Hello

    Use code tags, is more readable for us

    Code:
    class S;
    P extends S;
    class C{
    private S s;
    }
    Absolutely not, Spring work with Interfaces since it works with the Proxy Pattern and furthermore interfaces offer a level of loose coupling

    Code:
    Interface A;
    S implements A;
    P extends S implements A;
    class C{
    private A a;
    }
    I think is fine, but you must consider for this situation use @Service("p") and @Service("s") for each class implementation, therefore when you inject the interface into the controller with @Autowired you must now use and add the @Qualifier("p")

    Let me know your advance
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    Aug 2011
    Posts
    7

    Default

    Hi Dr pompeii,

    I had also decided to go with the design you suggested. But my confusion is:

    P is extending S to share common implemenation. But doesnt it then conflict with the spring's advice of loose coupling. Should we not separate the common implemenation into some another class and inject that in P and S? So that it looks like this:

    Design 2


    Code:
    Interface A;
    S implements A;
    P implements A;
    class C{
    private A a;
    }
    In terms of this, when and how to decide what design we should go for? And why we should go which design?
    Plz put some expert light on this?

  4. #4
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    P is extending S to share common implemenation
    With such context, is correct

    Should we not separate the common implemenation into some another class and inject that in P and S
    If is common not, it is used when is uncommon for example you have a interface DAO and you have 3 implementations of such interface, one with Hibernate, other with JPA and the last with MyBatis (known as Ibatis), with my previous explanation

    Code:
    Interface A;
    S implements A;
    P implements A;
    class C{
    private A a;
    }
    YOur code has more sense now

    In terms of this, when and how to decide what design we should go for? And why we should go which design?
    It depends if has common methods inplementations or not

    HTH
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •