Basic types of design patterns

Tram Ho

What is Design Pattern (DP)?

As a developer, we have certainly been and will have difficult issues that need to be addressed. But what if that problem was already solved by others? Instead of taking time to rethink the solution from the beginning but not necessarily optimal, we should reuse the methods that have been solved, widely recognized by the developer in the community.

Design patterns are basically generic designs for a software solution. It will be a framework, offering optimal solutions for a specific problem.

Reason for Design Pattern

A question every developer has to ask before using DP: Is it really important? Wouldn’t we be able to code without it?

The answer is simple: No DP still has normal code, but it is really important. There are several main reasons as follows:

  • Save time: are you willing to spend a lot of time thinking about how to solve problems when the answers are available and proven to be effective through many other developers? Ok, you can say you are ready to research again, but maybe your boss will say no
  • DP is widely known: used and recognized throughout the dev community worldwide. It is not difficult to understand when the solution you choose by DP is highly effective, as well as you can easily communicate with other people because there is a tool that is no different from a “common language” at hand.
  • Most of the solutions offered by DP are fairly simple and effective. While we decided to do it in a different way, it might not have been so good.

Basic types of patterns

There are about 26 types of DP, grouped into three main categories:

  1. Creational : The dp belongs to this group are all for the purpose of class creation. More specifically, it will be divided into ” class-creation patterns ” and ” object-creational patterns “. class-creation patterns will use inheritance effectively during initialization. Object-creational patterns use a delegation model.
  2. Structural : Dp of this group is designed in relation to the structure and components of the class. The goal is to increase the functionality of the related classes without damaging many of its existing components.
  3. Behavioral : Dp of this group is designed based on the interoperability and communication of a class with other components.

For example

Singleton (Creational group)

This is a fairly common DP, applied even in frameworks or libs. With Android, we will encounter quite a lot when using Dagger to inject objects. Or when interacting with the Java Calendar object, we cannot instantiate an instance of it, we must call getInstance ().

A class using singleton will have the following components:

  • The static private variable holds a single instance of the class.
  • Private constructor, preventing creation from other places
  • Public static method to return the single instance above.

Singleton – Eager

Single instance is initialized during load class.

Singleton – Lazy

The only difference, with lazy, only when we explicitly call the get function, is a single-instance initialized.

Singleton – Thread-safe

What happens when more than one thread accesses the singleton class at the same time? At that time, check null is true, so 2 new instances will be created.

The synchronization class allows to solve the above problem. However, we will have to trade back with performance.

Proxy (Structural group)

Proxy belongs to Structural group. The purpose of building a proxy pattern is to create an object that will authorize instead of another object.

Think of the Http proxy, where the communication between the user and the internet is mediated, the Proxy pattern directs the client to use an intermediary class to communicate with the target components and prevent direct interaction. Its meaning depends on the problem, can block to handle logic to increase performance, or block to check access, handle cache, …

We will go into an example later. The system provides a CommandExecutor class that allows the client to pass commands to execute. However, a mechanism is needed to prevent the execution of malicious orders. For example, members are not allowed to execute delete orders.

This is the main impl class. The proxy finally calls this class. Here in order to reduce direct access to this class, we can leave it as a final class to avoid inheritance.

Next the constructor will be protected. In the next step we create the Proxy class will share the same package with this class to have the right to create new instances of it.

When the proxy is initialized, it will check if the admin executes the command. If it is correct then auto run the command. If not, you must check, as a member not to run delete commands.

Iterator (Behavioral group)

Iterator must have been very familiar to everyone. This is a pattern that provides a way to browse through a group of objects. Iterator is widely used in Java Collection Framework, with target object as 1 collection.

In principle, iterators provide not only an element-traversal method, but also different iterator types based on problem requirements.

This pattern hides the specific implementation of the data, providing the only way for the client to interact via iterator functions.

We have the following example. We have a list of different channels on TV. The client will perform the browsing through that data, which may be in turn, and also according to the type of channel (in language). Let’s say someone likes to watch only channels that speak Vietnamese, so they just want to browse through those channels and skip the rest.

In this example:

  • The list of channels is the target object to interact with.
  • Iterator will browse through these elements, and can be based on channelType.

Above we have defined the POJO channel, representing the channel. Along with that are the interfaces that define the interactive functions for the channel.

Looking at ChannelCollection, we see absolutely no function that returns the list channel. We will make the client can only interact through its iterator.

Here we have created a channel browsing logic based on its channel type.


Chia sẻ bài viết ngay

Nguồn bài viết : Viblo