技術情報
- 2020年08月28日
- 技術情報
[Laravel] sessionの共有について
過去記事「[Laravel] 設定ファイル「.env」の切り替えについて」にて、URLのパス指定により、複数の「.env」設定ファイルの切り替えをおこなう方法を紹介いたしました。
今回は上記過去記事の補足説明となります。
nishida at 2020年08月28日 10:00:24
- 2020年08月24日
- 技術情報
Abstract Factory Design Pattern
Abstract Factory defines an interface to create all the different products, but leaves the actual creation of the product to specific factory classes. Each type of factory corresponds to a certain variety of product.
The client code calls the methods of creating a factory object instead of creating products directly with a call to the constructor (new operator). Since a factory corresponds to a single product variant, all of its products will be compatible.
The client code works with factories and products only through their abstract interfaces. This allows the client code to work with any product variant, created by the factory object.
C# code sample
using System;
namespace HelloWorld
{
/**
* Define the Factory interface first.
*/
abstract class TvSetFactory
{
public abstract Tv createTv();
public abstract CdPlayer createCdPlayer();
}
/**
* Then I will create related factory classes and implements the interface
* Panasonic Factory & SamsaungFactory in this scenario.
*/
class PanasonicFactory : TvSetFactory
{
public override Tv createTv()
{
return new PanasonicTv();
}
public override CdPlayer createCdPlayer()
{
return new PanasonicCdPlayer();
}
}
class SamsaungFactory : TvSetFactory
{
public override Tv createTv()
{
return new SamsaungTv();
}
public override CdPlayer createCdPlayer()
{
return new SamsaungCdPlayer();
}
}
/**
* Now lets build family of object classes.
* Tv & CdPlayer in this scenario.
*/
abstract class Tv
{
public abstract string getTv();
}
/**
* These classes have to be implemented by their relative factory.
*/
class PanasonicTv : Tv
{
public override string getTv()
{
return "PanasonicTv";
}
}
class SamsaungTv : Tv
{
public override string getTv()
{
return "SamsaungTv";
}
}
abstract class CdPlayer
{
public abstract string getCdPlayer();
}
class PanasonicCdPlayer : CdPlayer
{
public override string getCdPlayer()
{
return "PanasonicCdPlayer";
}
}
class SamsaungCdPlayer : CdPlayer
{
public override string getCdPlayer()
{
return "SamsaungCdPlayer";
}
}
// client code
/**
* Now you can call the specific factory to return family of objects without knowing their own detail implementations.
* if you want to get samsaung products, you just need to change the Factory class to samsaung factory.
*/
class Program
{
static void Main(string[] args)
{
var tv = new PanasonicFactory();
Console.WriteLine(tv.createTv().getTv());
var cdPlayer = new PanasonicFactory();
Console.WriteLine(cdPlayer.createCdPlayer().getCdPlayer());
}
}
}
PHP code sample
<?php
/**
* Define the Factory interface first.
*/
abstract class TvSetFactory
{
abstract function createTv();
abstract function createCdPlayer();
}
/**
* Then I will create related factory classes and extends the interface
* Panasonic Factory & SamsaungFactory in this scenario.
*/
class PanasonicFactory extends TvSetFactory
{
public function createTv()
{
return new PanasonicTv;
}
public function createCdPlayer()
{
return new PanasonicCdPlayer();
}
}
class SamsaungFactory extends TvSetFactory
{
public function createTv()
{
return new SamsaungTv;
}
public function createCdPlayer()
{
return new SamsaungCdPlayer();
}
}
/**
* Now lets build family of object classes.
* Tv & CdPlayer in this scenario.
*/
abstract class Tv
{
abstract function getTv();
}
/**
* These classes have to be implemented by their relative factory.
*/
class PanasonicTv extends Tv
{
public function getTv()
{
return "PanasonicTv";
}
}
class SamsaungTv extends Tv
{
public function getTv(): string
{
return "SamsaungTv";
}
}
abstract class CdPlayer
{
abstract function getCdPlayer();
}
class PanasonicCdPlayer extends CdPlayer
{
public function getCdPlayer()
{
return "PanasonicCdPlayer";
}
}
class SamsaungCdPlayer extends CdPlayer
{
public function getCdPlayer()
{
return "SamsaungCdPlayer";
}
}
// client code
function test($tvSetFactory)
{
$tv = $tvSetFactory->createTv();
echo('TV model is'.$tv->getTv());
$cdPlayer = $tvSetFactory->createCdPlayer();
echo('CD Player model is'.$cdPlayer->getCdPlayer());
}
/**
* Now you can call the specific factory to return family of objects without knowing their own detail implementations.
* if you want to get samsaung products, you just need to change the Factory class to samsaung factory.
*/
$tvSetFactory = new PanasonicFactory;
test($tvSetFactory);
By Yuuma
yuuma at 2020年08月24日 11:05:10
- 2020年08月11日
- 技術情報
Design Patterns In Programming
In software engineering, a design pattern is a general solution to a common problem in software design. A design pattern is not a finished design that can be directly transformed into code. It is a description or template for how to solve a problem that can be used in many different situations.
Design patterns can speed up the development process by providing tried and tested development paradigms. Effective software design requires considering issues that may not be visible until later in implementation. Reusing design patterns helps prevent subtle problems that can cause major problems and improves code readability for coders and architects familiar with the patterns.
Generally , there are 23 kinds of design patterns under three main groups called creational, structural and behavioral.
Under the creational design pattern.
- Abstract Factory
- Builder
- Factory Method
- Object Pool
- Prototype
- Singleton
Structural design pattern
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
- Private Class Data
- Proxy
Behavioral design pattern
- Chain of responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Null Object
- Observer
- State
- Strategy
- Template method
- Visitor
I will talk about those patterns one by one in the coming weeks, see ya.
By Yuuma.
yuuma at 2020年08月11日 11:00:44
- 2020年08月03日
- 技術情報
Factory Design Pattern
Factory Method is a creation design pattern that provides an interface for creating objects and allows subclasses to alter the type of objects that will be created.
Imagine that you’re implementing a mailing feature . At first you are sending email with mailgun and all of your business source code are all in one place.
Later you have to implement using another service called mailchimp. So you have to overwrite you source code again. If you are going to use both of the services, your code will be messy to be able to work for both services.
At this point , we can use factory design pattern, by creating objects for both service classes. So, we gonna build a mailing interface with an abstract function `sendmail` first. Then we will have two sub classes to implement that interface (mailgun and mailchimp classes for now). Inside these classes, there will be a function to override the abstract function from interface called `sendmail` which will be different in implementation according to classes.
At the final step, we will create factory classes and return mail implementation concrete classes. The client just has to choose which factory to be used, doesn’t has to know the detail implementation of the concrete classes which is the main theme of this factory pattern.
So I will create code samples for the above scenarios. I will write in PHP & C#.
PHP Sample Code
<?php
abstract class MailerFactory
{
abstract function mailDriver();
}
class MailchimpFactory extends MailerFactory
{
public function mailDriver()
{
return new MailchimpMailer();
}
}
class MailgunFactory extends MailerFactory
{
public function mailDriver()
{
return new MailgunMailer();
}
}
interface Mailer
{
function sendmail($message);
}
class MailchimpMailer implements Mailer
{
public function sendmail($message)
{
echo("Mailchimp email > " . $message . "<br/>");
}
}
class MailgunMailer implements Mailer
{
public function sendmail($message)
{
echo("mailgun email > " . $message . "<br/>");
}
}
//Client code
$client = new MailchimpFactory();
$client->mailDriver()->sendmail("This is from Mailchimp");
$client = new MailgunFactory();
$client->mailDriver()->sendmail("this is from mailgun");
?>
C# sample code
using System;
namespace HelloWorld
{
abstract class MailerFactory
{
public abstract Mailer mailDriver();
}
class MailchimpFactory : MailerFactory
{
public override Mailer mailDriver()
{
return new MailchimpMailer();
}
}
class MailgunFactory : MailerFactory
{
public override Mailer mailDriver()
{
return new MailgunMailer();
}
}
public interface Mailer
{
string sendmail(string message);
}
class MailchimpMailer : Mailer
{
public string sendmail(string message)
{
return "sending from mailchimp";
}
}
class MailgunMailer : Mailer
{
public string sendmail(string message)
{
return "sending from mailgun";
}
}
class Program
{
static void Main(string[] args)
{
var mailgun = new MailgunFactory();
Console.WriteLine(mailgun.mailDriver().sendmail("sending from mailgun"));
var mailchimp = new MailchimpFactory();
Console.WriteLine(mailchimp.mailDriver().sendmail("sending from mailchimp"));
}
}
}
Pros
- loosely coupling as classes are not depending on each other.
- easy to maintain (extend or remove) concrete classes.
Cons
- Code might be nasty when many concrete classes are implementing to one interface.
By Yuuma
yuuma at 2020年08月03日 11:07:22
- 2020年07月31日
- 技術情報
[Laravel]バッチ処理のタスクスケジュール2
nishida at 2020年07月31日 10:00:31