アプリ関連ニュース
- 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年10月02日
- VR
Oculus Quest 2 続報
先日のFacebook Connectで発表のあった「Oculus Quest 2」ですが、そのスペックと戦略的な価格設定から発売前からとても話題になっています。
VR系の製品紹介やゲーム実況などをおこなっている何人かのYouTuberのもとには、発売前の「Oculus Quest 2」の実機が先行して届けられているようで、「Oculus Quest 2」の開封動画や製品紹介、比較動画などが、アップロードされはじめています。
今回はそれらの動画やレビューを視聴しての感想をかきたいと思います。
nishida at 2020年10月02日 10:00:12
- 2020年9月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年9月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
- 2020年9月21日
- 技術情報
Laravel Livewire
I am talking about the brief introduction about laravel livewire today.
Its aim is to provide a modern and dynamic web experience without having to waste time with reactive web frameworks or Javascript at all.
And for the most part: it can. Livewire really works, and in the next few years we will almost definitely see some big, serious projects on it.
When you activate the livewire, you will
– Write a PHP class, which will act as the backend part of a frontend component. Public fields will be visible to Livewire.
– Write an interface component in the Laravel (Blade) template language. This component refers to the fields in the backend component that you wrote earlier.
– Write methods in your backend component class that change the state of public fields.
– Create an instance of the component in another view or another component.
– When you visit the page, Livewire will render the initial state of the component as HTML and inject it into the page you see. Livewire will have already attached auto-generated JavaScript events to the page, which call the methods you wrote in step 3 above.
– When you take action, the Javascript events that Livewire generates will make an AJAX request to the Livewire endpoint on the backend. This will call the method you wrote, changing the public properties you defined.
-Your backend component’s render method is then called, which produces a snippet of HTML. This HTML snippet is the response to the AJAX request.
– Finally, the client-side Livewire component hot-patches this HTML on the page.
Further more you can look up the livewire lifecycle hooks more details here.
https://laravel-livewire.com/docs/2.x/lifecycle-hooks
Livewire has lots of interesting features but it still have some drawbacks as follow.
– Livewire violates the separation of concerns
– Livewire violates the single-responsibility principle
– Livewire complicates the development workflow
– Too Many AJAX Requests
– Livewire is difficult to test
And Finally maintainability would be harder as the application grows.
yuuma at 2020年09月21日 11:00:43