理解:当有几套不同的方案需要随时切换时,可以使用该模式,比如一个手机里面的软件,当从一个操作系统切换到另一个操作系统,不需要去改每一个软件,只需要改创建所有软件的那个工厂就行了。
代码:
using System.Windows.Forms;
namespace DesignMode.AbstractFactory
{
//抽象工厂类
public abstract class OS_Factory
{
public abstract PhoneGame CreateGame();
public abstract PhoneOffice CreateOffice();
}
//抽象游戏类
public abstract class PhoneGame
{
public abstract void Play();
}
//抽象办公软件类
public abstract class PhoneOffice
{
public abstract void Use();
}
//安卓工厂
public class Android_Factory:OS_Factory
{
//只创建安卓的
public override PhoneGame CreateGame()
{
return new AndroidGame();
}
public override PhoneOffice CreateOffice()
{
return new AndroidOffice();
}
}
//苹果工厂
public class Mac_Factory : OS_Factory
{
//只创建苹果的
public override PhoneGame CreateGame()
{
return new MacGame();
}
public override PhoneOffice CreateOffice()
{
return new MacOffice();
}
}
//塞班工厂
public class Symbian_Factory : OS_Factory
{
public override PhoneGame CreateGame()
{
return new SymbianGame();
}
public override PhoneOffice CreateOffice()
{
return new SymbianOffice();
}
}
//安卓游戏
public class AndroidGame : PhoneGame
{
public override void Play()
{
MessageBox.Show("玩安卓游戏");
}
}
//苹果游戏
public class MacGame : PhoneGame
{
public override void Play()
{
MessageBox.Show("玩苹果游戏");
}
}
//塞班游戏
public class SymbianGame : PhoneGame
{
public override void Play()
{
MessageBox.Show("玩塞班游戏");
}
}
//安桌办公软件
public class AndroidOffice : PhoneOffice
{
public override void Use()
{
MessageBox.Show("使用安卓办公软件");
}
}
//苹果办公软件
public class MacOffice : PhoneOffice
{
public override void Use()
{
MessageBox.Show("使用苹果办公软件");
}
}
//塞班办公软件
public class SymbianOffice : PhoneOffice
{
public override void Use()
{
MessageBox.Show("使用塞班办公软件");
}
}
}
namespace DesignMode.AbstractFactory
{
//抽象工厂类
public abstract class OS_Factory
{
public abstract PhoneGame CreateGame();
public abstract PhoneOffice CreateOffice();
}
//抽象游戏类
public abstract class PhoneGame
{
public abstract void Play();
}
//抽象办公软件类
public abstract class PhoneOffice
{
public abstract void Use();
}
//安卓工厂
public class Android_Factory:OS_Factory
{
//只创建安卓的
public override PhoneGame CreateGame()
{
return new AndroidGame();
}
public override PhoneOffice CreateOffice()
{
return new AndroidOffice();
}
}
//苹果工厂
public class Mac_Factory : OS_Factory
{
//只创建苹果的
public override PhoneGame CreateGame()
{
return new MacGame();
}
public override PhoneOffice CreateOffice()
{
return new MacOffice();
}
}
//塞班工厂
public class Symbian_Factory : OS_Factory
{
public override PhoneGame CreateGame()
{
return new SymbianGame();
}
public override PhoneOffice CreateOffice()
{
return new SymbianOffice();
}
}
//安卓游戏
public class AndroidGame : PhoneGame
{
public override void Play()
{
MessageBox.Show("玩安卓游戏");
}
}
//苹果游戏
public class MacGame : PhoneGame
{
public override void Play()
{
MessageBox.Show("玩苹果游戏");
}
}
//塞班游戏
public class SymbianGame : PhoneGame
{
public override void Play()
{
MessageBox.Show("玩塞班游戏");
}
}
//安桌办公软件
public class AndroidOffice : PhoneOffice
{
public override void Use()
{
MessageBox.Show("使用安卓办公软件");
}
}
//苹果办公软件
public class MacOffice : PhoneOffice
{
public override void Use()
{
MessageBox.Show("使用苹果办公软件");
}
}
//塞班办公软件
public class SymbianOffice : PhoneOffice
{
public override void Use()
{
MessageBox.Show("使用塞班办公软件");
}
}
}
客户端代码:
private void btn_AbstractFactory_Click(object sender, EventArgs e)
{
//下面选择要创建系统软件
OS_Factory factory = new Symbian_Factory(); //new Mac_Factory(); //new Android_Factory();
PhoneGame game = factory.CreateGame();
PhoneOffice office = factory.CreateOffice();
game.Play();
office.Use(); }
{
//下面选择要创建系统软件
OS_Factory factory = new Symbian_Factory(); //new Mac_Factory(); //new Android_Factory();
PhoneGame game = factory.CreateGame();
PhoneOffice office = factory.CreateOffice();
game.Play();
office.Use(); }