亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Understanding Open Closed Principle and Depe

系統(tǒng) 1656 0

Introduction

In the following three articles I'm going to discuss SOLID.This is the Part One of the series.I'll try to explain Open Colsed Priciple and Dependency Inversion.

Now what does SOLID mean? SOLID is the OOD(Object-Orient Design) Principle,where each letter has its own mean

  • S-> Single responsibility
  • O-> Open Closed
  • L-> Liskov substitution
  • I-> Interface segregation
  • D-> Dependency Inversion

According to the Wikipedia the definition of SOLID is

" SOLID are guidelines that can be applied while working on software to remove code smells by causing the programmer to refactor the software's source code until it is both legible and extensible."

Using the code

Before start tec discuss I want to answer the below questions:

What is Open Closed Principle?

Answer:" software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification "

What is Dependency Inversion?

Answer:

  • High-level modules should not depend on low-level modules. Both should depend on abstraction.
  • Abstractions should not depend upon details. Details should depend upon abstractions.

Let's consider an example to make it better understand.Suppose I need a computer ,not decided yet whether I need desktop, laptop or other.

Suppose I go to a computer shop and ask for desktop computer.Let's convert my requirements into code.?

      
        using
      
      
         System;




      
      
        namespace
      
      
         SOLID

{

    
      
      
        public
      
      
        class
      
      
         Desktop

    {

        
      
      
        public
      
      
        string
      
      
         GetComputerDescription()

        {

            
      
      
        return
      
      
        "
      
      
        You get a desktop
      
      
        "
      
      
        ;

        }

    }



    
      
      
        public
      
      
        class
      
      
         Laptop

    {

        
      
      
        public
      
      
        string
      
      
         GetComputerDescription()

        {

            
      
      
        return
      
      
        "
      
      
        You get a laptop
      
      
        "
      
      
        ;

        }

    }



    
      
      
        public
      
      
        class
      
      
         ComputerShop

    {

        
      
      
        public
      
      
        enum
      
       ComputerType
      
        //
      
      
        Violation of OCP
      
      
                { 

            Desktop,Laptop

        }



        
      
      
        public
      
      
        string
      
      
         GetComputerDescripution(ComputerType type)

        {

            
      
      
        var
      
       myComp = 
      
        string
      
      
        .Empty;

            
      
      
        if
      
       (type == ComputerType.Desktop)
      
        //
      
      
        Violation of OCP
      
      
                    {

                myComp 
      
      = 
      
        new
      
       Desktop().GetComputerDescription();
      
        //
      
      
        Volation of DI
      
      
                    }

            
      
      
        if
      
       (type ==
      
         ComputerType.Laptop)

            {

                myComp 
      
      = 
      
        new
      
      
         Laptop().GetComputerDescription();

            }

            
      
      
        return
      
      
         myComp;

        }

    }



    
      
      
        class
      
      
         Program

    {

        
      
      
        static
      
      
        void
      
       Main(
      
        string
      
      
        [] args)

        {

            
      
      
        var
      
       computerShop = 
      
        new
      
      
         ComputerShop();

            
      
      
        string
      
       desc=
      
        computerShop.GetComputerDescripution(ComputerShop.ComputerType.Desktop);            

        }

    }

}
      
    

If you run the code it will execute fine and give you a?output "You get a desktop". So what's wrong? Here we are violating?OCP(Open Closed Principle) and DIP(Dependency Inversion Principle).

  • Closed for modification .(we did violation of OCP)
  • High-level modules should not depend on low-level modules.Both should depend on abstraction .(we did violation of DIP)

Still confuse? No problem I am explaining.

How we violate OCP ?

Yes we did it by creating instances of Desktop class and Laptop class on GetMyComputerDescripution method.Because if a new type computer come,then we need to modify the function as well as the class enum .

How we violate DIP?

Yes we did it by creating low-level object(Desktop,Laptop) on high-level object(Computer).So high-level module depends on low-level module(Desktop,Laptop) and no abstraction here.

Now I am adding a new type(Tablet) and modify my class by violation of OCP and DIP.

      
        public
      
      
        class
      
      
         ComputerShop

{

    
      
      
        public
      
      
        enum
      
      
         ComputerType

    {

        Laptop, Desktop, Tablet  
      
      
        //
      
      
         Violation of OCP
      
      
            }

 

    
      
      
        public
      
      
         ComputerShop()

    {

    }

    
      
      
        public
      
      
        string
      
      
         GetMyComputerDescription(ComputerType type)

    {

        
      
      
        var
      
       myComp = 
      
        string
      
      
        .Empty;

        
      
      
        if
      
       (ComputerType.Desktop ==
      
         type)

        {

            myComp 
      
      = 
      
        new
      
      
         Desktop().GetComputer();

        }

        
      
      
        else
      
      
        if
      
       (ComputerType.Laptop ==
      
         type)

        {

            myComp 
      
      = 
      
        new
      
      
         Laptop().GetComputer();

        }                                         
      
      
        //
      
      
         Violation of OCP
      
      
        else
      
      
        if
      
       (ComputerType.Tablet ==
      
         type)

        {

            myComp 
      
      = 
      
        new
      
      
         Tablet().GetComputer();

        }

        
      
      
        return
      
      
         myComp;

    }

}
      
    

