locked
Architecture RRS feed

  • Question


  • Hi,

    would like to know about the best strategy to implement the following requirement...In example I have gone for inheritance, but don't know whether its possible to do like that.

     public class A : IDisposable
     {
        SqlConnection sqlConnection = null;
    
        public void Dispose()
        {
          Dispose(true);
          GC.SuppressFinalize(this);
        }
     }
    protected void A1()
    {
         .....
    }
    protected void A2()
    {
         .....
    }
    
    protected void A3 ()
    {
          .....
    }
    }
    
     public class B : A
    {
    	// need access to A1 but not to A2 and A3
    }
    
     public class C : A
    {
    	// need access to A1 and A2 but not to A3
       
    }
    
    public class D : A
    {
    	// need access to A3 but not to A1 and A2   
    }

    Thank you for your suggestions :-)

    Friday, July 3, 2015 7:35 AM

Answers

  • got it....thanks for the reply :-)
    • Marked as answer by swingme Monday, July 6, 2015 11:24 AM
    Monday, July 6, 2015 11:24 AM

All replies

  • Please define "need access to" and "not to".

    As your classes B to D are derived from A, they can all access the protected methods A1 to A3.

    When it means, that derived classes must not access those methods, then you need three intermediate derived classes and make A internal. And your classes B to D must be external.

    E.g.

    namespace ClassLibrary
    {
        using System;
    
        internal class A
        {
            internal void A1() { Console.WriteLine("A1"); }
            internal void A2() { Console.WriteLine("A2"); }
            internal void A3() { Console.WriteLine("A3"); }
        }
    
        public interface IB { void A1(); }
        public interface IC { void A2(); }
        public interface ID { void A3(); }
    
        public class IntermediateB : IB { public void A1() { A a = new A(); a.A1(); } }
        public class IntermediateC : IC { public void A2() { A a = new A(); a.A3(); } }
        public class IntermediateD : ID { public void A3() { A a = new A(); a.A3(); } }
    }

    But I don't see the sense behind this.

    Friday, July 3, 2015 8:01 AM
  • Hallo Stefan,

    thanks for your reply.

    with access I mean callable from the respective classes. will try to explain my req..

    as I mentioned I used inheritance in my example but may be the wrong way to go.

    the need is to have a class with some methods...of which a few available to "derived" classes or whatever you say and few not. but some should be available for all derived classes. hope you understand my requirement.

    :-)

    Friday, July 3, 2015 8:12 AM
  • Sounds like a bad OO design. Stefan's solutions is the "best" for what you've asked for but it doesn't sound like a good practice. It's a bit like you want multi-inheritance. E.g. consider two bases classes Animal and Car.

    Animal.Talk(). Car.Drive(). Typically you want to model some things that are an animal and some that are a car. What you're asking for is a Transformer, part animal part car. But a Transformer is neither an animal nor a car, so it shouldn't derive from those base classes ;)


    http://pauliom.wordpress.com

    Friday, July 3, 2015 10:51 AM
  • A derived class can always call the methods of its ancestor.

    Seems that Paul (?) is right. You should examine whether you really need inheritance and not composition instead of it.

    Friday, July 3, 2015 1:27 PM
  • got it....thanks for the reply :-)
    • Marked as answer by swingme Monday, July 6, 2015 11:24 AM
    Monday, July 6, 2015 11:24 AM