diff --git a/202409/ConsoleApp1/Animals/Animal.cs b/202409/ConsoleApp1/Animals/Animal.cs index 640537c..e1e0262 100644 --- a/202409/ConsoleApp1/Animals/Animal.cs +++ b/202409/ConsoleApp1/Animals/Animal.cs @@ -1,5 +1,69 @@ namespace ConsoleApp1.Animals { + + internal class MealReceiverAdapter : IMealReceiver + { + public IMealContainer MealContainer { get; } + + public MealReceiverAdapter(IMealContainer mealContainer) + { + MealContainer = mealContainer; + } + + + public int PutAMeal(int iCount) + { + if (iCount == 0) return 0; + for(int i = 0; i < iCount; i++) + { + MealContainer.ReturnAMeal(); + } + return 1; + } + } + + internal class MealContainer : IMealContainer + { + private int MealCount = 10; + public int GetAMeal() + { + if (MealCount == 0) return 0; + MealCount--; + return 1; + } + + public void ReturnAMeal() + { + MealCount++; + } + } + + internal class MealConsumer : IDisposable + { + public IMealContainer Container { get; } + public int MealCount { get; private set; } = 0; + + public MealConsumer(IMealContainer container) + { + Container = container; + } + + public void ThreeHoursLater() + { + MealCount += Container.GetAMeal(); + } + + + public void Dispose() + { + for(int ii =0; ii < MealCount; ii++) + { + Container.ReturnAMeal(); + } + } + } + + internal class Animal { #region internal classes diff --git a/202409/ConsoleApp1/Animals/IMealContainer.cs b/202409/ConsoleApp1/Animals/IMealContainer.cs new file mode 100644 index 0000000..27b5ac9 --- /dev/null +++ b/202409/ConsoleApp1/Animals/IMealContainer.cs @@ -0,0 +1,14 @@ +namespace ConsoleApp1.Animals +{ + internal interface IMealContainer + { + int GetAMeal(); + void ReturnAMeal(); + } + + internal interface IMealReceiver + { + int PutAMeal(int iCount); + } + +} \ No newline at end of file diff --git a/202409/ConsoleApp1/Program.cs b/202409/ConsoleApp1/Program.cs index 5bbed40..a43c7aa 100644 --- a/202409/ConsoleApp1/Program.cs +++ b/202409/ConsoleApp1/Program.cs @@ -9,22 +9,69 @@ namespace ConsoleApp1 { static void Main(string[] args) { - // h_PlayCards(); - //Animal animal = new Animal(); - //Animal.MilkConsumerAreaInfo info = animal.GetAreaInfo(); - //h_Afd();\ - // h_InfoIO(); - // File.Delete("123.csv"); - // string sFn = "123.csv"; - //h_WriteCsv(sFn); - //h_ReadCsv(sFn); - - string sXmlFn = "123.xml"; + h_DisposableDemo(); + using (var object_ = h_DisposablelessDemo()) + { + //... + } + + + // h_PlayCards(); + //Animal animal = new Animal(); + //Animal.MilkConsumerAreaInfo info = animal.GetAreaInfo(); + //h_Afd();\ + // h_InfoIO(); + // File.Delete("123.csv"); + // string sFn = "123.csv"; + //h_WriteCsv(sFn); + //h_ReadCsv(sFn); + + string sXmlFn = "123.xml"; h_WriteXml(sXmlFn); h_ReadXml(sXmlFn); Console.ReadKey(); } + private static void h_DisposableDemo() + { + using (MealConsumer human = new MealConsumer(GetMealContainer1())) + { + //try + //{ + human.ThreeHoursLater(); + Thread.Sleep(3 * 60 * 60 * 1000 / 400000); + human.ThreeHoursLater(); + Thread.Sleep(3 * 60 * 60 * 1000 / 400000); + human.ThreeHoursLater(); + Thread.Sleep(3 * 60 * 60 * 1000 / 400000); + Console.WriteLine(); + } + + //} + //finally + //{ + // human.Dispose(); + //} + } + + private static MealConsumer h_DisposablelessDemo() + { + MealConsumer human = new MealConsumer(GetMealContainer1()); + human.ThreeHoursLater(); + Thread.Sleep(3 * 60 * 60 * 1000 / 400000); + human.ThreeHoursLater(); + Thread.Sleep(3 * 60 * 60 * 1000 / 400000); + human.ThreeHoursLater(); + Thread.Sleep(3 * 60 * 60 * 1000 / 400000); + Console.WriteLine(); + return human; + } + + private static IMealContainer GetMealContainer1() + { + return new MealContainer(); + } + private static void h_WriteXml(string sXmlFn) { if (File.Exists(sXmlFn)) File.Delete(sXmlFn);