Home Help Center

Servant inheritance

dashiedashie Member Maurizio MerliOrganization: Università degli Studi di MilanoProject: N/A
How can I implement inheritance with servants?
In my object hierachy child servant have to do many BASE operations (the same operations) of the BASE interface.

Comments

  • michimichi Member Michi HenningOrganization: Triodia TechnologiesProject: I have a passing interest in Ice :-) ✭✭✭
    Have a look at the documentation. It explains how to implement inheritance. Basically, the base servant inherits from the base skeleton, and the derived servant inherits from the derived skeleton. You can make the derived servant also inherit from the base servant, in which case the derived servant simply inherits all the base operations from the base servant (and can override those implementations if need be).

    Alternatively, if you want to reimplement the derived operations, have the derived inherit only inherit from the derived skeleton (but not the base servant) and then implement both derived and base operations in the derived servant explicitly.

    Cheers,

    Michi.
  • dashiedashie Member Maurizio MerliOrganization: Università degli Studi di MilanoProject: N/A
    Can I have a java semple with this servant hierarchy?

    principal

    user extend aprincipal

    gourp extends principal


    IN java

    principal has its own servant: principalI

    then

    user servant: userI extends principalI but the other methods of a user ????????

    and group: ... ????
  • michimichi Member Michi HenningOrganization: Triodia TechnologiesProject: I have a passing interest in Ice :-) ✭✭✭
    Given
    interface Principal {
        void opPrincipal();
    };
    
    interface User extends Principal {
        void opUser();
    };
    
    interface Group extends Principal {
        void opGroup();
    };
    
    you would do something like this in Java:
    class PrincipalI extends _PrincipalDisp
    {
        public void opPrincipal(Ice.Current c)
        {
            // ...
        }
    }
    
    class UserI extends _UserDisp
    {
        public void opPrincipal()
        {
            // ...
        }
    
        public void opUser(Ice.Current c)
        {
            // ...
        }
    }
    
    The implementation of Group would be analogous. If you want to avoid reimplementing opPrincipal() in UserI and delegate to a common implementation instead, you need to move the operation implementations of Principal into a class of their own, and then have instances of PrincipalI as well as UserI delegate to that class. Something along the lines of:
    class PrincipalOperationsI implements PrincipalOperations
    {
        public void opPrincipal(Ice.Current c)
        {
            // ...
        }
    
        // Any other state required by the servant goes here...
    }
    
    class PrincipalI extends _PrincipalDisp
    {
        public PrincipalI()
        {
            _ops = new PrincipalOperationsI();
        }
    
        public void opPrincipal(Ice.Current c)
        {
            _ops.opPrincipal(c);
        }
    
        private PrincipalOperationsI _ops;
    }
    
    class UserI extends _UserDisp
    {
        public UserI()
        {
            _baseOps = new PrincipalOperationsI();
        }
    
        public void opPrincipal(Ice.Current c)
        {
            _baseOps.opPrincipal(c);
        }
    
        public void opUser(Ice.Current c)
        {
            // ...
        }
    
        private PrincipalOperationsI _baseOps;
    }
    

    Cheers,

    Michi.
  • dashiedashie Member Maurizio MerliOrganization: Università degli Studi di MilanoProject: N/A
    And in you opinion it's a good thing to write code in this way or it's better rewrite method in all sub-interfaces? What you can suggest me?

    However: VERY VERY thanks :))
  • michimichi Member Michi HenningOrganization: Triodia TechnologiesProject: I have a passing interest in Ice :-) ✭✭✭
    All this really is necessary only because Java doesn't support multiple inheritance. Personally, I would move all the operation implementations into their own class, and then delegate to that class from the servants. That way, you don't have duplicated code everwhere.

    I think the answer to this thread also answers the question you posted about server private methods. The same technique can be used to avoid losing polymorphism in that case.

    Cheers,

    Michi.
  • dashiedashie Member Maurizio MerliOrganization: Università degli Studi di MilanoProject: N/A
    First af all: HAPPY CRHISTMAS!!!
    And now... I think very much about your suggest but I think that there is always a problem.
    I'm implmenting an application where I can't you use your Freeze Evictor, becouse for every operation I have to watch permission, locks, updates and read/write all this things in an object DB.
    I have to do all these operation for every method because every method has his different policy end behavior.
    However the points in my mind are two:
    1) It will be very very simple for you generate servant classes with inheritance and I cant't understand why don't you do. You generate code and for you it will be easy.
    2) Why proxy don't implements Interfaces that I define in slice file?
  • michimichi Member Michi HenningOrganization: Triodia TechnologiesProject: I have a passing interest in Ice :-) ✭✭✭
    Originally posted by dashie
    And now... I think very much about your suggest but I think that there is always a problem.
    I'm implmenting an application where I can't you use your Freeze Evictor, becouse for every operation I have to watch permission, locks, updates and read/write all this things in an object DB.
    I have to do all these operation for every method because every method has his different policy end behavior.

    Well, if you are in the situation where you want to perform the same actions on entry and exit for every operation, you can use a servant locator and execute that code from within the servant locator.
    1) It will be very very simple for you generate servant classes with inheritance and I cant't understand why don't you do. You generate code and for you it will be easy.

    Have you tried the --impl option with slice2java? It generates empty servant implementations for which you then fill in the code. That might make things easier for you.
    2) Why proxy don't implements Interfaces that I define in slice file?

    I'm sorry, but I don't understand what you mean. Can you explain this a bit more? (An example would help.)

    Cheers,

    Michi.
  • dashiedashie Member Maurizio MerliOrganization: Università degli Studi di MilanoProject: N/A
    I'm in late with reply because i have prefered working more with ICE before other question!
    What I'm asking for is the possibility to use inheritance at Servant level.
    For example if I have:
    - FileSystemObject
    - File
    - Folder

    I need to do 3 Servant: FileSystemObjectI, FileI and FolderI.
    Why when I implement FileI and FolderI you don't use a mechanism that permit user to implement FileI extending FileSystemObjectI?
  • marcmarc FloridaAdministrators, ZeroC Staff Marc LaukienOrganization: ZeroC, Inc.Project: The Internet Communications Engine ZeroC Staff
    Are you using Java or C++? If you use Java, then the problem is that you do not have multiple inheritance available. Since servants already inherit from the skeleton class, they cannot inherit from other servant base classes. Therefore we use the delegation approach in such cases. For C++, you can have the servants inherit from both the skeleton class and the other servant base classes.
  • dashiedashie Member Maurizio MerliOrganization: Università degli Studi di MilanoProject: N/A
    You are right but you can generate Servant and Skeleton able to implement delegation in a transparent way. Or no?
    Imagine that I register MY OWN CLASS that implements _InterfaceOperation to a standard servant generate from slice2java and that delegate all method. Then when you use a servent you create it with for example

    File extends FileSystemObject implements _FileOperation
    FileI is the Servant that you generate automatically

    adapter.add(id, FileI file = new FileI(new File()) )

    What do you think about it!? It will be very very easy!

    Sorry for my insistence BUT now that I know ICE I love it very much :)) and I'm very happy to see it always better and easy.
  • mesmes CaliforniaAdministrators, ZeroC Staff Mark SpruiellOrganization: ZeroC, Inc.Project: Ice Developer ZeroC Staff
    The slice2java translator already supports this. See Section 12.7 of the Ice manual for more information on "tie" classes.

    - Mark
  • dashiedashie Member Maurizio MerliOrganization: Università degli Studi di MilanoProject: N/A
    I like ICE :)))
Sign In or Register to comment.