using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 抽象工厂 { //抽象工厂 public abstract class AbstractFactory { public abstract Aircondition CreateAircondition(); public abstract Washer CreateWasher(); } //具体工厂 public class HaierFactory:AbstractFactory { public override Aircondition CreateAircondition() { return new HaierAircondition(); } public override Washer CreateWasher() { return new HaierWasher(); } } public class MedieaFactory : AbstractFactory { public override Aircondition CreateAircondition() { return new MideaAircondition(); } public override Washer CreateWasher() { return new MideaWasher(); } } //抽象产品类 public abstract class Aircondition { public abstract void Show(); } public abstract class Washer { public abstract void Show(); } //具体产品类 public class HaierAircondition : Aircondition { public override void Show() { Console.WriteLine("海尔空调:领先科技,世界名牌!"); } } public class HaierWasher:Washer { public override void Show() { Console.WriteLine("海尔洗衣机:智能感知 洗净即停 你掌握生活 我掌握衣服"); } } public class MideaAircondition : Aircondition { public override void Show() { Console.WriteLine("美的空调:精彩生活由我创造 原来生活可以更美的、 创新科技、美的空调"); } } public class MideaWasher : Washer { public override void Show() { Console.WriteLine("美的洗衣机:原来生活可以更美的"); } } class Program { static void Main(string[] args) { AbstractFactory Hfactory = new HaierFactory(); Aircondition Haircon = Hfactory.CreateAircondition(); Haircon.Show(); Washer Hwasher = Hfactory.CreateWasher(); Hwasher.Show(); Console.WriteLine(); AbstractFactory Mfactory = new MedieaFactory(); Aircondition Maircon = Mfactory.CreateAircondition(); Maircon.Show(); Washer Mwasher = Mfactory.CreateWasher(); Mwasher.Show(); Console.Read(); } } }
(4)当你提供一个产品类库,而只想显示它们的接口而不是实现时。
原文:http://blog.csdn.net/ry513705618/article/details/31720629