技術情報
- 2020年11月16日
- 技術情報
Facade Design Pattern
Facade is an structural design style that provides a simplified interface for a library, framework, or any other complex set of classes.
Let’s say you need to make your code work with a broad set of objects belonging to a sophisticated library or framework. This typically involves initializing all of these objects, tracking dependencies, executing methods in the correct order, and so on.
As a result, the business logic of the class is tightly coupled with the implementation details of the third-party class, making it difficult to understand and maintain.
Facade is a class that provides a simple interface to a complex subsystem containing many moving parts. Facades can provide limited functionality compared to working directly with subsystems. However, this only includes features that the client really cares about.
Lets look at the examples as below.
PHP Sample Code
<?php
// 3rd party module classes
class Kitchen
{
public function cook()
{
echo "cooking order";
}
}
class Cashier
{
public function bill()
{
echo "printing bill";
}
}
//Waiter facade hiding the complexity of other 3rd party classes.
//wrapper class of our all third party packages.
class Waiter
{
public $kitchen;
public $cashier;
public function __construct()
{
$this->kitchen = new Kitchen();
$this->cashier = new Cashier();
}
//providing a simple method called order for client
public function order()
{
$this->kitchen->cook();
$this->cashier->bill();
}
}
$waiter = new Waiter();
$waiter->order();
C# Sample Code
using System;
namespace HelloWorld
{
//Waiter facade hiding the complexity of other 3rd party classes.
//wrapper class of our all third party packages.
public class Facade
{
protected Kitchen _kitchen;
protected Cashier _cashier;
public Facade()
{
this._kitchen = new Kitchen();
this._cashier = new Cashier();
}
//providing a simple method called order for client
public string order()
{
string result = "";
result += this._kitchen.cook();
result += this._cashier.bill();
return result;
}
}
// 3rd party module classes
public class Kitchen
{
public string cook()
{
return "cook order";
}
}
public class Cashier
{
public string bill()
{
return "print bill";
}
}
class Program
{
static void Main(string[] args)
{
Facade facade = new Facade();
Console.Write(facade.order());
}
}
}
By Yuuma.
yuuma at 2020年11月16日 05:40:43
- 2020年11月13日
- 技術情報
[Error: Cannot create file “xampp-control.ini” アクセスが拒否されました]の対処方法
Xampp(Windows)の終了時に「Error: Cannot create file “xampp-control.ini” アクセスが拒否されました」といったエラーが表示される場合の対処方法を紹介します。
続きを読むnishida at 2020年11月13日 10:00:54
- 2020年11月09日
- 技術情報
Decorator Design Pattern
Decorator is a structural design pattern that allows you to attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.
Extending a class is the first thing that comes to mind when you need to modify the behavior of an object. However, inheritance has several important caveats that you should be aware of. Inheritance is static. You cannot modify the behavior of an existing object at run time. You can only replace the entire object with one created from a different subclass. Subclasses can have only one main class. In most languages, inheritance does not allow a class to inherit behaviors from multiple classes at the same time.
One of the ways to overcome these caveats is through aggregation or composition rather than inheritance. Both alternatives work in much the same way: one object has a reference to another and delegates some work to it, whereas with inheritance, the object itself can do that work, inheriting the behavior of its superclass.
Lets look at the examples I created as below.
Sample PHP
<?php
/**
* The interface that must be implemented by concrete class or decorator class
* Many methods might exist in a real world application.
*/
interface Home
{
public function setup();
}
/**
* An example simple concrete class implementing the interface through the setup method.
*/
class ModernHome implements Home
{
public $addon;
public function setup()
{
$this->addon[] = "Added extra bed room";
}
}
/**
* Here is the base decorator class referncing the home interface.
* Implementations don't exist here as this is the scheme of the concrete classes that will extend to it.
*/
abstract class ModernHomeDecorator implements Home
{
protected $home;
public function __construct(Home $home)
{
$this->home = $home;
}
abstract public function setup();
}
/**
* Concrete decorator classes extending the basedecorator(wrapping the original ModernHome Instance).
*/
class CinemaDecorator extends ModernHomeDecorator
{
public function setup()
{
$this->home->setup();
$this->home->addon[] = "added mini cinema";
$this->addon = $this->home->addon;
}
}
class PoolDecorator extends ModernHomeDecorator
{
public function setup()
{
$this->home->setup();
$this->home->addon[] = "added pool";
$this->addon = $this->home->addon;
}
}
/**
* Clients just have to pass the modernhome instance to each decorator class that they want.
* We can see here it is wrapping step by step. ModenHome is wrapped by cinema. Afterwards, cinema is wrapped by pool.
* Array ( [0] => Added extra bed room [1] => added mini cinema [2] => added pool )
*/
$home = new ModernHome();
$home = new CinemaDecorator($home);
$home = new PoolDecorator($home);
$home->setup();
print_r($home->addon);
Sample C#
using System;
namespace HelloWorld
{
/**
* The interface that must be implemented by concrete class or decorator class
* Many methods might exist in a real world application.
*/
abstract class Home
{
public abstract string setup();
}
/**
* An example simple concrete class implementing the interface through the setup method.
*/
class ModernHome : Home
{
public override string setup()
{
return "ModernHome";
}
}
/**
* Here is the base decorator class referncing the home interface.
* Implementations don't exist here as this is the scheme of the concrete classes that will extend to it.
* we have a condition here to do the wrapping process.
*/
abstract class Decorator : Home
{
protected Home _Home;
public Decorator(Home Home)
{
this._Home = Home;
}
public override string setup()
{
if (this._Home != null)
{
return this._Home.setup();
}
else
{
return string.Empty;
}
}
}
/**
* Concrete decorator classes extending the basedecorator(wrapping the original ModernHome Instance).
*/
class CinemaDecorator : Decorator
{
public CinemaDecorator(Home comp) : base(comp){}
public override string setup()
{
return $"CinemaDecorator({base.setup()})";
}
}
class PoolDecorator : Decorator
{
public PoolDecorator(Home comp) : base(comp){}
public override string setup()
{
return $"PoolDecorator({base.setup()})";
}
}
/**
* Clients just have to pass the modernhome instance to each decorator class that they want.
* We can see here it is wrapping step by step. ModenHome is wrapped by cinema. Afterwards, cinema is wrapped by pool.
* PoolDecorator(CinemaDecorator(ModernHome))
*/
class Program
{
static void Main(string[] args)
{
var moden = new ModernHome();
CinemaDecorator cinema = new CinemaDecorator(moden);
PoolDecorator pool = new PoolDecorator(cinema);
Console.WriteLine(pool.setup());
}
}
}
By Yuuma.
yuuma at 2020年11月09日 11:00:26
- 2020年11月06日
- 技術情報
[Laravel] Eloquentとページネーション(2)
今回はLaravel標準のO/Rマッパー「Eloquent」を使用したデータ取得方法を紹介したいと思います。本記事は前回の「[Laravel] Eloquentとページネーション(1)」の続きになります。
続きを読むnishida at 2020年11月06日 10:00:02
- 2020年11月02日
- 技術情報
Composite Design Pattern
Composite is a structural design pattern that allows you to compose objects into tree structures and then work with these structures as if they were individual objects.
The application needs to manipulate a hierarchical collection of “primitive” and “compound” objects. Processing of a primitive object is handled in one way and processing of a composite object is handled differently. You don’t want to have to query the “type” of each object before trying to process it.
Define an abstract base class (Component) that specifies the behavior to be exerted uniformly on all primitive and composite objects. Subclass the Primitive and Composite classes outside of the Component class. Each composite object “docks” itself only with the abstract type component, since it manages its “children”.
Use this pattern whenever you have “compounds that contain components, each of which could be a compound.”
Let’s look at the example I wrote as below.
PHP Code Sample
<?php
/**
* This is the common interface of the composite pattern, declares the necessary operations here.
* this might be a set of complex operations under here in a real world application.
*/
interface Common
{
public function report();
}
/**
* Leaf class where there is no any children under this.
*/
class Individual implements Common
{
private $name;
private $total;
public function __construct($name,$total)
{
$this->name = $name;
$this->total = $total;
}
public function report()
{
echo "$this->name got $this->total\n";
}
}
/**
* composite class where has complex component structure.
* getting to each target children to get the final result
*/
class Composition implements Common
{
public $report_list = [];
public function addRecord($list)
{
$this->report_list[] = $list;
}
/**
* traversing child nodes.
*/
public function report()
{
foreach($this->report_list as $list) {
$list->report();
}
}
}
/**
* client code can now access both individual and composite class without acknowleding the concrete implementations.
*/
$p1 = new Individual("Mg Mg",100);
$p2 = new Individual("Hla Hla",200);
$list = new Composition();
$list->addRecord($p1);
$list->addRecord($p2);
$list->report();
?>
C# Code Sample
using System;
using System.Collections.Generic;
namespace HelloWorld
{
/**
* This is the common interface of the composite pattern, declares the necessary operations here.
* this might be a set of complex operations under here in a real world application.
*/
interface Component
{
string report();
}
/**
* Leaf class where there is no any children under this.
*/
class Individual : Component
{
private string _name;
private int _total;
public Individual(string name,int total)
{
this._name = name;
this._total = total;
}
public string report()
{
return _name + " got " + _total;
}
}
/**
* composite class where has complex component structure.
* getting to each target children to get the final result
*/
class Composition : Component
{
protected List<Component> _lists = new List<Component>();
public string report()
{
int i = 0;
string result = "";
foreach (Component component in this._lists)
{
result += component.report();
if (i != this._lists.Count - 1)
{
result += "\n";
}
i++;
}
return result;
}
public void Add(Component component)
{
this._lists.Add(component);
}
}
class Program
{
static void Main(string[] args)
{
/**
* client code can now access both individual and composite class without acknowleding the concrete implementations.
*/
var p1 = new Individual("Mg Mg", 100);
var p2 = new Individual("Hla Hla", 200);
var list = new Composition();
list.Add(p1);
list.Add(p2);
Console.Write(list.report());
}
}
}
By Yuuma.
yuuma at 2020年11月02日 11:00:04