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



アプリ関連ニュース

お問い合わせはこちら

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

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

お問い合わせフォーム