Some design patterns programmers should know

Tram Ho

As a programmer, our task is to solve the given problems. Many problems have been solved by other developers, so why do we need to solve them. We all don’t want to “reinvent the wheel.” Design pattern will help solve this problem. Let’s learn some design patterns that we should know through examples to better understand them.

1. Singleton

  • This is a design pattern that is quite popular. Lots of framwork using this pattern design. This pattern is used when we want to create an object from a class and want to make sure that only one object is created from it. Implement for this design pattern

A construtor is private to prevent access from outside. It is also necessary to create a static variable and the getInstance () method ensures that only one instance of the class is created.

2. Initialization On Demand Holder

This pattern is quite similar to the above Singleton, but it has an advantage over that when it comes to the thread, this pattern will help thread safe, the case of singleton if not in sync can create 2 different instances. For example, Initialization On Demand Holder is as follows:

True to the name of this pattern, it will not initialize the instance until the getInstance () method is called, with this advantage it helps thread safe.

3. Strategy and factory pattern

  • These two patterns are very popular, consider for example when they are combined

If you need a certain type of building, you only need to pass it on and it will return an object of that type or null if there is no instance for this type, it will be very useful in case of using polymorphism.

Design Pattern – Builder

Design Patterns – Object Pool

4. Chain of responsibility

When building an application with a lot of business logic, there are a lot of complex logic behind the implementation, this complexity can make code difficult to understand and hard to track, log bug … This pattern will help your code We can be broken down into sections and manage them step by step.

We have broken the code when implementing the method of interface Commands and separated its logic into one place. We can also reorder code if we want to make code decoupled more.

5. Builder

  • Many classes when creating an object need to pass many parameters, in which case when we use contructor or use get, set, it makes the code become quite tangled and lengthy. Builder pattern will help us solve this problem. Consider the example

6. Template method

This pattern is applied in case we have many common methods but differ in their behavior, this pattern is completely based on polymorphism.

7. State

  • Many objects have their own state. For example, the radio has two states: on (on) and off (off). We will represent it in object-oriented form

The example above may be simple but in the case of multiple states that can be very helpful, for example we can condition the state, a state is transferred when another state is executed, For example, in the above case, the on state is only executed when the off state is available or the exception is fired, we can also execute any operation that we want.

8. Conclusion

Above are the knowledge to learn some useful patterns through examples. Hopefully the article will be useful for everyone.

Reference

https://medium.com/educative/the-7-most-important-software-design-patterns-d60e546afb0e http://www.thedevpiece.com/design-patterns-that-every-developer-should-know/ https : //www.tutorialspoint.com/design_pattern/index.htm

Brief introduction about Design Patterns in Web development
Overview of Pattern Design Patterns
Share the news now

Source : viblo