Question

suppose we have a class structure where the code is divided in two parts lets us say computer science and business, now this also further divides in terms of country also, say Indian (cs or MBA) and US (cs or MBA). now let us consider a scenario where i created classes like

1)Education class(parent class)

2) MBA class extends Education class

3) BS (cs) class extends Education class

now in terms of country also i made the classes

4) INDIA_BS class extends BS (cs) class

5)INDIA_MBA class extends MBA class

6) US_BS class extends BS (cs) class

7) US_MBA class extends MBA class

now let us say i write code where the country is set in the classes-method which are lowest in hierarchy (i.e country classes INDIA_BS,INDIA_MBA,US_BS,US_MBA)
but the logic is similar.I pass country name and it is set.

so my questions are
1) is it wise to put the common logic in parent classes(if i do that way) and calling that method from the child class which is lowest in hierarchy).

2) if this is wrong than what are the principles of OOPS that it violate

3) does it violate SOLID principle also if yes then how ?

4) is it decreasing coherence of the child class if i am putting the common code in parent class.

please be elaborate as possible. thanks

Was it helpful?

Solution

Your class diagram:

Your class diagram

i see x violations:

  1. Favor Composition Over Inheritance
  2. Program To An Interface, Not An Implementation
  3. Software Entities Should Be Open For Extension, Yet Closed For Modification
  4. etc

So, i would suggest you use Abstract Factory pattern. Code:

    class Test
    {
        static void Main(string[] args)
        {
            IEducationFactory india = new IndianEducation();
            IEducationFactory newYork = new USEducation();

            IDiplom d1 = india.Create_BSC();
            IDiplom d2 = newYork.Create_MBA();
        }
    }

    public interface IDiplom
    {
    }

    public interface IEducationFactory
    {
        IDiplom Create_MBA();
        IDiplom Create_BSC();
    }

    public class IndianEducation : IEducationFactory
    {
        public IDiplom Create_MBA()
        {
            throw new NotImplementedException();
        }

        public IDiplom Create_BSC()
        {
            throw new NotImplementedException();
        }
    }

    public class USEducation : IEducationFactory
    {
        public IDiplom Create_MBA()
        {
            throw new NotImplementedException();
        }

        public IDiplom Create_BSC()
        {
            throw new NotImplementedException();
        }
    }

And, your class diagram looks like:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top