using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _006桥模式
{
/*----------------------------------------各种桥组件----------------------------------------*/
public interface PhoneScreen
{
void SetScreen();
}
public interface PhoneBattery
{
void SetBattery();
}
/*----------------------------------------各种桥组件----------------------------------------*/
/*----------------------------------------手机基类----------------------------------------*/
public class Phone
{
public Phone()
{
}
public Phone(PhoneBattery battery, PhoneScreen screen)
{
this.Screen = screen;
this.Battery = battery;
}
public PhoneScreen Screen;
public PhoneBattery Battery;
public void ShowComponent()
{
Screen.SetScreen();
Battery.SetBattery();
}
}
/*----------------------------------------手机基类----------------------------------------*/
/*----------------------------------------实体组件----------------------------------------*/
public class HTCScreen : PhoneScreen
{
public void SetScreen()
{
Console.WriteLine("组装上htc屏幕...");
}
}
public class HTCBattery : PhoneBattery
{
public void SetBattery()
{
Console.WriteLine("组装上htc电池...");
}
}
public class IphoneBattery : PhoneBattery
{
public void SetBattery()
{
Console.WriteLine("组装上苹果电池...");
}
}
/*----------------------------------------实体组件----------------------------------------*/
/*----------------------------------------guochan手机----------------------------------------*/
public class GuoChanPhone : Phone
{
public GuoChanPhone(PhoneBattery battery)
{
Battery = battery;
Screen = new HTCScreen();
}
public GuoChanPhone(PhoneBattery battery, PhoneScreen screen) : base(battery, screen)
{
}
}
internal class Program
{
private static void Main(string[] args)
{
HTCBattery battery = new HTCBattery();
HTCScreen screen = new HTCScreen();
Phone Htc = new Phone(battery, screen);
Htc.ShowComponent();
IphoneBattery ibattery = new IphoneBattery();
GuoChanPhone phone = new GuoChanPhone(ibattery);
phone.ShowComponent();
/*
* 桥模式 : 外部传入需要构造的子类对象组建目标对象
*
* 在使用桥模式和组件模式时需要取舍,如果是着手开发则首选组件模式,如果是接手开发则首选桥模式
*
*/
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _006桥模式
{
/*----------------------------------------各种桥组件----------------------------------------*/
public interface PhoneScreen
{
void SetScreen();
}
public interface PhoneBattery
{
void SetBattery();
}
/*----------------------------------------各种桥组件----------------------------------------*/
/*----------------------------------------手机基类----------------------------------------*/
public class Phone
{
public Phone()
{
}
public Phone(PhoneBattery battery, PhoneScreen screen)
{
this.Screen = screen;
this.Battery = battery;
}
public PhoneScreen Screen;
public PhoneBattery Battery;
public void ShowComponent()
{
Screen.SetScreen();
Battery.SetBattery();
}
}
/*----------------------------------------手机基类----------------------------------------*/
/*----------------------------------------实体组件----------------------------------------*/
public class HTCScreen : PhoneScreen
{
public void SetScreen()
{
Console.WriteLine("组装上htc屏幕...");
}
}
public class HTCBattery : PhoneBattery
{
public void SetBattery()
{
Console.WriteLine("组装上htc电池...");
}
}
public class IphoneBattery : PhoneBattery
{
public void SetBattery()
{
Console.WriteLine("组装上苹果电池...");
}
}
/*----------------------------------------实体组件----------------------------------------*/
/*----------------------------------------guochan手机----------------------------------------*/
public class GuoChanPhone : Phone
{
public GuoChanPhone(PhoneBattery battery)
{
Battery = battery;
Screen = new HTCScreen();
}
public GuoChanPhone(PhoneBattery battery, PhoneScreen screen) : base(battery, screen)
{
}
}
internal class Program
{
private static void Main(string[] args)
{
HTCBattery battery = new HTCBattery();
HTCScreen screen = new HTCScreen();
Phone Htc = new Phone(battery, screen);
Htc.ShowComponent();
IphoneBattery ibattery = new IphoneBattery();
GuoChanPhone phone = new GuoChanPhone(ibattery);
phone.ShowComponent();
/*
* 桥模式 : 外部传入需要构造的子类对象组建目标对象
*
* 在使用桥模式和组件模式时需要取舍,如果是着手开发则首选组件模式,如果是接手开发则首选桥模式
*
*/
Console.ReadKey();
}
}
}