Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Design Question

Design Question

Scheduled Pinned Locked Moved C#
questiondesign
13 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P PozzaVecia

    suppose I have a class A, and the interfaces I1 and I2. Interfaces I1 contains methods a,b Interfaces I2 contains methods a,b,c and d class A can implements I1 or I2 depending on how it is constructed. Question, what is the best structure to organize it? one solution is to create a base class for Class A which implements only I1, and make Class A derived implementing I2: Class BaseA:I1 Class A: I2, BaseA Do you know any smarter solution?

    R Offline
    R Offline
    Rahul Rajat Singh
    wrote on last edited by
    #4

    Why not make I2 and abstract class so that when I2's methods are not implemented by ClassA the default empty implementation will work. here is some sample code: Note: Replace I2 with A2 and ClassA and ClassA2 are two possible versions you could have for ClassA

    public interface I1
    {
        void A();
        void B();
    }
    public abstract class A2 : I1
    {
        public virtual void C()
        {
            //nothing in default implementation
        }
        public virtual void D()
        {
            //nothing in default implementation
        }
    
        #region I1 Members
    
        public abstract void A();
        public abstract void B();
    
        #endregion
    }
    
    // case 1
    // fuction A and B will be there always
    // not implementing C and D
    public class ClassA : A2
    {
        public override void A()
        {
    
        }
    
        public override void B()
        {
    
        }
    }
    
    // case 2
    // fuction A and B will be there always
    // ALSO implementing C and D
    public class ClassA2 : A2
    {
        public override void A()
        {
    
        }
    
        public override void B()
        {
    
        }
    
        public override void C()
        {
    
        }
    
        public override void D()
        {
    
        }
    }
    

    Every now and then say, "What the Elephant." "What the Elephant" gives you freedom. Freedom brings opportunity. Opportunity makes your future.

    P 1 Reply Last reply
    0
    • P PozzaVecia

      suppose I have a class A, and the interfaces I1 and I2. Interfaces I1 contains methods a,b Interfaces I2 contains methods a,b,c and d class A can implements I1 or I2 depending on how it is constructed. Question, what is the best structure to organize it? one solution is to create a base class for Class A which implements only I1, and make Class A derived implementing I2: Class BaseA:I1 Class A: I2, BaseA Do you know any smarter solution?

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #5

      TheGermoz wrote:

      class A can implements I1 or I2 depending on how it is constructed

      This is a dead giveaway for poor design. Inheritance hierarchies are telling you what operations you can do on an object, and you should be able to tell from the type of something what you can do with it. That is, an instance of A should always be treatable as an I2, or never. What you almost certainly want to do is:

      interface I1 {
      void A();
      void B();
      }

      interface I2 : I1 {
      void C();
      void D();
      }

      class A : I1 {
      // Implementations of the I1 functionality, and the I1 type constructor
      public virtual void A() {}
      public virtual void B() {}
      }

      class A2 : A, I2 {
      // Implementations of the I2 functionality, and the I2 type constructor
      public void C() {}
      public void D() {}

      // You can also override implementations from A
      public override void A() {
      base.A();
      // other stuff
      }
      }

      P R 2 Replies Last reply
      0
      • R Rahul Rajat Singh

        Why not make I2 and abstract class so that when I2's methods are not implemented by ClassA the default empty implementation will work. here is some sample code: Note: Replace I2 with A2 and ClassA and ClassA2 are two possible versions you could have for ClassA

        public interface I1
        {
            void A();
            void B();
        }
        public abstract class A2 : I1
        {
            public virtual void C()
            {
                //nothing in default implementation
            }
            public virtual void D()
            {
                //nothing in default implementation
            }
        
            #region I1 Members
        
            public abstract void A();
            public abstract void B();
        
            #endregion
        }
        
        // case 1
        // fuction A and B will be there always
        // not implementing C and D
        public class ClassA : A2
        {
            public override void A()
            {
        
            }
        
            public override void B()
            {
        
            }
        }
        
        // case 2
        // fuction A and B will be there always
        // ALSO implementing C and D
        public class ClassA2 : A2
        {
            public override void A()
            {
        
            }
        
            public override void B()
            {
        
            }
        
            public override void C()
            {
        
            }
        
            public override void D()
            {
        
            }
        }
        

        Every now and then say, "What the Elephant." "What the Elephant" gives you freedom. Freedom brings opportunity. Opportunity makes your future.

        P Offline
        P Offline
        PozzaVecia
        wrote on last edited by
        #6

        Is there a plus in this design with respect my one?

        R 1 Reply Last reply
        0
        • B BobJanova

          TheGermoz wrote:

          class A can implements I1 or I2 depending on how it is constructed

          This is a dead giveaway for poor design. Inheritance hierarchies are telling you what operations you can do on an object, and you should be able to tell from the type of something what you can do with it. That is, an instance of A should always be treatable as an I2, or never. What you almost certainly want to do is:

          interface I1 {
          void A();
          void B();
          }

          interface I2 : I1 {
          void C();
          void D();
          }

          class A : I1 {
          // Implementations of the I1 functionality, and the I1 type constructor
          public virtual void A() {}
          public virtual void B() {}
          }

          class A2 : A, I2 {
          // Implementations of the I2 functionality, and the I2 type constructor
          public void C() {}
          public void D() {}

          // You can also override implementations from A
          public override void A() {
          base.A();
          // other stuff
          }
          }

          P Offline
          P Offline
          PozzaVecia
          wrote on last edited by
          #7

          yes this is my degign I propose, I was wandering if it is ok

          1 Reply Last reply
          0
          • P PozzaVecia

            Is there a plus in this design with respect my one?

            R Offline
            R Offline
            Rahul Rajat Singh
            wrote on last edited by
            #8

            Strictly talking from a designs standpoint - Yes there is. You see when we have an interface we are saying that we are defining a contract and all the classes implementing this interface should implement this contract i.e. methods. We cannot then say that we need to selectively implement the methods. The abstract class says that, I am providing a default implementation and the derived class is free to have his own IF it needs to. So in your case you needed some functions to be implemented selectively and some mandatory so following the design principle, I moved the mandatory ones in the contract i.e. the interface and the optional ones in abstract class. and in this particular case the default implementation of optional methods is to do nothing. I hope i am able to convey my thoughts clearly. Do let me know if not. I am also open to counter arguments as they will only enhance my learning. (counter arguments == brainstorming) i.e. always beneficial :)

            Every now and then say, "What the Elephant." "What the Elephant" gives you freedom. Freedom brings opportunity. Opportunity makes your future.

            1 Reply Last reply
            0
            • B BobJanova

              TheGermoz wrote:

              class A can implements I1 or I2 depending on how it is constructed

              This is a dead giveaway for poor design. Inheritance hierarchies are telling you what operations you can do on an object, and you should be able to tell from the type of something what you can do with it. That is, an instance of A should always be treatable as an I2, or never. What you almost certainly want to do is:

              interface I1 {
              void A();
              void B();
              }

              interface I2 : I1 {
              void C();
              void D();
              }

              class A : I1 {
              // Implementations of the I1 functionality, and the I1 type constructor
              public virtual void A() {}
              public virtual void B() {}
              }

              class A2 : A, I2 {
              // Implementations of the I2 functionality, and the I2 type constructor
              public void C() {}
              public void D() {}

              // You can also override implementations from A
              public override void A() {
              base.A();
              // other stuff
              }
              }

              R Offline
              R Offline
              Rahul Rajat Singh
              wrote on last edited by
              #9

              This is good. +5. P.S. We might do away with the interface containing the optional functions as these are not a part of the contract. Also we can make class A as abstract to make sure no one is able to create it directly as its only purpose is to facilitate default implementation for optional functions. I tried to do this design this way. I would love to hear your opinion on it: http://www.codeproject.com/Messages/4429095/Re-Design-Question.aspx[^]

              Every now and then say, "What the Elephant." "What the Elephant" gives you freedom. Freedom brings opportunity. Opportunity makes your future.

              B 1 Reply Last reply
              0
              • R Rahul Rajat Singh

                This is good. +5. P.S. We might do away with the interface containing the optional functions as these are not a part of the contract. Also we can make class A as abstract to make sure no one is able to create it directly as its only purpose is to facilitate default implementation for optional functions. I tried to do this design this way. I would love to hear your opinion on it: http://www.codeproject.com/Messages/4429095/Re-Design-Question.aspx[^]

                Every now and then say, "What the Elephant." "What the Elephant" gives you freedom. Freedom brings opportunity. Opportunity makes your future.

                B Offline
                B Offline
                BobJanova
                wrote on last edited by
                #10

                Since we don't know what the interfaces or the class 'A' represent we can't be sure what the contracts needed are. But as the question has two interfaces in it, it seems likely that he needs both, and publicly instantiable classes that implement each.

                Rahul Rajat Singh wrote:

                Also we can make class A as abstract to make sure no one is able to create it directly as its only purpose is to facilitate default implementation for optional functions.

                I don't think that's true. The original question suggests that it is possible to construct an A which is fully functional but only supports the I1 operations, and that it's also possible to construct an A which additionally implements I2. That means you need a concrete class and a concrete subclass.

                1 Reply Last reply
                0
                • P PozzaVecia

                  suppose I have a class A, and the interfaces I1 and I2. Interfaces I1 contains methods a,b Interfaces I2 contains methods a,b,c and d class A can implements I1 or I2 depending on how it is constructed. Question, what is the best structure to organize it? one solution is to create a base class for Class A which implements only I1, and make Class A derived implementing I2: Class BaseA:I1 Class A: I2, BaseA Do you know any smarter solution?

                  S Offline
                  S Offline
                  SledgeHammer01
                  wrote on last edited by
                  #11

                  Is there a reason why you are using interfaces? Too many .NET programmers make the mistake of thinking they need to use interfaces everywhere. Just like any other technology/construct/etc., you should only use interfaces when appropriate. Is anybody using your class external to your app? Based on the information you provided in your post, your design should be: class A { virtual void a(); virtual void b(); } class B : A { virtual void c(); virtual void d(); } why over complicate? KISS.

                  P 1 Reply Last reply
                  0
                  • P PozzaVecia

                    suppose I have a class A, and the interfaces I1 and I2. Interfaces I1 contains methods a,b Interfaces I2 contains methods a,b,c and d class A can implements I1 or I2 depending on how it is constructed. Question, what is the best structure to organize it? one solution is to create a base class for Class A which implements only I1, and make Class A derived implementing I2: Class BaseA:I1 Class A: I2, BaseA Do you know any smarter solution?

                    C Offline
                    C Offline
                    Clifford Nelson
                    wrote on last edited by
                    #12

                    Make interface I1 an abstract class instead (A1) or just create the class, whichever makes more sense, and inherit from A1 when you implement I2. Probably no reason to have the interface and the base class A1

                    1 Reply Last reply
                    0
                    • S SledgeHammer01

                      Is there a reason why you are using interfaces? Too many .NET programmers make the mistake of thinking they need to use interfaces everywhere. Just like any other technology/construct/etc., you should only use interfaces when appropriate. Is anybody using your class external to your app? Based on the information you provided in your post, your design should be: class A { virtual void a(); virtual void b(); } class B : A { virtual void c(); virtual void d(); } why over complicate? KISS.

                      P Offline
                      P Offline
                      PozzaVecia
                      wrote on last edited by
                      #13

                      Yes really there is a reason, the example was a very simplify version of may real problem. Anyway I appreciate your answer

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups