Interfaces, depending on inverted test unit
Interface is a treaty that stipulates that it must be open, only to be public;
static void Main(string[] args) { int[] num1 = new int[] { 1, 2, 3, 4, 5 }; Console.WriteLine(Sum(num1).ToString()); Console.WriteLine("=================="); Console.WriteLine(Avg(num1).ToString()); } static int Sum(int[] arr) { int result = 0; foreach (var item in arr)result += item; return result; } static double Avg(int[] arr) { int result = 0; double count = 0; foreach (var item in arr) { result += item; count++; }; return (result/count); } }
In the above code, if we are not the parameter type int [], but ArrayList type (type object stored)
A program overloaded method, a cast method in the interior:
static void Main(string[] args) { int[] num1 = new int[] { 1, 2, 3, 4, 5 }; ArrayList array = new ArrayList { 1, 2, 3, 4, 5 }; Console.WriteLine(Sum(array).ToString()); Console.WriteLine("=================="); Console.WriteLine(Avg(array).ToString()); } static int Sum(ArrayList arr) { int result = 0; foreach (var item in arr)result += (int)item; return result; } static double Avg(ArrayList arr) { int result = 0; double count = 0; foreach (var item in arr) { result += (int)item; count++; }; return (result/count); }
Summing and averaging in the above-mentioned problems, we want to call, that Party to provide a method for the Party, careful observation is not difficult to find that our Party is only required to be an iterative method parameters as a set on the line, see int [] ArrayList the base class, they all can be iterative, implements the IEnumerable Interface
static void Main(string[] args) { int[] num1 = new int[] { 1, 2, 3, 4, 5 }; ArrayList array = new ArrayList { 1, 2, 3, 4, 5 }; Console.WriteLine(Sum(num1).ToString()); Console.WriteLine("=================="); Console.WriteLine(Avg(array).ToString()); } static int Sum(IEnumerable arr) { int result = 0; foreach (var item in arr)result += (int)item; return result; } static double Avg(IEnumerable arr) { int result = 0; double count = 0; foreach (var item in arr) { result += (int)item; count++; }; return (result/count); }
Problem solved by this realization, the parameters change the abstract concrete, with a contractual relationship between supply and demand management.
The next example continues:
public class Program { static void Main(string[] args) { Engine engine = new Engine(); Car car = new Car(engine); car.Run(3); Console.WriteLine(car.Speed.ToString()); } } public class Engine { public int RPM { get;private set; } public void Work(int gas) { this.RPM = 1000 * gas; } } public class Car { public Engine engine { get;private set; } public int Speed { get;private set; } public Car(Engine engine) { this.engine = engine; } public void Run(int gas) { this.engine.Work(gas); this.Speed = this.engine.RPM / 100; } }
When the above code in the Work Engine problems can cause problems Car call, because Car Engine class and has a tight coupling
The reason to pursue low coupling: reduce the need for providers, just to meet the treaty would meet the requirements, it can be replaced.