shengjie_yan %!s(int64=8) %!d(string=hai) anos
pai
achega
07ab012c65

+ 11 - 0
DesignPattern.sln

@@ -39,6 +39,13 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IteratorPattern", "Iterator
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisitorPattern", "VisitorPattern\VisitorPattern.csproj", "{02F820F2-CE4C-4E47-8CE8-4E7C0A234D42}"
 EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4BDF7BDF-B443-4CD7-8105-0164BC5D5F0C}"
+	ProjectSection(SolutionItems) = preProject
+		README.md = README.md
+	EndProjectSection
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StatePattern", "StatePattern\StatePattern.csproj", "{7C82D171-CCD0-48B7-B91E-34F86918C401}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -117,6 +124,10 @@ Global
 		{02F820F2-CE4C-4E47-8CE8-4E7C0A234D42}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{02F820F2-CE4C-4E47-8CE8-4E7C0A234D42}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{02F820F2-CE4C-4E47-8CE8-4E7C0A234D42}.Release|Any CPU.Build.0 = Release|Any CPU
+		{7C82D171-CCD0-48B7-B91E-34F86918C401}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{7C82D171-CCD0-48B7-B91E-34F86918C401}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{7C82D171-CCD0-48B7-B91E-34F86918C401}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{7C82D171-CCD0-48B7-B91E-34F86918C401}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 2 - 0
README.md

@@ -53,4 +53,6 @@
 
 20. [这一次数据说了算,『访问者模式』](http://www.jianshu.com/p/3aa45b6be7ae)
 
+21. [自动驾驶谈谈『状态模式』](http://www.jianshu.com/p/42d4ca7316ad)
+
 ![设计模式之禅](http://upload-images.jianshu.io/upload_images/2799767-4df489c0f630a241.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

+ 6 - 0
StatePattern/App.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<configuration>
+    <startup> 
+        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
+    </startup>
+</configuration>

+ 47 - 0
StatePattern/Car.cs

@@ -0,0 +1,47 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace StatePattern
+{
+    /// <summary>
+    /// 汽车
+    /// </summary>
+    public class Car
+    {
+        public string Name { get; set; }
+
+        public Car()
+        {
+            this.CurrentCarState = StopState;//初始状态为停车状态
+        }
+
+        internal static ICarState StopState = new StopState();
+        internal static ICarState RunState = new RuningState();
+        internal static ICarState SpeedDownState = new SpeedDownState();
+        internal static ICarState SpeedUpState = new SpeedUpState();
+
+        public ICarState CurrentCarState { get; set; }
+
+        public void Run()
+        {
+            this.CurrentCarState.Drive(this);
+        }
+
+        public void Stop()
+        {
+            this.CurrentCarState.Stop(this);
+        }
+
+        public void SpeedUp()
+        {
+            this.CurrentCarState.SpeedUp(this);
+        }
+        public void SpeedDown()
+        {
+            this.CurrentCarState.SpeedDown(this);
+        }
+    }
+}

+ 35 - 0
StatePattern/ICarState.cs

@@ -0,0 +1,35 @@
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace StatePattern
+{
+    /// <summary>
+    /// 状态接口类
+    /// </summary>
+    public interface ICarState
+    {
+        /// <summary>
+        /// 启动
+        /// </summary>
+        void Drive(Car car);
+
+        /// <summary>
+        /// 停车
+        /// </summary>
+        void Stop(Car car);
+
+        /// <summary>
+        /// 加速
+        /// </summary>
+        /// <param name="car"></param>
+        void SpeedUp(Car car);
+
+        /// <summary>
+        /// 减速
+        /// </summary>
+        /// <param name="car"></param>
+        void SpeedDown(Car car);
+    }
+}

+ 22 - 0
StatePattern/Program.cs

@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace StatePattern
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            Car tesla = new Car() {Name = "特斯拉 Model S"};
+            tesla.Run();
+            tesla.SpeedUp();
+            tesla.SpeedDown();
+            tesla.Stop();
+
+            Console.WriteLine();
+        }
+    }
+}

