Design Principles [closed]

2019-01-30 07:07发布

What principles do you generally follow when doing class design?

12条回答
Root(大扎)
2楼-- · 2019-01-30 07:31

Principles Of Object Oriented Class Design (the "SOLID" principles)

  • SRP: The Single Responsibility Principle A class should have one, and only one, reason to change.
  • OCP: The Open Closed Principle You should be able to extend a classes behavior, without modifying it.
  • LSP: The Liskov Substitution Principle Derived classes must be substitutable for their base classes.
  • ISP: The Interface Segregation Principle Make fine grained interfaces that are client specific.
  • DIP: The Dependency Inversion Principle Depend on abstractions, not on concretions.

Source: http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

查看更多
时光不老,我们不散
3楼-- · 2019-01-30 07:32

Don't forget the Law of Demeter.

查看更多
闹够了就滚
4楼-- · 2019-01-30 07:33

Basically I get away with programming to interfaces. I try to encapsulate that which changes through cases to avoid code duplication and to isolate code into managable (for my brain) chunks. Later, if I need, I can then refactor the code quite easily.

查看更多
劫难
5楼-- · 2019-01-30 07:38

Domain Driven Design is generally a good principle to follow.

查看更多
甜甜的少女心
6楼-- · 2019-01-30 07:39

A thing which I would like to add to all this is layering, Define layers in your application, the overall responsibility of a layer, they way two layers will interact. Only classes which have the same responsibility as that of the layer should be allowed in that layer. Doing this resolves a lot of chaos, ensures exceptions are handled appropriately, and it makes sure that new developers know where to place their code.

Another way to design is by designing your class to be configurable creating a mechanism where the configuration can be plugged in your class, rather than overriding methods in sub classes, identify what changes, see if that can be made configurable and ensures that this functionality is derived from configurations

查看更多
Bombasti
7楼-- · 2019-01-30 07:40

The most fundamental design pattern should be KISS (keep it simple stupid) Which means that sometimes not using classes for some elements at all it the right solution.

That and CRC(Class, Responsibility, Collaborators) cards (write the card down in your header files, not on actual cards that way they because easy to understand documentation too)

查看更多
登录 后发表回答