Learn C#.NET, ASP.NET MVC 5,ASP.NET Core that you can increase your knowledge and coding to develop the real-time project.


Tuesday, September 18, 2018

What is SOLID ? Why should we use SOLID design Principles

In this article, we will learn what is solid? Why should we use solid design principle?.

What is SOLID? Why should we use SOLID principle?

What is SOLID:

SOLID is basically design principle. which is used to create good software Architecture to develop a good software.
S.O.L.I.D is an acronym for the first five object-oriented design(OOD) principles by Robert C. Martin, popularly known as Uncle Bob.

What is meant by good software?



What is meant by good software?

The software has a good structure that is flexible, maintainable, and reusable.
These principles, when combined together, make it easy for a programmer to develop software that is easy to maintain and extend.

Why should we use SOLID design Principles? 

 To avoid the symptoms of poor design, or design smells.

Symptoms of poor design, or design smells:



• Rigidity: (The design is difficult to change)
A design is rigid if a single change causes a cascade of subsequent changes in independent modules. The more modules that must be changed, the more rigid the design.

• Fragility: (The design is easy to break)
Fragility is the tendency of a program to break in many places when a single change is made.

• Immobility:
The design is difficult to reusable.

• Viscosity: (The design is difficult to do the right thing.)
It is easy to do the wrong thing but difficult to do the right thing.

• Overdesign
• Needless repetition:
When the same code appears over and over again, in slightly different forms, the developers are missing an abstraction. Finding all the repetition and eliminating it with an appropriate abstraction
may not be high on their priority list, but it would go a long way toward making the system easier to understand and maintain.
When there is redundant code in the system, the job of changing the system can become difficult.
Bugs found in such a repeating unit have to be fixed in every repetition. However, since each repetition is slightly different from every other, the fix is not always the same.

An advantage of the SOLID principle or How do we know how whether the design of a software system is good?

• The design is easy to change.
• The design is easy to Flexible and Extensible
• The design is easy to Testable
• The design is easy to Reusable
• The design is easy to avoid the needless repetition
• The design is easy to do the right thing

The principles are there to help us eliminate bad smells. They are not a perfume to be liberally scattered all over the system. Over-conformance to the principles leads to the design smell of needless complexity.

S.O.L.I.D Stand for:



S: Stand for Single Responsibility Principle (SRP)
O: Stand for Open Close Principle (OCP)
L: Stand for Stand for Liscko Substitution Principle (LSP)
I: Stand for Interface Segregation Principle (ISP)
D: Dependency Inversion Principle (DIP)
We will go with one by one principle in detail with the example of the solid principle in the next article of this series.
I hope you understood the basic knowledge of what SOLID Is and Why should we use and advantages of using it.
If you like my article then please add your valuable comment on below comment box that will encourage me a lot to writing more and more articles.
Share:

Wednesday, September 5, 2018

What is Diamond Problem in C#

In this blog, we will learn what is Diamond problem in c# and We will learn Diamond problem in c# with a simple example.

Diamond Problem in C#

Prerequisites:

Why c# does not supports multiple inheritances?

One of the main reason behind this is the “diamond problem.

What is the Diamond Problem:


The "diamond problem" is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which class of the method does D inherit: that of B, or that of C? So this is an ambiguity problem in multiple inheritances in c#. So that c# does not support multiple inheritances. It also called an ambiguity problem in c#.


diamond problem in c#

Example 1: 

This is a simple example to understand the concepts of the diamond problem in c#.

First of all, we will check when does the diamond problem will arise with programmatically.
We will be creating class A, class B, class C, class D having Print() method in the base class. The class B and C inherits from class A. and class D inherit from class B and Class C as follow.


using System;

namespace CSharpConsoleApp.DiamondProblemExample
{
   public class A
    {
        public virtual void Print()
        {
            Console.WriteLine("Print method of class A.");
        }
    }

    public class BA
    {
        public override void Print()
        {
            Console.WriteLine("Print method of class B");
        }
    }

    public class CA
    {
        public override void Print()
        {
            Console.WriteLine("Print method of class C");
        }
    }

    // Error: D class can not have multiple base classes.
    public class DC, B
    {
        
    }

    class DiamondProblemExample
    {
        static void Main(string[] args)
        {
            D obj = new D();
            obj.Print();
        }
    }
}


How can we resolve the diamond problem in c#?

We can resolve the diamond problem by using Interface in c#.


Example 2:
using System;

namespace CSharpConsoleApp.DiamondProblemExample
{
    interface IA
    {
        void PrintIA();
    }
    interface IB
    {
        void PrintIB();
    }
    interface IC
    {
        void PrintIC();
    }
   public class A : IA
    {
        public void PrintIA()
        {
            Console.WriteLine("PrintIA method from class A.");
        }
    }

    public class B:IB
    {
        public void PrintIB()
        {
            Console.WriteLine("PrintIB method from class B.");
        }
    }

    public class C:IC
    {
        public void PrintIC()
        {
            Console.WriteLine("PrintIC method from class C.");
        }
    }
    
    public class D: IA, IB,IC
    {
        public void PrintIA()
        {
            Console.WriteLine("PrintIA method from class D.");
        }
        public void PrintIB()
        {
            Console.WriteLine("PrintIB method from class D.");
        }

        public void PrintIC()
        {
            Console.WriteLine("PrintIC method from class D.");
        }
    }

    class DiamondProblemExample
    {
        static void Main(string[] args)
        {
            D obj = new D();
            obj.PrintIA();
            obj.PrintIB();
            obj.PrintIC();
            Console.ReadLine();
        }
    }
}



OutPut:


Diamond Problem in C#

Real Time Diamond Problem in C# Example: 


diamond problem in c#


Real Time Diamond Problem in C# Solution: 



diamond problem in c#


A real-time example of resolving a diamond problem by using Interface in c#. 

using System;

namespace CSharpConApp.DiamondProblem1
{
    interface IMother
    {
        void Loan();
    }
    interface IFather
    {
        void Loan();
    }
    public class Mother:IMother
    {
        public void Loan()
        {
            Console.WriteLine("Loan taken by mother.");
        }
    }
    public class Father:IFather
    {
        public void Loan()
        {
            Console.WriteLine("Loan taken by father");
        }
    }
    public class Child:IMother,IFather
    {
        void IMother.Loan()
        {            
            Console.WriteLine("Mother's loan paid by child");
        }

        void IFather.Loan()
        {
            Console.WriteLine("Father's loan paid by child");
        }
    }

   public class DiamondProblem1
    {
        static void Main(string[] args)
        {
            Child child = new Child();
            ((IMother)child).Loan();
            ((IFather)child).Loan();

            Console.ReadLine();
        }
    }
}



Output:
Diamond Problem in C#

To understand the concepts of Implicit and Explicit interface visit the following link.


I hope you understood the concept of the diamond problem in C#.
Share:

Upcoming Articles/Videos

Design Pattern
SOLID Design Principles
Copyright © Programming With Shri | Powered by Shrimant Telgave Home | Disclaimer | Privacy Policy | Terms and Conditions Design by Shrimant Telgave