Learn about the Design Patterns

Tram Ho

Hello everyone, this article is an overview of Design Patterns, I am looking forward to your follow up.

1) What are Design Patterns?

  • Design patterns are a technique in object-oriented programming, which researchers draw and create standard designs.
  • Design pattern is not a specific programming language, but it can be used in most programming languages ​​that support OOP today.

2) Why use Design Pattern?

  • Design pattern provides solution in general form
  • Design pattern makes it easy to expand and speed up software development
  • Reusing design patterns helps avoid potential problems that can cause major errors, making it easy to upgrade and maintain later.
  • A great advantage to using a design pattern is that other programmers will be able to easily recognize it (especially if you use good naming conventions).

3) Classification Design Pattern

  • Basically, the design pattern will be divided into 3 main types:

Creational Design Pattern: (initialization group): This group will help you a lot in creating objects

  • Abstract FactoryProvides an interface for creating objects (related to each other) without specifying a class when or specifying a specific class (concrete) to create each object.
  • BuilderSeparate the construction of a complex object from its representation so that the same construction process can create different representations.
  • Factory MethodDefine Interface to create object but let subclass decide which class to use to generate Factory method object that allows a class to pass object initialization process to subclass
  • MultitonThe Multiton pattern is extended from the singleton pattern. If you notice it will detect right in the singleton pattern the contructor has absolutely no parameters in it. But in many cases, we need to initialize an object with certain configuration parameters, and it is because of this that multiton was born.
  • PoolProvides a technique for reusing objects instead of uncontrolled initialization
  • PrototypeSpecifies the type of objects to be created using a template object, created by copying this pattern object
  • Simple FactoryCreate objects without revealing how the object is created when used
  • SingletonMake sure a class has only one instance and provide a global access point to it

Strutural Design Pattern: (structure group) Patterns related to structure and structure of objects

  • Adapter / WrapperDue to compatibility issues, change the interface of one class to another that suits the user of the class
  • BridgeSeparate the semantics of a problem from installation; The purpose for both parts (semantics and settings) to vary independently
  • CompositeOrganize the objects according to the tree hierarchical structure; All objects in the structure are manipulated in the same homogeneous way. Create hierarchical relationships between objects. Clients can view objects that are included and included in the same way -> generalization in client code -> easy to develop, upgrade, maintain
  • DecoratorAssign more responsibilities to the object (extend functionality) at run time
  • FacadeProvide a homogeneous interface for a set of interfaces in a “subsystem”. It defines a higher interface than the available interfaces to make the subsystem easier to use
  • FlyweightUse sharing to work effectively on a large number of “small” objects (such as paragraphs, lines, columns, characters, etc.)
  • ProxyProvide an object on behalf of another object to assist or control the process of retrieving that object. The replacement object is called a proxy

Behavioral Design Pattern (behavior groups) Patterns that address object behavior problems

  • Chain Of Responsibilities
  • Command
  • Iterator
  • Mediator
  • Memento
  • Null Object
  • Observer
  • Specification
  • State
  • Strategy
  • Template Method
  • Visitor

4) Singleton Design Pattern

  • Singleton pattern belongs to Creational Design Pattern
  • Is a design pattern with a frequency of use quite a lot in programming
  • The Singleton Design Pattern ensures that a class has only one instance and provides the most common interface to access that class.
  • Use Singleton when we wantEnsure that there is only one instance of the class. Better access control because there is only one instance. You can manage the number of instances of a class within the specified limit.

For example, the Website needs an object connected to the database, but only one object for the entire application, using Singleton Pattern will solve this problem. To start you use a static property to ensure that only one instance of this class exists

The static property is shared between the objects of the class, so if there is already an instance of the class, all references to that class can use this property. Next create a method, this method will create an instance of the class if it does not already exist and return that instance.

  • The getInstance () function will check to see if $ _instance has a value of Null, it will create a new instance and assign it to the $ _instance property then return the newly created instance.
  • This class can be used as follows

Example of a Singleton pattern, creating a file for configuration settings

Conclude

  • Design patterns are a description, or a pattern, to solve a problem
  • Not the final design
  • Allows code to be optimized, easy to reuse. It is easy for others to capture your code. Easy to upgrade and repair
  • Help programmers can communicate with each other

Reference source

Share the news now

Source : Viblo