c#单例模式设置属性
C#概念 (C# CONCEPTS)
先决条件 (Prerequisites)
Please go through the articles below for a basic understanding of C# 8.0 concepts required in this article.
请仔细阅读下面的文章,以基本了解本文所需的C#8.0概念。
开始吧 (Lets Start)
Let’s take an example of Toll Calculator and see how pattern matching helps to write an algorithm for that.
让我们以通行费计算器为例,看看模式匹配如何帮助编写算法。
整篇文章中使用的实体类 (Entity class used throughout the article)
public class Car
{
public int PassengerCount { get; set; }
}public class DeliveryTruck
{
public int Weight { get; set; }
}public class Taxi
{
public int Fare { get; set; }
}public class Bus
{
public int Capacity { get; set; }
public int RidersCount { get; set; }
}
Example 1: Calculate toll fare as per following conditions:
示例1:根据以下条件计算通行费:
- If the vehicle is Car => 100 Rs 如果车辆是Car => 100 Rs
- If the vehicle is DeliveryTruck => 200 Rs 如果车辆是DeliveryTruck => 200 Rs
- If the vehicle is Bus => 150 Rs 如果车辆是公共汽车=> 150卢比
- If the vehicle is Taxi => 120 Rs 如果车辆是出租车=> 120 Rs
具有新开关语法的模式匹配程序 (Pattern matching program with new switch syntax)
If vehicle type matches with Car 100 is returned & so on. Notice the null & {} are default cases for object type.
如果车辆类型与Car 100相匹配,则返回等等。 请注意,null和{}是对象类型的默认情况。
Also, “_” can be used to program the default scenario. Refer new switch syntax.
另外,“ _”可用于对默认方案进行编程。 请参阅新的开关语法。
Its a much more cleaned & efficient way of coding & also recommended use single-letter variable names inside the switch syntax.
它是一种更加干净和有效的编码方式,并且建议在switch语法内使用单字母变量名称。
public static int TollFare(Object vehicleType) => vehicleType switch
{
Car c => 100,
DeliveryTruck d => 200,
Bus b => 150,
Taxi t => 120,
null => 0,
{ } => 0
};
测试以上程序 (Test above program)
Test examples from a console application standpoint. The below code illustrates how to call the above pattern-matching function from the main method.
从控制台应用程序角度测试示例。 以下代码说明了如何从main方法中调用上述模式匹配功能。
var car = new Car();
var taxi = new Taxi();
var bus = new Bus();
var truck = new DeliveryTruck();Console.WriteLine($"The toll for a car is {TollFare(car)}");
Console.WriteLine($"The toll for a taxi is {TollFare(taxi)}");
Console.WriteLine($"The toll for a bus is {TollFare(bus)}");
Console.WriteLine($"The toll for a truck is {TollFare(truck)}");
控制台输出 (Console Output)
The toll for a car is 100
The toll for a taxi is 120
The toll for a bus is 150
The toll for a truck is 200
Example 2: Add occupancy pricing based upon vehicle type
示例2:根据车辆类型添加入住价格
- Cars & taxis with “NO” passengers pay an extra 10 Rs. 乘客为“ NO”的汽车和出租车要多付10卢比。
- Cars & taxis with two passengers get a 10 Rs discount. 有两名乘客的汽车和出租车可享受10卢比的折扣。
- Cars & taxis with three or more passengers get a 20 Rs discount. 具有三名或更多乘客的汽车和出租车可享受20卢比的折扣。
- Buses that are less than 50% of passengers pay an extra 30 Rs. 少于乘客50%的巴士要多付30卢比。
- Buses that are more than 90% of passengers get a 40 Rs discount. 超过90%乘客的公共汽车可享受40卢比的折扣。
- Trucks over 5000 lbs, charged an extra 100 Rs. 超过5000磅的卡车加收100卢比。
- Light trucks under 3000 lbs, given a 20 Rs discount. 低于3000磅的轻型卡车,可享受20卢比的折扣。
模式匹配开关 (Pattern Matching Switch)
Refer pattern matching syntax with single & multiple property classes. Link
请参考具有单个和多个属性类的模式匹配语法。 链接
模式匹配-汽车实体 (Pattern Matching — Car Entity)
Car { PassengerCount: 0 } => 100 + 10,
Car { PassengerCount: 1 } => 100,
Car { PassengerCount: 2 } => 100 - 10,
Car c => 100 - 20,
模式匹配-出租车实体 (Pattern Matching — Taxi Entity)
Taxi {Fare:0 }=>100+10,
Taxi { Fare: 1 } => 100,
Taxi { Fare: 2 } => 100 - 10,
Taxi t => 100 - 20,
模式匹配-总线实体 (Pattern Matching — Bus Entity)
Bus b when ((double)b.RidersCount / (double)b.Capacity) < 0.50 => 150 + 30,Bus b when ((double)b.RidersCount / (double)b.Capacity) > 0.90 => 150 - 40,Bus b => 150,
模式匹配-送货卡车实体 (Pattern Matching — Delivery Truck Entity)
DeliveryTruck t when (t.Weight > 5000) => 200 + 100,
DeliveryTruck t when (t.Weight < 3000) => 200 - 20,
DeliveryTruck t => 200,
合并所有实体 (Combining all entities)
The below example highlights the advantages of pattern matching: the pattern branches are compiled in order. The compiler also warns about the unreachable code.
以下示例突出显示了模式匹配的优点:模式分支按顺序编译。 编译器还会警告无法访问的代码。
public static int OccupancyTypeTollFare(Object vehicleType) => vehicleType switch{Car { PassengerCount: 0 } => 100 + 10,
Car { PassengerCount: 1 } => 100,
Car { PassengerCount: 2 } => 100 - 10,
Car c => 100 - 20,Taxi {Fare:0 }=>100+10,
Taxi { Fare: 1 } => 100,
Taxi { Fare: 2 } => 100 - 10,
Taxi t => 100 - 20,Bus b when ((double)b.RidersCount / (double)b.Capacity) < 0.50 => 150 + 30,
Bus b when ((double)b.RidersCount / (double)b.Capacity) > 0.90 => 150 - 40,
Bus b => 150,DeliveryTruck t when (t.Weight > 5000) => 200 + 100,
DeliveryTruck t when (t.Weight < 3000) => 200 - 20,
DeliveryTruck t => 200,null => 0,
{ } => 0,};
测试以上程序 (Test above program)
Test examples from a console application standpoint. The below code illustrates how to call the above pattern-matching function from the main method.
从控制台应用程序角度测试示例。 以下代码说明了如何从main方法中调用上述模式匹配功能。
var car1 = new Car{ PassengerCount=2};
var taxi1 = new Taxi { Fare = 0 };
var bus1 = new Bus { Capacity = 100, RidersCount = 30 };
var truck1 = new DeliveryTruck { Weight = 30000 };Console.WriteLine($"The toll for a car is {OccupancyTypeTollFare(car1)}");
Console.WriteLine($"The toll for a taxi is {OccupancyTypeTollFare(taxi1)}");
Console.WriteLine($"The toll for a bus is {OccupancyTypeTollFare(bus1)}");
Console.WriteLine($"The toll for a truck is {OccupancyTypeTollFare(truck1)}");
控制台输出 (Console Output)
The toll for a car is 90
The toll for a taxi is 110
The toll for a bus is 180
The toll for a truck is 300
翻译自: https://medium.com/swlh/property-pattern-c-8-0-40925ae07b2c
c#单例模式设置属性