技術情報

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



[Laravel]バッチ処理のタスクスケジュール2

今回は前回作成したコマンドの処理をLinuxのタスクスケジュール(cron)に登録をおこない、指定した日時で自動的に実行させる方法について紹介したいと思います。

続きを読む

[Laravel]バッチ処理のタスクスケジュール

今回はLaravelでバッチ処理をタスクスケジュールで実行する方法を紹介します。

続きを読む

[SQL]年齢による条件抽出

データベースレコードには「生年月日」のカラムがありますが、「年齢」のカラムはありません。そのような場合に年齢によるSQLの条件抽出をおこなうためにはどのようにSQLを組み立てていけばいいのでしょうか。

続きを読む

Common Aggregate Functions in SQL

Today I will talk about some common aggregate functions that are widely used in SQL programming language.

Count

if we want to get the count of all records or specific where condition records, we can get by using count function.

SELECT COUNT(column_name) FROM table_name
SELECT COUNT(column_name) FROM table_name WHERE condition;

AVG

For average rate calculations, we can use this.

SELECT AVG(column_name) FROM table_name;
SELECT AVG(column_name) FROM table_name WHERE condition;

SUM

For sum results of all records, we can use this.

SELECT SUM(column_name) FROM table_name 
SELECT SUM(column_name) FROM table_name WHERE condition;

MIN

For specific minimum result of a column, we can use this.

SELECT MIN(column_name) FROM table_name;
SELECT MIN(column_name) FROM table_name WHERE condition;

Likewise min, we can use max to get maximum result.

SELECT MAX(column_name) FROM table_name;
SELECT MAX(column_name) FROM table_name WHERE condition;

By Yuuma




アプリ関連ニュース

お問い合わせはこちら

お問い合わせ・ご相談はお電話、またはお問い合わせフォームよりお受け付けいたしております。

tel. 06-6454-8833(平日 10:00~17:00)

お問い合わせフォーム