OO Software Design Principles

后端 未结 8 587
梦毁少年i
梦毁少年i 2020-12-23 20:00

I am a huge fan of software design principles such as SOLID and DRY. What other principles exist for OO software design?

Note. I’m

相关标签:
8条回答
  • 2020-12-23 20:26

    Chose composition over inheritance, is one.

    Many people, especially those new to OO will start extending classes when all they really need to is to use composition. Really if you should ask your self, is the new class B a Class A? If not then you shouldn't extend.

    For example, let's say I have a Person Class, a Car Class, and I would like to make a new class called a DrivenCar class. A naive implementation would be to say (let's pretend we got multiple inheritance)

    class DrivenCar extends Person, Car  { ... }
    

    Is the DrivenCar a type of Person? No so it shouldn't be extending Person. Is the DrivenCar a Car? yes so it makes sense to extend

    Using composition the implmentation would look like

    class DrivenCar extends Car {
        private Person driver;
    }
    
    0 讨论(0)
  • 2020-12-23 20:27

    The GRASP patterns. Yes, they seem rather trivial. More like distillation down to core qualities that other, more involved patterns demonstrate.

    0 讨论(0)
  • 2020-12-23 20:33

    Interface. Most design patterns are based on separation of interface & implementation.

    0 讨论(0)
  • 2020-12-23 20:36

    KISS

    0 讨论(0)
  • 2020-12-23 20:41

    YAGNI

    0 讨论(0)
  • 2020-12-23 20:42

    When your API are expected to grow, use Abstract class instead of Interface. Adding a new method in Interface requires to change ALL the classes which implement it.

    0 讨论(0)
提交回复
热议问题