技術情報
- 2020年10月12日
- 技術情報
Builder Design Pattern
Builder is a creative design pattern that allows you to build complex objects step by step. The pattern allows you to produce different types and representations of an object using the same building code.
Imagine a complex object that requires laborious step-by-step initialization of many nested objects and fields. This initialization code is usually buried inside a monster constructor with many parameters. Or even worse: scattered throughout the client code.
The Builder pattern suggests that you extract the object construction code out of its own class and move it to separate objects called builders.
Lets look up the code examples I wrote as below.
PHP Code Sample
<?php
/**
* This is the original application interface
* where we place the methods need to develop from its concrete sub classes
*/
interface ApplicationInterface
{
public function setDbDriver(DbDriver $dbDriver);
public function query($query);
}
/**
* This abstract class will implements the interface as we have to reference the next hierarchy object here.
* After that we can access the methods of their sub concrete classes in our sub classes. see below.
*/
abstract class Application implements ApplicationInterface
{
protected $dbDriver;
public function setDbDriver(DbDriver $dbDriver)
{
$this->dbDriver = $dbDriver;
}
}
/**
* Concrete sub classes of the original class working with the reference object's method.
*/
class android extends Application
{
public function query($query)
{
$query .= "\n\n running android app query\n";
return $this->dbDriver->handleQuery($query);
}
}
class ios extends Application
{
public function query($query)
{
$query .= "\n\n running ios app query\n";
return $this->dbDriver->handleQuery($query);
}
}
/**
* This is the interface that need to be referenced by original class instead of inheritance.
*/
interface DbDriver
{
public function handleQuery($query);
}
/**
* Concrete classes that will have the detail implementations of the interface.
*/
class MysqlDriver implements DbDriver
{
public function handleQuery($query)
{
echo "\nUsing the mysql driver: ".$query;
}
}
class OracleDriver implements DbDriver
{
public function handleQuery($query)
{
echo "\nUsing the oracle driver: ".$query;
}
}
//client code
// client doesn't need to know any implementation details.
// Just build the original concrete class and inject the concrete object that will be referenced.
$android = new android();
$android->setDbDriver(new MysqlDriver());
echo $android->query("select * from table");
$android->setDbDriver(new OracleDriver());
echo $android->query("select * from table");
$ios = new ios();
$ios->setDbDriver(new MysqlDriver());
echo $ios->query("select * from table");
$ios->setDbDriver(new OracleDriver());
echo $ios->query("select * from table");
C# Code Sample
using System;
using System.Collections.Generic;
namespace HelloWorld
{
/**
* Two Original classes referencing the same DbDriver which is the next hierarchy object
* After that we can access the methods of their sub concrete classes in our sub classes. query method in this case
*/
class ApplicationInterface
{
protected DbDriver _dbdriver;
public ApplicationInterface(DbDriver dbdriver)
{
this._dbdriver = dbdriver;
}
public virtual string setDbDriver()
{
return "Base DB Driver:" +
_dbdriver.query();
}
}
class RefinedAbstraction : ApplicationInterface
{
public RefinedAbstraction(DbDriver dbdriver) : base(dbdriver)
{
}
public override string setDbDriver()
{
return "Refined DB Driver:" +
base._dbdriver.query();
}
}
/**
* This is the interface that need to be referenced by original class instead of inheritance.
*/
public interface DbDriver
{
string query();
}
/**
* Concrete classes that will have the detail implementations of the interface.
*/
class MysqlDriver : DbDriver
{
public string query()
{
return "Using the mysql driver:\n";
}
}
class OracleDriver : DbDriver
{
public string query()
{
return "Using the oracle driver:.\n";
}
}
// client doesn't need to know any implementation details.
// Just build the original class and inject the concrete object that will be referenced.
class Client
{
public void ClientCode(ApplicationInterface applicationInterface)
{
Console.Write(applicationInterface.setDbDriver());
}
}
class Program
{
static void Main(string[] args)
{
Client client = new Client();
ApplicationInterface applicationInterface;
applicationInterface = new ApplicationInterface(new MysqlDriver());
client.ClientCode(applicationInterface);
Console.WriteLine();
applicationInterface = new RefinedAbstraction(new OracleDriver());
client.ClientCode(applicationInterface);
}
}
}
By Yuuma
yuuma at 2020年10月12日 11:00:33
- 2020年10月09日
- 技術情報
[Laravel] Passportを使用したBearer token認証[3]
PHPのLaravelフレームワークのPassportを使用したBearer token認証の方法について紹介します。
本記事は前回の「[Laravel] Passportを使用したBearer token認証[2]]」の続きとなります。
今回は認証テスト用ユーザーの追加をおこない、Bearer tokenの発行と認証について説明します。
nishida at 2020年10月09日 10:00:28
- 2020年10月05日
- 技術情報
Prototype Design Pattern
Prototype is a build design pattern that allows you to copy existing objects without making your code dependent on their classes.
Declare an abstract base class that specifies a pure virtual “clone” method, and maintain a dictionary of all concrete “cloneable” derived classes. Any class that needs a “polymorphic constructor” capability: It derives from the abstract base class, registers its prototypical instance, and implements the clone () operation.
So the client, instead of writing code that invokes the “new” operator on a wired class name, calls a “clone” operation on the abstract base class, supplying an enumerated string or data type that designates the class desired derivative.
Lets see PHP & C# code samples as below.
PHP
<?php
// I made a common prototype interface for cloning purpose.
// has a single function called _clone
abstract class Person {
protected $name;
abstract function __clone();
function getName() {
return $this->name;
}
function setName($name) {
$this->name = $name;
}
}
// Here is a concrete class extending the person prototype.
// we will implement the _clone method to support object cloning.
//This may contain a lot of fields and implementations but I will keep this simple for now.
class Boy extends Person {
function __clone() {
}
}
//here is our first time instantiation of boy concrete class
$person = new Boy();
//next time we will clone that object by using clone.
// we will also pass the name param dynamically.
// clone1
$person1 = clone $person;
$person1->setName('Hlaing');
$person1->getName();
// clone2
$person2 = clone $boy;
$person2->setName('Tin');
$person2->getName();
print_r($person1);
print_r($person2);
?>
C#
using System;
using System.Collections.Generic;
namespace HelloWorld
{
// Here is my simple object that support object cloning.
// I already build a function called copyclone to clone this object.
public class Person
{
public string Name;
public Person CopyClone()
{
return (Person)this.MemberwiseClone();
}
}
class Program
{
static void Main(string[] args)
{
// firstly we build a object using new instantiation
Person person = new Person();
//next time we will clone that object by theh function copyclone.
// we will also pass the name param dynamically.
// clone1
Person person1 = person.CopyClone();
person1.Name = "Hlaing";
// clone2
Person person2 = person.CopyClone();
person2.Name = "Tin";
Console.WriteLine(person1.Name);
Console.WriteLine(person2.Name);
}
}
}
By Yuuma.
yuuma at 2020年10月05日 11:00:21
- 2020年09月28日
- 技術情報
Builder Design Pattern
Builder is a creational design pattern that allows you to build complex objects step by step. The pattern allows you to produce different types and representations of an object using the same building code.
The Builder pattern suggests that you extract the building code from the object of its own class and move it to separate objects called constructors.
To create an object, run a series of these steps on a constructor object. The important part is that you don’t need to call all the steps. You can call only the steps necessary to produce a particular configuration of an object.
Check out the sample codes below.
PHP sample code
<?php
/**
* Builder interface with different methods.
*/
interface Builder
{
public function addVege();
public function addNoodle();
public function addMeat();
}
/**
* Implementing the builder and interface with different implementations
*/
class MalaBuilder implements Builder
{
private $product;
/**
* To rest the Mala Object as a new one.
*/
public function __construct()
{
$this->product = new Mala;
}
/**
* All production steps work with the same product instance.
*/
public function addVege()
{
$this->product->ingredients[] = "Vegetable";
}
public function addNoodle()
{
$this->product->ingredients[] = "Noodle";
}
public function addMeat()
{
$this->product->ingredients[] = "Meat";
}
/**
* returning results for each different implementatins.
*/
public function add()
{
$result = $this->product;
/**
* To rest the Mala Object as a new one.
*/
$this->product = new Mala;
return $result;
}
}
/**
* display the final results of Malabuilder
*/
class Mala
{
public $ingredients = [];
public function returnMala()
{
echo implode(', ', $this->ingredients) . "<br>";
}
}
/**
* I added a sample MalaDirector to create a few ready made implementations.
*/
class MalaDirector
{
private $builder;
public function setBuilder(Builder $builder)
{
$this->builder = $builder;
}
public function addVegeNoodle()
{
$this->builder->addVege();
$this->builder->addNoodle();
}
public function addVegeMeatNoodle()
{
$this->builder->addVege();
$this->builder->addNoodle();
$this->builder->addMeat();
}
}
function clientCode(MalaDirector $malaDirector)
{
$builder = new MalaBuilder;
$malaDirector->setBuilder($builder);
echo "This is vege Mala:\n";
$malaDirector->addVegeNoodle();
$builder->add()->returnMala();
echo "Full Ingredients Mala:\n";
$malaDirector->addVegeMeatNoodle();
$builder->add()->returnMala();
// This time with no MalaDirector usage.
echo "Custom Mala:\n";
$builder->addNoodle();
$builder->addMeat();
$builder->add()->returnMala();
}
$malaDirector = new MalaDirector;
clientCode($malaDirector);
C# sample code
using System;
using System.Collections.Generic;
namespace HelloWorld
{
/**
* Builder interface with different methods.
*/
public interface Builder
{
void addVege();
void addNoodle();
void addMeat();
}
/**
* Implementing the builder and interface with different implementations
*/
public class MalaBuilder : Builder
{
private Mala _mala = new Mala();
/**
* To rest the Mala Object as a new one.
*/
public MalaBuilder()
{
this._mala = new Mala();
}
public void addVege()
{
this._mala.Add("vegetable");
}
public void addNoodle()
{
this._mala.Add("noodle");
}
public void addMeat()
{
this._mala.Add("meat");
}
/**
* returning results for each different implementatins.
*/
public Mala FinalMala()
{
Mala result = this._mala;
this._mala = new Mala();
return result;
}
}
/**
* add function to the list object of ingredients.
* return the final results of Malabuilder.
*/
public class Mala
{
private List<object> incredigents = new List<object>();
public void Add(string part)
{
this.incredigents.Add(part);
}
public string ReturnMala()
{
string str = string.Empty;
for (int i = 0; i < this.incredigents.Count; i++)
{
str += this.incredigents[i] + ", ";
}
str = str.Remove(str.Length - 2); //removing last comma and space.
return + str + "\n";
}
}
/**
* A sample MalaDirector to create a few ready made implementations.
*/
public class Director
{
private Builder _builder;
public Builder Builder
{
set { _builder = value; }
}
public void addVegeNoodle()
{
this._builder.addVege();
}
public void addVegeMeatNoodle()
{
this._builder.addVege();
this._builder.addNoodle();
this._builder.addMeat();
}
}
class Program
{
static void Main(string[] args)
{
var director = new Director();
var builder = new MalaBuilder();
director.Builder = builder;
Console.WriteLine("Vegetable Mala:");
director.addVegeNoodle();
Console.WriteLine(builder.FinalMala().ReturnMala());
Console.WriteLine("Full Ingredients Mala:");
director.addVegeMeatNoodle();
Console.WriteLine(builder.FinalMala().ReturnMala());
// This time with no MalaDirector usage.
Console.WriteLine("Custom Mala:");
builder.addNoodle();
builder.addMeat();
Console.Write(builder.FinalMala().ReturnMala());
}
}
}
By Yuuma
yuuma at 2020年09月28日 11:00:41
- 2020年09月25日
- 技術情報
[Laravel] Passportを使用したBearer token認証[2]
PHPのLaravelフレームワークのPassportを使用したBearer token認証の方法について紹介いたします。本記事は前回の「[Laravel] Passportを使用したBearer token認証[1]」の続きとなります。今回はPassportのインストール方法からLaravel標準の認証機能をPassportに変更するところまでを説明します。
続きを読むnishida at 2020年09月25日 10:00:37