+ 36 - 0
StatePattern/Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// 有关程序集的一般信息由以下
+// 控制。更改这些特性值可修改
+// 与程序集关联的信息。
+[assembly: AssemblyTitle("StatePattern")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("StatePattern")]
+[assembly: AssemblyCopyright("Copyright ©  2017")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// 将 ComVisible 设置为 false 会使此程序集中的类型
+//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
+//请将此类型的 ComVisible 特性设置为 true。
+[assembly: ComVisible(false)]
+
+// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
+[assembly: Guid("7c82d171-ccd0-48b7-b91e-34f86918c401")]
+
+// 程序集的版本信息由下列四个值组成: 
+//
+//      主版本
+//      次版本
+//      生成号
+//      修订号
+//
+// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
+// 方法是按如下所示使用“*”: :
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

+ 33 - 0
StatePattern/RuningState.cs

@@ -0,0 +1,33 @@
+using System;
+
+namespace StatePattern
+{
+    /// <summary>
+    /// 运行状态
+    /// </summary>
+    public class RuningState : ICarState
+    {
+        public void Drive(Car car)
+        {
+            Console.WriteLine("车辆正在自动驾驶!");
+        }
+
+        public void Stop(Car car)
+        {
+            Console.WriteLine("车辆已停止!");
+            car.CurrentCarState = Car.StopState;
+        }
+
+        public void SpeedUp(Car car)
+        {
+            Console.WriteLine("路况良好,开始加速行驶!");
+            car.CurrentCarState = Car.SpeedUpState;
+        }
+
+        public void SpeedDown(Car car)
+        {
+            Console.WriteLine("路况一般,开始加速行驶!");
+            car.CurrentCarState = Car.SpeedDownState;
+        }
+    }
+}

+ 32 - 0
StatePattern/SpeedDownState.cs

@@ -0,0 +1,32 @@
+using System;
+
+namespace StatePattern
+{
+    /// <summary>
+    /// 减速状态
+    /// </summary>
+    public class SpeedDownState : ICarState
+    {
+        public void Drive(Car car)
+        {
+            Console.WriteLine("车辆正在自动驾驶!");
+        }
+
+        public void Stop(Car car)
+        {
+            Console.WriteLine("车辆已停止!");
+            car.CurrentCarState = Car.StopState;
+        }
+
+        public void SpeedUp(Car car)
+        {
+            Console.WriteLine("路况良好,加速行驶!");
+            car.CurrentCarState = Car.SpeedUpState;
+        }
+
+        public void SpeedDown(Car car)
+        {
+            Console.WriteLine("车辆正在减速行驶!");
+        }
+    }
+}

+ 32 - 0
StatePattern/SpeedUpState.cs

@@ -0,0 +1,32 @@
+using System;
+
+namespace StatePattern
+{
+    /// <summary>
+    /// 加速状态
+    /// </summary>
+    public class SpeedUpState : ICarState
+    {
+        public void Drive(Car car)
+        {
+            Console.WriteLine("车辆正在自动驾驶!");
+        }
+
+        public void Stop(Car car)
+        {
+            Console.WriteLine("车辆已停止!");
+            car.CurrentCarState = Car.StopState;
+        }
+
+        public void SpeedUp(Car car)
+        {
+            Console.WriteLine("车辆正在加速行驶!");
+        }
+
+        public void SpeedDown(Car car)
+        {
+            Console.WriteLine("路况一般,减速行驶!");
+            car.CurrentCarState = Car.SpeedDownState;
+        }
+    }
+}

+ 58 - 0
StatePattern/StatePattern.csproj

@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{7C82D171-CCD0-48B7-B91E-34F86918C401}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>StatePattern</RootNamespace>
+    <AssemblyName>StatePattern</AssemblyName>
+    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Core" />
+    <Reference Include="System.Xml.Linq" />
+    <Reference Include="System.Data.DataSetExtensions" />
+    <Reference Include="Microsoft.CSharp" />
+    <Reference Include="System.Data" />
+    <Reference Include="System.Net.Http" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="ICarState.cs" />
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Car.cs" />
+    <Compile Include="RuningState.cs" />
+    <Compile Include="SpeedDownState.cs" />
+    <Compile Include="SpeedUpState.cs" />
+    <Compile Include="StopState.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="App.config" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+</Project>

+ 31 - 0
StatePattern/StopState.cs

@@ -0,0 +1,31 @@
+using System;
+
+namespace StatePattern
+{
+    /// <summary>
+    /// 停车状态
+    /// </summary>
+    public class StopState : ICarState
+    {
+        public void Drive(Car car)
+        {
+            Console.WriteLine($"{car.Name}已启动,开始自动驾驶!");
+            car.CurrentCarState = Car.RunState;
+        }
+
+        public void Stop(Car car)
+        {
+            Console.WriteLine("车辆已停止!");
+        }
+
+        public void SpeedUp(Car car)
+        {
+            Console.WriteLine("车辆已停止!");
+        }
+
+        public void SpeedDown(Car car)
+        {
+            Console.WriteLine("车辆已停止!");
+        }
+    }
+}