Home > front end >  Is this class a builder?
Is this class a builder?

Time:06-15

Thanks to this class:

https://github.com/WebPajooh/TeleBot/blob/master/src/InlineKeyboard.php

I can build keyboards step by step:

$keyboard = new InlineKeyboard()
    ->addCallbackButton('Start', 'start_callback')
    ->addCallbackButton('Help', 'help_callback')
    ->addUrlButton('FAQ', 'https://example.com/faq')
    ->get();

The output of get() method is an object encoded by json_encode().

My question is:

  1. Can we consider this class a builder?
  2. If answer is no, why?
  3. Its output is not an object, but a string; does it mean that it is not implementing builder pattern?

CodePudding user response:

As wiki says:

The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming

As your code creates InlineKeyboard with some buttons such as urlButton and etc, then it looks like it is Builder pattern.

In addition, Fluent interface pattern can be seen here as there are chainable methods such as:

->addCallbackButton('Start', 'start_callback')
->addCallbackButton('Help', 'help_callback')
->addUrlButton('FAQ', 'https://example.com/faq')
->get()
  • Related