├── .gitignore ├── .idea └── .idea.replace-condition-logic-with-strategy │ └── .idea │ ├── contentModel.xml │ ├── encodings.xml │ ├── indexLayout.xml │ ├── modules.xml │ ├── projectSettingsUpdater.xml │ └── vcs.xml ├── replace-condition-logic-with-strategy.sln ├── replace-condition-logic-with-strategy.sln.DotSettings └── replace-condition-logic-with-strategy ├── Cart.cs ├── CartTests.cs ├── Properties └── AssemblyInfo.cs ├── packages.config └── replace-condition-logic-with-strategy.csproj /.gitignore: -------------------------------------------------------------------------------- 1 | bin/ 2 | obj/ 3 | /packages/ 4 | riderModule.iml 5 | /_ReSharper.Caches/ 6 | /.idea/* 7 | -------------------------------------------------------------------------------- /.idea/.idea.replace-condition-logic-with-strategy/.idea/contentModel.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/.idea.replace-condition-logic-with-strategy/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/.idea.replace-condition-logic-with-strategy/.idea/indexLayout.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/.idea.replace-condition-logic-with-strategy/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/.idea.replace-condition-logic-with-strategy/.idea/projectSettingsUpdater.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/.idea.replace-condition-logic-with-strategy/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /replace-condition-logic-with-strategy.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "replace-condition-logic-with-strategy", "replace-condition-logic-with-strategy\replace-condition-logic-with-strategy.csproj", "{3F0E4202-15FD-4915-9A1E-49CFDCD60412}" 4 | EndProject 5 | Global 6 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 7 | Debug|Any CPU = Debug|Any CPU 8 | Release|Any CPU = Release|Any CPU 9 | EndGlobalSection 10 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 11 | {3F0E4202-15FD-4915-9A1E-49CFDCD60412}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 12 | {3F0E4202-15FD-4915-9A1E-49CFDCD60412}.Debug|Any CPU.Build.0 = Debug|Any CPU 13 | {3F0E4202-15FD-4915-9A1E-49CFDCD60412}.Release|Any CPU.ActiveCfg = Release|Any CPU 14 | {3F0E4202-15FD-4915-9A1E-49CFDCD60412}.Release|Any CPU.Build.0 = Release|Any CPU 15 | EndGlobalSection 16 | EndGlobal 17 | -------------------------------------------------------------------------------- /replace-condition-logic-with-strategy.sln.DotSettings: -------------------------------------------------------------------------------- 1 |  2 | True -------------------------------------------------------------------------------- /replace-condition-logic-with-strategy/Cart.cs: -------------------------------------------------------------------------------- 1 | #region 2 | 3 | using System; 4 | 5 | #endregion 6 | 7 | namespace replace_condition_logic_with_strategy 8 | { 9 | public class Cart 10 | { 11 | public double shippingFee(string shipper, double length, double width, double height, double weight) 12 | { 13 | if (shipper.Equals("black cat")) 14 | { 15 | if (weight > 20) 16 | { 17 | return 500; 18 | } 19 | else 20 | { 21 | return 100 + weight * 10; 22 | } 23 | } 24 | else if (shipper.Equals("hsinchu")) 25 | { 26 | double size = length * width * height; 27 | if (length > 100 || width > 100 || height > 100) 28 | { 29 | return size * 0.00002 * 1100 + 500; 30 | } 31 | else 32 | { 33 | return size * 0.00002 * 1200; 34 | } 35 | } 36 | else if (shipper.Equals("post office")) 37 | { 38 | double feeByWeight = 80 + weight * 10; 39 | double size = length * width * height; 40 | double feeBySize = size * 0.00002 * 1100; 41 | return feeByWeight < feeBySize ? feeByWeight : feeBySize; 42 | } 43 | else 44 | { 45 | throw new ArgumentException("shipper not exist"); 46 | } 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /replace-condition-logic-with-strategy/CartTests.cs: -------------------------------------------------------------------------------- 1 | #region 2 | 3 | using NUnit.Framework; 4 | 5 | #endregion 6 | 7 | namespace replace_condition_logic_with_strategy 8 | { 9 | [TestFixture] 10 | public class CartTests 11 | { 12 | private readonly string blackCat = "black cat"; 13 | private readonly Cart cart = new Cart(); 14 | private readonly string hsinchu = "hsinchu"; 15 | private readonly string postOffice = "post office"; 16 | 17 | [Test] 18 | public void black_cat_with_light_weight() 19 | { 20 | double shippingFee = cart.shippingFee(blackCat, 30, 20, 10, 5); 21 | feeShouldBe(150, shippingFee); 22 | } 23 | 24 | [Test] 25 | public void black_cat_with_heavy_weight() 26 | { 27 | double shippingFee = cart.shippingFee(blackCat, 30, 20, 10, 50); 28 | feeShouldBe(500, shippingFee); 29 | } 30 | 31 | [Test] 32 | public void hsinchu_with_small_size() 33 | { 34 | double shippingFee = cart.shippingFee(hsinchu, 30, 20, 10, 50); 35 | feeShouldBe(144, shippingFee); 36 | } 37 | 38 | [Test] 39 | public void hsinchu_with_huge_size() 40 | { 41 | double shippingFee = cart.shippingFee(hsinchu, 100, 20, 10, 50); 42 | feeShouldBe(480, shippingFee); 43 | } 44 | 45 | [Test] 46 | public void post_office_by_weight() 47 | { 48 | double shippingFee = cart.shippingFee(postOffice, 100, 20, 10, 3); 49 | feeShouldBe(110, shippingFee); 50 | } 51 | 52 | [Test] 53 | public void post_office_by_size() 54 | { 55 | double shippingFee = cart.shippingFee(postOffice, 100, 20, 10, 300); 56 | feeShouldBe(440, shippingFee); 57 | } 58 | 59 | private void feeShouldBe(double expected, double shippingFee) 60 | { 61 | Assert.AreEqual(expected, shippingFee); 62 | } 63 | } 64 | } -------------------------------------------------------------------------------- /replace-condition-logic-with-strategy/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | #region 2 | 3 | using System.Reflection; 4 | using System.Runtime.InteropServices; 5 | 6 | #endregion 7 | 8 | // General Information about an assembly is controlled through the following 9 | // set of attributes. Change these attribute values to modify the information 10 | // associated with an assembly. 11 | [assembly: AssemblyTitle("replace_condition_logic_with_strategy")] 12 | [assembly: AssemblyDescription("")] 13 | [assembly: AssemblyConfiguration("")] 14 | [assembly: AssemblyCompany("")] 15 | [assembly: AssemblyProduct("replace_condition_logic_with_strategy")] 16 | [assembly: AssemblyCopyright("Copyright © 2020")] 17 | [assembly: AssemblyTrademark("")] 18 | [assembly: AssemblyCulture("")] 19 | 20 | // Setting ComVisible to false makes the types in this assembly not visible 21 | // to COM components. If you need to access a type in this assembly from 22 | // COM, set the ComVisible attribute to true on that type. 23 | [assembly: ComVisible(false)] 24 | 25 | // The following GUID is for the ID of the typelib if this project is exposed to COM 26 | [assembly: Guid("3F0E4202-15FD-4915-9A1E-49CFDCD60412")] 27 | 28 | // Version information for an assembly consists of the following four values: 29 | // 30 | // Major Version 31 | // Minor Version 32 | // Build Number 33 | // Revision 34 | // 35 | // You can specify all the values or you can default the Build and Revision Numbers 36 | // by using the '*' as shown below: 37 | // [assembly: AssemblyVersion("1.0.*")] 38 | [assembly: AssemblyVersion("1.0.0.0")] 39 | [assembly: AssemblyFileVersion("1.0.0.0")] -------------------------------------------------------------------------------- /replace-condition-logic-with-strategy/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /replace-condition-logic-with-strategy/replace-condition-logic-with-strategy.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | Debug 7 | AnyCPU 8 | {3F0E4202-15FD-4915-9A1E-49CFDCD60412} 9 | Library 10 | Properties 11 | replace_condition_logic_with_strategy 12 | replace_condition_logic_with_strategy 13 | v4.6.1 14 | 512 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | ..\packages\NUnit.3.12.0\lib\net45\nunit.framework.dll 38 | True 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}. 57 | 58 | 59 | 60 | 67 | 68 | 69 | --------------------------------------------------------------------------------