桥接模式(Bridge Pattern)属于结构型模式。通常桥接多个维度之间的组合,而减少类的类的数量。它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦。 需要一个作为桥接的接口,使得实体类的功能独立于接口实现类。这两种类型的类可被结构化改变而互不影响。
设备品牌:小米,华为,苹果,等
设备类型:平板,电脑,手机,等
直接定义类:3X3=9个,交接后的类3+3=6个
public interface Brand
{
void Show();
}
public class XiaomiBrand : Brand
{
public void Show()
{
UnityEngine.Debug.Log("dell brand");
}
}
public class HuaweiBrand : Brand
{
public void Show()
{
UnityEngine.Debug.Log("huawei brand");
}
}
public class AppleBrand : Brand
{
public void Show()
{
UnityEngine.Debug.Log("apple brand");
}
}
public abstract class Device
{
protected Brand brand;
public Device(Brand brand)
{
this.brand = brand;
}
public virtual void Show()
{
brand.Show();
}
}
public class Pad : Device
{
public Pad(Brand brand) : base(brand)
{
}
public override void Show()
{
base.Show();
UnityEngine.Debug.Log("pad device");
}
}
public class Computer : Device
{
public Computer(Brand brand) : base(brand)
{
}
public override void Show()
{
base.Show();
UnityEngine.Debug.Log("computer device");
}
}