{"id":303486,"date":"2020-05-13T09:00:37","date_gmt":"2020-05-13T09:00:37","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=303486"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=303486","title":{"rendered":"\u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c c# \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b ?: vs if-else vs switch"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/501676\/\">\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043d\u0430 \u0440\u0435\u0432\u044c\u044e \u043f\u0440\u0438\u043b\u0435\u0442\u0435\u043b\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u0430\u044f \u0444\u0430\u0431\u0440\u0438\u043a\u0430 \u0436\u0438\u0432\u043e\u0442\u043d\u044b\u0445:  <\/p>\n<pre><code class=\"cs\">public static class AnimalsFactory {     public static Animal CreateAnimalByTernaryOperator(bool isCat)     {         return isCat ? (Animal)new Cat() : new Dog();     } } <\/code><\/pre>\n<p>  \u0412 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0437 \u0440\u0430\u0441\u0441\u0442\u0440\u043e\u0438\u043b\u0441\u044f, \u0447\u0442\u043e C# \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u043a\u0430\u0441\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 Cat \u043a Animal. \u041d\u043e \u043f\u0443\u0441\u0442\u044c \u043b\u0443\u0447\u0448\u0435 \u0431\u0443\u0434\u0435\u0442 \u043a\u0430\u0441\u0442, \u0432\u0435\u0434\u044c \u0447\u0435\u0440\u0435\u0437 if-else \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u043a\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0435\u0449\u0451 \u0434\u043b\u0438\u043d\u043d\u0435\u0435:  <\/p>\n<pre><code class=\"cs\">public static class AnimalsFactory {     public static Animal CreateAnimalByIfElseOperator(bool isCat)     {         if (isCat)             return new Cat();          return new Dog();     } } <\/code><\/pre>\n<p>  \u041e\u0442\u0432\u043b\u0435\u0447\u0451\u043c\u0441\u044f \u043d\u0430 \u043c\u0438\u043d\u0443\u0442\u0443 \u043e\u0442 \u0440\u0435\u0432\u044c\u044e \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f:<br \/>   \u2014 \u0431\u0443\u0434\u0435\u0442 \u043b\u0438 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f IL-\u043a\u043e\u0434 \u0432 \u044d\u0442\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445?<br \/>   \u2014 \u0431\u0443\u0434\u0435\u0442 \u043b\u0438 \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0432 \u0432\u044b\u0438\u0433\u0440\u044b\u0448\u0435 \u043f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438?<br \/>  <a name=\"habracut\"><\/a><br \/>  \u041e\u0442\u0432\u0435\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441 \u2014 \u0434\u0430, IL-\u043a\u043e\u0434 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f, \u043d\u0438\u0436\u0435 \u043f\u043e\u043a\u0430\u0436\u0443 \u0447\u0435\u043c.<br \/>  \u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u0432\u043e\u043f\u0440\u043e\u0441\u0443 \u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u041a\u0430\u0447\u0430\u0435\u043c nuget-\u043f\u0430\u043a\u0435\u0442 \u0434\u043b\u044f \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u043e\u0432 BenchmarkDotNet \u0438 \u043f\u0438\u0448\u0435\u043c \u0442\u0435\u0441\u0442:  <\/p>\n<pre><code class=\"cs\">public class AnimalFactoryPerformanceTests {     [ParamsAllValues]     public bool IsCat { get; set; }      [Benchmark]     public void CreateAnimalByTernaryOperator() =&gt;         AnimalsFactory.CreateAnimalByTernaryOperator(IsCat);      [Benchmark]     public void CreateAnimalByIfElseOperator() =&gt;         AnimalsFactory.CreateAnimalByIfElseOperator(IsCat); } <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u043e\u0432:  <\/p>\n<pre><code class=\"cs\">|                         \u041c\u0435\u0442\u043e\u0434 | IsCat |    \u0412\u0440\u0435\u043c\u044f | |------------------------------ |------ |---------:| | CreateAnimalByTernaryOperator | False | 1.357 ns | | CreateAnimalByTernaryOperator |  True | 1.655 ns | |------------------------------ |------ |---------:| |  CreateAnimalByIfElseOperator | False | 1.636 ns | |  CreateAnimalByIfElseOperator |  True | 1.360 ns | <\/code><\/pre>\n<p>  \u0423\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0447\u0442\u043e \u00ab\u0434\u043b\u044f \u0441\u043e\u0431\u0430\u043a\u0438\u00bb \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0435\u0440\u043d\u0430\u0440\u043d\u044b\u0439, \u0430 \u00ab\u0434\u043b\u044f \u043a\u043e\u0442\u0430\u00bb \u2014 if-else \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440. <br \/>  \u0421\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 IL-\u043a\u043e\u0434 \u043c\u0435\u0442\u043e\u0434\u0430 \u0441 \u0442\u0435\u0440\u043d\u0430\u0440\u043d\u044b\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c:  <\/p>\n<pre><code class=\"cs\">CreateAnimalByTernaryOperator(bool isCat) {     IL_0000: ldarg.0      \/\/ isCat     IL_0001: brtrue.s     IL_0009     IL_0003: newobj       instance void AnimalPerformance.Dog::.ctor()     IL_0008: ret     IL_0009: newobj       instance void AnimalPerformance.Cat::.ctor()     IL_000e: ret } <\/code><\/pre>\n<p>  \u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <i>Dog<\/i> \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u0441\u044f \u043a\u043e\u043c\u0430\u043d\u0434\u044b <i>IL_0000 \u2014 IL_0008<\/i>, \u0432 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <i>Cat<\/i> \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u043e\u0434 (<i>IL_0001: brtrue.s IL_0009<\/i>) <\/p>\n<p>  \u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0433\u0430\u0434\u0430\u0442\u044c\u0441\u044f, \u0434\u043b\u044f if-else \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f IL-\u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432 \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <i>Cat<\/i>. \u0412 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a \u043e\u0431\u044a\u0435\u043a\u0442 <i>Dog<\/i> \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u043e\u0434:  <\/p>\n<pre><code class=\"cs\">CreateAnimalByIfElseOperator(bool isCat) {     IL_0000: ldarg.0      \/\/ isCat     IL_0001: brfalse.s    IL_0009     IL_0003: newobj       instance void AnimalPerformance.Cat::.ctor()     IL_0008: ret     IL_0009: newobj       instance void AnimalPerformance.Dog::.ctor()     IL_000e: ret } <\/code><\/pre>\n<p>  \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u0444\u0430\u0431\u0440\u0438\u043a\u0443 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u00ab\u041f\u043e\u043f\u0443\u0433\u0430\u044f\u00bb \u0438 \u043d\u043e\u0432\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0441 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c <i>switch<\/i>:  <\/p>\n<pre><code class=\"cs\">public static class AnimalFactory {     public static Animal CreateAnimalByTernaryOperator(AnimalType animalType)     {         return animalType == AnimalType.Cat             ? new Cat()             : animalType == AnimalType.Dog                 ? (Animal)new Dog()                 : new Parrot();     }      public static Animal CreateAnimalByIfElseOperator(AnimalType animalType)     {         if (animalType == AnimalType.Cat)             return new Cat();          if (animalType == AnimalType.Dog)             return new Dog();          return new Parrot();     }      public static Animal CreateAnimalBySwitchOperator(AnimalType animalType)     {         switch (animalType)         {             case AnimalType.Cat:                 return new Cat();             case AnimalType.Dog:                 return new Dog();             case AnimalType.Parrot:                 return new Parrot();             default:                 throw new InvalidOperationException();         }     } } <\/code><\/pre>\n<p>  \u041a\u0430\u043a\u043e\u0439 \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0431\u044b\u0441\u0442\u0440\u0435\u0435?<\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u043e\u0432<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">|                         \u041c\u0435\u0442\u043e\u0434 | AnimalType |    \u0412\u0440\u0435\u043c\u044f | |------------------------------ |----------- |---------:| | CreateAnimalByTernaryOperator |        Cat | 2.490 ns | | CreateAnimalByTernaryOperator |        Dog | 2.515 ns | | CreateAnimalByTernaryOperator |     Parrot | 2.333 ns | |------------------------------ |----------- |---------:| |  CreateAnimalByIfElseOperator |        Cat | 2.368 ns | |  CreateAnimalByIfElseOperator |        Dog | 2.545 ns | |  CreateAnimalByIfElseOperator |     Parrot | 2.735 ns | |------------------------------ |----------- |---------:| |  CreateAnimalBySwitchOperator |        Cat | 2.747 ns | |  CreateAnimalBySwitchOperator |        Dog | 2.730 ns | |  CreateAnimalBySwitchOperator |     Parrot | 2.722 ns | <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">IL-\u043a\u043e\u0434<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">CreateAnimalByTernaryOperator(AnimalsFactory.AnimalType animalType) {     IL_0000: ldarg.0      \/\/ animalType     IL_0001: brfalse.s    IL_0013     IL_0003: ldarg.0      \/\/ animalType     IL_0004: ldc.i4.1     IL_0005: beq.s        IL_000d     IL_0007: newobj       instance void AnimalsFactory.Parrot::.ctor()     IL_000c: ret     IL_000d: newobj       instance void AnimalsFactory.Dog::.ctor()     IL_0012: ret     IL_0013: newobj       instance void AnimalsFactory.Cat::.ctor()     IL_0018: ret }  CreateAnimalByIfElseOperator(AnimalsFactory.AnimalType animalType) {     IL_0000: ldarg.0      \/\/ animalType     IL_0001: brtrue.s     IL_0009     IL_0003: newobj       instance void AnimalsFactory.Cat::.ctor()     IL_0008: ret     IL_0009: ldarg.0      \/\/ animalType     IL_000a: ldc.i4.1     IL_000b: bne.un.s     IL_0013     IL_000d: newobj       instance void AnimalsFactory.Dog::.ctor()     IL_0012: ret     IL_0013: newobj       instance void AnimalsFactory.Parrot::.ctor()     IL_0018: ret  }  CreateOtherAnimalBySwitchOperator(AnimalsFactory.AnimalType animalType) {     IL_0000: ldarg.0      \/\/ animalType     IL_0001: switch       (IL_0014, IL_001a, IL_0020)     IL_0012: br.s         IL_0026     IL_0014: newobj       instance void AnimalsFactory.Cat::.ctor()     IL_0019: ret     IL_001a: newobj       instance void AnimalsFactory.Dog::.ctor()     IL_001f: ret     IL_0020: newobj       instance void AnimalsFactory.Parrot::.ctor()     IL_0025: ret     IL_0026: newobj       instance void System.InvalidOperationException::.ctor()     IL_002b: throw } <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  <b>\u0412\u044b\u0432\u043e\u0434 1:<\/b> \u0414\u043b\u044f \u0442\u0435\u0440\u043d\u0430\u0440\u043d\u043e\u0433\u043e \u0438 if-else \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u043e\u0437\u043e\u0448\u043b\u0438 \u0432 \u043f\u043e\u0442\u043e\u043a\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<br \/>  <b>\u0412\u044b\u0432\u043e\u0434 2:<\/b> C# \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 switch \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u0432 IL-\u043a\u043e\u0434\u0435 \u0432 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e switch \u0438 \u0432 \u0441\u0440\u0435\u0434\u043d\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u044f.<br \/>  <b>\u0412\u044b\u0432\u043e\u0434 3:<\/b> \u0412\u0441\u0451, \u0447\u0442\u043e \u0432\u044b\u0448\u0435, \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f .NET Framework v.4.8. \u041f\u0440\u043e\u0433\u043d\u0430\u0432 \u0442\u0435 \u0436\u0435 \u0441\u0430\u043c\u044b\u0435 \u0442\u0435\u0441\u0442\u044b \u043d\u0430 .NetCore \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u0441\u043e\u0432\u0441\u0435\u043c \u0434\u0440\u0443\u0433\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0435\u0449\u0451 \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u043a\u0430\u043a-\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c.  <\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u043e\u0432 .NetCore 3.0<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cs\">|                         \u041c\u0435\u0442\u043e\u0434 | AnimalType |    \u0412\u0440\u0435\u043c\u044f | |------------------------------ |----------- |---------:| | CreateAnimalByTernaryOperator |        Cat | 3.046 ns | | CreateAnimalByTernaryOperator |        Dog | 2.984 ns | | CreateAnimalByTernaryOperator |     Parrot | 3.019 ns | |------------------------------ |----------- |---------:| |  CreateAnimalByIfElseOperator |        Cat | 2.980 ns | |  CreateAnimalByIfElseOperator |        Dog | 2.977 ns | |  CreateAnimalByIfElseOperator |     Parrot | 3.103 ns | |------------------------------ |----------- |---------:| |  CreateAnimalBySwitchOperator |        Cat | 3.519 ns | |  CreateAnimalBySwitchOperator |        Dog | 3.533 ns | |  CreateAnimalBySwitchOperator |     Parrot | 3.312 ns | <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  \u041f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440: Intel&reg; Core(TM) i7-7700K <br \/>  <a href=\"https:\/\/github.com\/sokolnikov90\/AnimalsFactory\" rel=\"nofollow\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438<\/a><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/501676\/\"> https:\/\/habr.com\/ru\/post\/501676\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/501676\/\">\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043d\u0430 \u0440\u0435\u0432\u044c\u044e \u043f\u0440\u0438\u043b\u0435\u0442\u0435\u043b\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u0430\u044f \u0444\u0430\u0431\u0440\u0438\u043a\u0430 \u0436\u0438\u0432\u043e\u0442\u043d\u044b\u0445:  <\/p>\n<pre><code class=\"cs\">public static class AnimalsFactory {     public static Animal CreateAnimalByTernaryOperator(bool isCat)     {         return isCat ? (Animal)new Cat() : new Dog();     } } <\/code><\/pre>\n<p>  \u0412 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0437 \u0440\u0430\u0441\u0441\u0442\u0440\u043e\u0438\u043b\u0441\u044f, \u0447\u0442\u043e C# \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u043a\u0430\u0441\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 Cat \u043a Animal. \u041d\u043e \u043f\u0443\u0441\u0442\u044c \u043b\u0443\u0447\u0448\u0435 \u0431\u0443\u0434\u0435\u0442 \u043a\u0430\u0441\u0442, \u0432\u0435\u0434\u044c \u0447\u0435\u0440\u0435\u0437 if-else \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 \u043a\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0435\u0449\u0451 \u0434\u043b\u0438\u043d\u043d\u0435\u0435:  <\/p>\n<pre><code class=\"cs\">public static class AnimalsFactory {     public static Animal CreateAnimalByIfElseOperator(bool isCat)     {         if (isCat)             return new Cat();          return new Dog();     } } <\/code><\/pre>\n<p>  \u041e\u0442\u0432\u043b\u0435\u0447\u0451\u043c\u0441\u044f \u043d\u0430 \u043c\u0438\u043d\u0443\u0442\u0443 \u043e\u0442 \u0440\u0435\u0432\u044c\u044e \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f:<br \/>   \u2014 \u0431\u0443\u0434\u0435\u0442 \u043b\u0438 \u043e\u0442\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f IL-\u043a\u043e\u0434 \u0432 \u044d\u0442\u0438\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445?<br \/>   \u2014 \u0431\u0443\u0434\u0435\u0442 \u043b\u0438 \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0432 \u0432\u044b\u0438\u0433\u0440\u044b\u0448\u0435 \u043f\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438?  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-303486","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/303486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=303486"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/303486\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=303486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=303486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=303486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}