public class Customer { public void CallSomeOne(){ PhoneA p1 = new PhoneA(); p1.Call(); } } public class PhoneA { public void Call(){} } public class PhoneB { public void Call(){} }
using System; namespace Example1 { public interface IProduct { } public class PhoneA : IProduct { } public class PhoneB : IProduct { } public class Factory { public IProduct Create() { // 工厂决定到底实例化哪个子类。 return new PhonetA (); } } } using System; namespace Example1 { public enum Category { A,
} public static class ProductFactory { public static IProduct Create(Category category) { switch (category) { case Category.A: return new PhoneA(); case Category.B: return new PhoneB(); default: throw new NotSupportedException(); } } } }