?Did you notice the violation? Yes we modify the class as new type introduce.

Now let's follow the OCP and DIP rules and implement the new structure. If we can get the required computer without creating object of low-level class on Shop class ,then we can achieve our goal . Now we are going to introduce the abstraction.

      
        using
      
      
         System;




      
      
        namespace
      
      
         OCPDI

{

    
      
      
        public
      
      
        interface
      
      
         IComputer

    {

        
      
      
        string
      
      
         GetComputerDescripution();

    }



    
      
      
        public
      
      
        class
      
      
         Desktop:IComputer 

    {

        
      
      
        public
      
      
        string
      
      
         GetComputerDescripution()

        {

            
      
      
        return
      
      
        "
      
      
        You get a desktop
      
      
        "
      
      
        ;

        }

    }



    
      
      
        public
      
      
        class
      
      
         Laptop:IComputer 

    {

        
      
      
        public
      
      
        string
      
      
         GetComputerDescripution()

        {

            
      
      
        return
      
      
        "
      
      
        You get a laptop
      
      
        "
      
      
        ;

        }

    }

    
      
      
        //


      
      
        public
      
      
        class
      
      
         Tablet : IComputer

    {

        
      
      
        public
      
      
        string
      
      
         GetComputerDescripution()

        {

            
      
      
        return
      
      
        "
      
      
        You get a tablet
      
      
        "
      
      
        ;

        }

    }

    
      
      
        //
      
      
        --
      
      
        public
      
      
        class
      
      
         AnotherNewItem : IComputer

    {

        
      
      
        public
      
      
        string
      
      
         GetComputerDescripution()

        {

            
      
      
        return
      
      
        "
      
      
        Another new item
      
      
        "
      
      
        ;

        }

    }



    
      
      
        public
      
      
        class
      
      
         ComputerShop

    {

        
      
      
        public
      
      
        string
      
      
         GetMyComputerDescripution(IComputer computer)

        {

            
      
      
        var
      
       myComp =
      
         computer.GetComputerDescripution();

            
      
      
        return
      
      
         myComp;

        }

    }



    
      
      
        class
      
      
         Program

    {

        
      
      
        static
      
      
        void
      
       Main(
      
        string
      
      
        [] args)

        {

            
      
      
        var
      
       computerShop = 
      
        new
      
      
         ComputerShop();

            
      
      
        var
      
       desc=computerShop.GetMyComputerDescripution(
      
        new
      
      
         Laptop());



            Console.WriteLine(desc);

        }

    }

}
      
    

?We introduce a Interface(IComputer) to remove the dependency from ComputerShop , Desktop,Laptop,etc.

?Also now both high-level and low-level modules depend on abstraction and the abstracion does not depend on? details .So if the details changed ,they should not affect the abstracion(Satisfy DIP).

Now we extend our new item(Tablet , AnotherNewItem) without modify the ComputerShop class(Satisfy OCP).

Points of?Interest?

So the interface gives an extendability and remove dependency.Now whatever computer type comes ,ComputerShop class does not depend on any?lowe-level module.

      
        public
      
      
        class
      
      
         AnotherNewItem : IComputer

    {

        
      
      
        public
      
      
        string
      
      
         GetComputerDescripution()

        {

            
      
      
        return
      
      
        "
      
      
        Another new item
      
      
        "
      
      
        ;

        }

    }
      
    

It simply give the product we need from abstracion.

      
        public
      
      
        class
      
      
         ComputerShop

    {

        
      
      
        public
      
      
        string
      
      
         GetMyComputerDescripution(IComputer computer)

        {

            
      
      
        var
      
       myComp =
      
         computer.GetComputerDescripution();

            
      
      
        return
      
      
         myComp;

        }

    }
      
    

So now our code satisfy both OCP and DIP.

?

Understanding Open Closed Principle and Dependency Inversion


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 最新av | 久久久久国产午夜 | 色综合亚洲欧美在线 | 国产综合影院 | 国内精品久久久久久中文字幕 | 毛片视| 亚洲国产成人最新精品资源 | 欧美一级一极性活片免费观看 | 香蕉网伊| 国99久9在线 | 免费 | 爽爽影院在线免费观看 | 在线免费观看a视频 | 日韩亚洲视频 | 久久久免费观看 | 最新仑乱免费视频 | 国产精品免费视频一区一 | 天天干天天爽天天操 | 久久天天躁狠狠躁夜夜中文字幕 | 日韩欧美国产高清在线观看 | 韩国19禁青草福利视频在线 | 阳光灿烂的日子在线观看 | 免费一级毛片在线播放泰国 | 国产亚洲精品久久久久久久网站 | 国产精品一区二区资源 | 国产片91人成在线观看 | 精品日韩一区二区三区视频 | 午夜久久免费视频 | 综合婷婷丁香 | 精品国产欧美 | 亚洲宗合| 精品国产三级 | 久久99久久精品久久久久久 | 黄色a毛片 | 久久香蕉国产精品一区二区三 | 国内久久久久久久久久 | 四虎884| 9999精品视频 | 亚洲一级片在线观看 | 国产午夜精品一区二区三区 | 久久国产精品自由自在 | 欧美人成毛片在线播放 |