打印 上一主題 下一主題

Their modification and optimization

[複製鏈接]
跳轉到指定樓層
樓主
parvej64 發表於 2023-10-14 12:34:26 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
In this example, these would be the three categories Cars, Ships, and Aircraft: At first glance, you can see how much of a problem the construction of this interface poses, since it requires the implementation of the , and methods. As we all know a car can only drive so in and method it throws exception with appropriate message, class works the same way as boat can swim and exception must be thrown for other methods too. For airplanes, both methods are reasonable since airplanes can fly both on landing gear and in the air, but for the method, the exception must be thrown.
  
Such code violates the interface segregation principle because interfaces are not specialized and force implementation of methods that not all classes require. This interface is too generic and contains too many implementation possibilities. So how do we change photo retouching the code so that it meets the interface isolation principle while allowing cars to drive, ships to swim, and planes to drive and fly? Each of these activities should be separated into a separate interface so that an implementation can choose which interface it implements based on the required activity. So, let's create three appropriate interfaces.



Interfaces designed in this way are specialized and have terms by which we can determine what exactly they are responsible for. We have organized the interfaces correctly and now it is enough for each vehicle to implement only the functionality it really needs. So let's look at the improved implementation of the , and classes: Thanks to the interface isolation principle, our code becomes shorter and simpler. Types are interchangeable, even when a base type is used, its derived type can be used without changing the behavior of the first type. This issue will become clearer after an example.



回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

快速回復 返回頂部 返回列表
一粒米 | 中興米 | 論壇美工 | 設計 抗ddos | 天堂私服 | ddos | ddos | 防ddos | 防禦ddos | 防ddos主機 | 天堂美工 | 設計 防ddos主機 | 抗ddos主機 | 抗ddos | 抗ddos主機 | 抗攻擊論壇 | 天堂自動贊助 | 免費論壇 | 天堂私服 | 天堂123 | 台南清潔 | 天堂 | 天堂私服 | 免費論壇申請 | 抗ddos | 虛擬主機 | 實體主機 | vps | 網域註冊 | 抗攻擊遊戲主機 | ddos |