Ever wonder what kind of design pattern? In this article, I’ll explain why design patterns are very important, and will provide some examples in PHP, when and why you should use them.

What is a design pattern?

Design mode is optimized solutions to programming problems that we encounter every day can be reused. A design pattern is not a class or we can simply plug into our system database; it’s much more than that. It is having the right circumstances to achieve the template. This is not language-specific either. A good design pattern should be most if not all of the language to achieve, depending on the language ability. Most importantly, any designs may, if put in the wrong place double-edged sword- implemented, this could be disastrous, and create many problems for you. However, the implementation of the right place, at the right time, it can be your savior.

There are three basic design patterns:

  • Structure
  • Creator
  • Behavior

Structure mode is usually handle the relationship between the entity, making it easier to work with for those entities.

Create mode provides examples of mechanisms to make it easier to create objects in a manner appropriate for the situation.

Patterns of behavior between the communication entities, and make it easier and more flexible to communicate with these entities.

Why should we use them?
Design patterns, by principle, thoughtful solutions to programming problems. Many programmers have encountered these problems and take advantage of these “solutions” to remedy the situation. If you encounter these problems, why create a solution, you can use a mature answer?

Example

Let’s imagine that you have been given to create a way to merge two classes which do different things depending on the circumstances of responsibility. These two classes are widely used in different places in the existing system, making it difficult to get rid of these two classes, change the existing code. To increase this, you need to change the existing code still need to test any changes to the code, because these types of editing, rely on different components of the system, almost always introduce new problems. Instead of doing so, the strategy pattern and adapter pattern can be achieved, it can easily handle changes in these types of scenarios.

<?php class StrategyAndAdapterExampleClass 
{ private $_class_one; private 
$_class_two; private $_context; public
 function __construct( $context ) { $this->_context = $context;
    }
  public function operation1() {
        if( $this->_context == "context_for_class_one" ) {
            $this->_class_one->operation1_in_class_one_context();
        } else ( $this->_context == "context_for_class_two" ) {
            $this->_class_two->operation1_in_class_two_context();
        }
    }
}

Very simple, is not it? Now, let’s take a closer look at the policy mode.

Strategy Mode

42

The strategy pattern is a behavioral design pattern that allows you to decide what action should be taken in the process of a project, depending on the particular background during operation. You encapsulate two different algorithms inside two classes, at runtime decide you want to go with the policy.
In the above example, the strategy is based on whether $ context variable is an instance of the class time. If you give it a class background, it uses class_one, and vice versa.

Lovely, but here I can use this?

Imagine that you are currently being developed to update or create a new user record class. It still requires the same input (name, address, mobile phone number, etc.), but the next depending on the given situation, it has to use different functions to create and update. Now, you could probably just use the if-else statement to do this, but if you need to use this class in different places? In this case, you have to start to rewrite the same if-else statements. Would not it be easier just to specify the range?

<?php
class User {   
   public function CreateOrUpdate($name, $address, $mobile, $userid = null)
    {
        if( is_null($userid) ) {
            // it means the user doesn't exist yet, create a new record
        } else {
            // it means the user already exists, 
just update based on the given userid
        }
    }
}

Now, the “usual” strategy pattern involves encapsulating algorithm in another class, but in this case, the other is a waste. Remember, you do not have to follow the template exactly. Variable work as long as the concept remains the same, and it solved the problem.

Adapter Model

44

Adapter pattern is a structural design pattern that allows you to readjust a class with a different interface that allows the use of it by the use of different methods of system calls.
This also allows you to change some of the input received from the client class, it’s functionally compatible with the adapter something.

How do I use it?

A reference to another noun adapter class is a package, basically allows you to “packaging” action into a class and reuse under the right circumstances, these actions. Table class field when you create a class typical example might. Instead of calling a different table classes, and each tune out their functions, you can use an adapter class encapsulates all these methods into a method. This not only allows you to reuse any action you want, so you do not if you need to use the same action to rewrite the code in different places.

Compare these two implementations.

Non-adapter method

<?php $user = new User(); $user->CreateOrUpdate( //inputs );
 
$profile = new Profile();
$profile->CreateOrUpdate( //inputs );

If we need to do it again in a different place that, even use this code in different projects, we will have to start all over again Enter everything.
better

This is opposed to doing something like this:

<?php $account_domain = new Account(); $account_domain->NewAccount( //inputs );

In this case, we have a wrapper class, which will be our Account domain class:

<?php class Account() { public function NewAccount( //inputs ) 
{ $user = new User(); $user->
CreateOrUpdate( //subset of inputs );  
        $profile = new Profile();
        $profile->CreateOrUpdate( //subset of inputs );
    }
}

This way, you can need to use your domain account on the increase again, you could also true to wrap other classes under your domain class.