阅读量:0
文章目录
一、使用C#实现单例模式,并说明其模式的适用场景
using System; using System.IO; public class Singleton { private static Singleton instance=null; private static object _lock = new object(); private Singleton() { Console.Write("实例化"); } public static Singleton Instance() { if (instance == null) { lock (_lock) { if (instance == null) { instance = new Singleton(); } } } return instance; } } namespace ConsoleApp1 { internal class Program { static void Main(string[] args) { Singleton a = Singleton.Instance(); Singleton b = Singleton.Instance(); Console.ReadKey(); } } }
- 当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。
- 当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实
例时。
二、使用C#实现工厂方法模式,并说明其模式的适用场景
CreditCard.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { public interface CreditCard { string GetCardType(); int GetCreditLimit(); int GetAnnualCharge(); } }
MoneyBack.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { public class Moneyback:CreditCard { public string GetCardType() { return "MoneyBack"; } public int GetCreditLimit() { return 15000; } public int GetAnnualCharge() { return 500; } } }
Titanium.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { public class Titanium:CreditCard { public string GetCardType() { return "Titanium Edge"; } public int GetCreditLimit() { return 25000; } public int GetAnnualCharge() { return 1500; } } }
Platinum.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { public class Platinum:CreditCard { public string GetCardType() { return "Platinum Plus"; } public int GetCreditLimit() { return 35000; } public int GetAnnualCharge() { return 2000; } } }
CreditCardfactory.cs(工厂类)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { public class CreditCardfactory { public static CreditCard GetCreditCard(string cardType) { CreditCard cardDetials = null; if (cardType == "MoneyBack") { cardDetials = new Moneyback(); } else if (cardType == "Titanium") { cardDetials = new Titanium(); } else if(cardType=="Platinum"){ cardDetials = new Platinum(); } return cardDetials; } } }
program.cs
using System; using System.IO; namespace ConsoleApp1 { internal class Program { static void Main(string[] args) { CreditCard cardDetails = CreditCardfactory.GetCreditCard("Platinum"); if(cardDetails != null ) { Console.WriteLine("CardType:" + cardDetails.GetCardType()); Console.WriteLine("CreditLimit:" + cardDetails.GetCreditLimit()); Console.WriteLine("AnnualCharge:" + cardDetails.GetAnnualCharge()); } else { Console.WriteLine("Invalid Card Type"); } Console.ReadLine(); } } }
- 复杂对象创建
- 解耦性
- 有条件的对象创建
- 可拓展性
- 可维护性
三、使用C#实现抽象工厂模式,并说明其模式的适用场景
using System; using System.Drawing; namespace Client { internal class Program { static void Main(string[] args) { //哥哥欧式风格的房子 AbstractFactory europeanFacory = new EuropeanFactory(); europeanFacory.CreateRoof().Create(); europeanFacory.CreateFloor().Create(); europeanFacory.CreateWindow().Create(); europeanFacory.CreateDoor().Create(); //弟弟现代风格的房子 AbstractFactory modernizationFacroty = new ModernizationFactory(); modernizationFacroty.CreateRoof().Create(); modernizationFacroty.CreateFloor().Create(); modernizationFacroty.CreateWindow().Create(); modernizationFacroty.CreateDoor().Create(); Console.Read(); } } } //抽象工厂类,提供创建不同类型房子的接口 public abstract class AbstractFactory { public abstract Roof CreateRoof(); public abstract Floor CreateFloor(); public abstract Window CreateWindow(); public abstract Door CreateDoor(); } //欧式风格的工厂 public class EuropeanFactory : AbstractFactory { public override Roof CreateRoof() { return new EuropeanRoof(); } public override Floor CreateFloor() { return new EuropeanFloor(); } public override Window CreateWindow() { return new EuropeanWindow(); } public override Door CreateDoor() { return new EuropeanDoor(); } } //现在风格的工厂 public class ModernizationFactory : AbstractFactory { public override Roof CreateRoof() { return new ModernizationRoof(); } public override Floor CreateFloor() { return new ModernizationFloor(); } public override Window CreateWindow() { return new ModernizationWindow(); } public override Door CreateDoor() { return new ModernizationDoor(); } } //房屋抽象类 public abstract class Roof { public abstract void Create(); } public abstract class Floor { public abstract void Create(); } public abstract class Window { public abstract void Create(); } public abstract class Door { public abstract void Create(); } //欧式地板类 public class EuropeanFloor : Floor { public override void Create() { Console.WriteLine("创建欧式的地板"); } } public class EuropeanRoof : Roof { public override void Create() { Console.WriteLine("创建欧式的房顶"); } } public class EuropeanWindow : Window { public override void Create() { Console.WriteLine("创建欧式的窗户"); } } public class EuropeanDoor : Door { public override void Create() { Console.WriteLine("创建欧式的房门"); } } public class ModernizationFloor : Floor { public override void Create() { Console.WriteLine("创建现代风格的地板"); } } public class ModernizationRoof : Roof { public override void Create() { Console.WriteLine("创建现代风格的房顶"); } } public class ModernizationWindow : Window { public override void Create() { Console.WriteLine("创建现代风格的窗户"); } } public class ModernizationDoor : Door { public override void Create() { Console.WriteLine("创建现代风格的房门"); } }
如果系统需要多套的代码解决方案,并且每套的代码方案中又有很多相互关联的产品类型,并且在系统中我们可以相互替换的使用一套产品的时候可以使用该模式,客户端不需要依赖具体实现。