一杯咖啡可以大杯或者小杯,可以加奶或者不加奶,如果单纯继承,概念会有重叠,很混乱
using UnityEngine;
using System.Collections;
public class BridgeStructure : MonoBehaviour
{
void Start ( )
{
Coffee a = new RefinedAbstractionA();
Coffee b = new RefinedAbstractionB();
// Set implementation and call
a.Implementor = new ConcreteImplementorA();
a.Operation();
// Change implemention and call
b.Implementor = new ConcreteImplementorB();
b.Operation();
}
}
/// <summary>
/// The 'Abstraction' class
/// </summary>
class Coffee
{
protected Implementor implementor;
// Property
public Implementor Implementor
{
set { implementor = value; }
}
public virtual void Operation()
{
implementor.Operation();
}
}
/// <summary>
/// The 'Implementor' abstract class
/// </summary>
abstract class Implementor
{
public abstract void Operation();
}
/// <summary>
/// The 'RefinedAbstraction' class
/// </summary>
class RefinedAbstractionA : Coffee
{
public override void Operation()
{
Debug.Log("大杯");
implementor.Operation();
}
}
class RefinedAbstractionB : Coffee
{
public override void Operation()
{
Debug.Log("小杯");
implementor.Operation();
}
}
/// <summary>
/// The 'ConcreteImplementorA' class
/// </summary>
class ConcreteImplementorA : Implementor
{
public override void Operation()
{
Debug.Log("加奶");
}
}
/// <summary>
/// The 'ConcreteImplementorB' class
/// </summary>
class ConcreteImplementorB : Implementor
{
public override void Operation()
{
Debug.Log("不加奶");
}
}