Let's talk about Design Patterns. Design Patterns are special structures applied to code base. These unique structures are efficient and elegant as long as developers use them in the fields that they are good at. Common Design Patterns and their use cases are arranged as follows:
Observer Pattern: The Observer Pattern allows objects (observers) to subscribe to a subject, so they are automatically notified of any state changes. Observer Pattern is used in products like message queues which has to broadcast messages to all the message subscribers.
Strategy Pattern: Strategy Pattern separates strategy code from certain client code, leading to convenient switch of strategy. Strategy Pattern makes strategy code run outside client (e.g., compiling to different executable files), which achieves low coupling. Programs like browsers which support plugin systems are using this model.
Template Pattern: This model encourages developers to extract abstract classes or interfaces so that we improve the reusability of code (often written in child classes or interface implementations). Java programmers are familiar with this model because Java uses abstract classes and interfaces everywhere.
Adapter Pattern: Sometimes we want classes with different interfaces to work together. Adapter Pattern defines adapter interface extracting the common "traits" of these classes. The abstraction is capable of calling methods of different classes by identical interface. The famous JDBC (Java Database Connectivity) is a demonstration of Adapter Pattern. Adapter Pattern is good at maintaining compatibility. No matter whether the implementation is new or old, as long as it implements the interface, the code will always work.
Decoration Pattern: This pattern prefers composition rather than inheritance. Each time the type needs a new function, we add new member fields rather than create child class to extend its responsibility. Rust enforces composition over inheritance at the language level, which significantly reduces the dependence complexity and is proved to be ideal for writing clear-minded code in practice.
Insight: Even though the benefits of these Design Patterns are tempting, experienced developers only apply them in suitable cases. The trade-off mindset must always be kept in mind.
Top comments (0)