【C++】Facade(デザインパターン)

## Facadeとは?
複雑なクラス群やサブシステムを
**単一のインターフェースにする**ことで、
それ以外の**複雑さを隠蔽し、保守性を高め、
使用部分を簡潔にする**設計。
結果、**依存関係を減らし、
変更に強い**ものができる。

### C++
```C++:Facade.cpp
#include <iostream>
#include <string>

/// <summary>
/// スマホ
/// </summary>
class SmartPhone
{
public:
	inline std::string PowerOn()
	{
		return "画面を開く\n";
	}
	inline std::string PowerOff()
	{
		return "画面を閉じる\n";
	}
};

/// <summary>
/// 動画
/// </summary>
class Movie
{
public:
	inline std::string PowerOn()
	{
		return "動画を開く\n";
	}
	inline std::string PowerOff()
	{
		return "動画を閉じる\n";
	}
};

/// <summary>
/// 視聴
/// </summary>
class View
{
public:
	inline std::string PowerOn()
	{
		return "再生する\n";
	}
	inline std::string PowerOff()
	{
		return "停止する\n";
	}
};

/// <summary>
/// ファサード
/// </summary>
class Facade
{
public:
	/// <summary>
	/// コンストラクタ
	/// </summary>
	/// <param name="_smartPhone">スマホ</param>
	/// <param name="_movie">動画</param>
	/// <param name="_view">視聴</param>
	Facade
	(
		SmartPhone* _smartPhone = nullptr,
		Movie* _movie = nullptr,
		View* _view = nullptr
	)
	{
		// 動的確保
		smartPhone = (_smartPhone) ? _smartPhone : new SmartPhone;
		movie = (_movie) ? _movie : new Movie;
		view = (_view) ? _view : new View;
	}
	/// <summary>
	/// デストラクタ
	/// </summary>
	virtual ~Facade()
	{
		// メモリの解放
		delete smartPhone;
		delete movie;
		delete view;
	}

	/// <summary>
	/// 一連の動作を呼び出す
	/// </summary>
	std::string Operation()
	{
		std::string result;
		result += "～動画を見たい!～\n";
		result += smartPhone->PowerOn();
		result += movie->PowerOn();
		result += view->PowerOn();
		result += "～動画を見終わりました～\n";
		result += view->PowerOff();
		result += movie->PowerOff();
		result += smartPhone->PowerOff();
		return result;
	}

private:
	SmartPhone* smartPhone;
	Movie* movie;
	View* view;
};

int main(void)
{
	// 動的確保
	SmartPhone* smartPhone = new SmartPhone;
	Movie* movie = new Movie;
	View* view = new View;
	Facade* facade = new Facade(smartPhone, movie, view);

	// 呼び出し
	std::cout << facade->Operation();

	// メモリの解放
	delete facade;

	return 0;
}
```

### C#
```C#:Facade.cs
using System;

/// <summary>
/// スマホ
/// </summary>
public class SmartPhone
{
    public string PowerOn()
    {
        return "画面を開く\n";
    }
    public string PowerOff()
    {
        return "画面を閉じる\n";
    }
}

/// <summary>
/// 動画
/// </summary>
public class Movie
{
    public string PowerOn()
    {
        return "動画を開く\n";
    }
    public string PowerOff()
    {
        return "動画を閉じる\n";
    }
}

/// <summary>
/// 視聴
/// </summary>
public class View
{
    public string PowerOn()
    {
        return "再生する\n";
    }
    public string PowerOff()
    {
        return "停止する\n";
    }
}

/// <summary>
/// ファサード
/// </summary>
public class Facade
{
    private SmartPhone smartPhone;
    private Movie movie;
    private View view;

    /// <summary>
    /// コンストラクタ
    /// </summary>
    /// <param name="_smartPhone">スマホ</param>
    /// <param name="_movie">動画</param>
    /// <param name="_view">視聴</param>
    public Facade
    (
        SmartPhone _smartPhone,
        Movie _movie,
        View _view
    )
    {
        smartPhone = _smartPhone;
        movie = _movie;
        view = _view;
    }

    /// <summary>
    /// 一連の動作を呼び出す
    /// </summary>
    public string Operation()
    {
        string result = "";
        result += "～動画を見たい!～\n";
        result += smartPhone.PowerOn();
        result += movie.PowerOn();
        result += view.PowerOn();
        result += "～動画を見終わりました～\n";
        result += view.PowerOff();
        result += movie.PowerOff();
        result += smartPhone.PowerOff();
        return result;
    }
}

public class Program
{
    static void Main()
    {
        // 動的確保
        SmartPhone smartPhone = new SmartPhone();
        Movie movie = new Movie();
        View view = new View();
        Facade facade = new Facade(smartPhone, movie, view);

        // 呼び出し
        Console.WriteLine(facade.Operation());
    }
}
```