ソースを参照

访问者模式

圣杰 8 年 前
コミット
35fa3f1fb5

+ 8 - 2
DesignPattern.sln

@@ -1,7 +1,7 @@
 
 Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio 14
-VisualStudioVersion = 14.0.25420.1
+# Visual Studio 15
+VisualStudioVersion = 15.0.26228.9
 MinimumVisualStudioVersion = 10.0.40219.1
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryPattern", "FactoryPattern\FactoryPattern.csproj", "{B6F92CD5-5347-4F36-96CB-2ED8B9F66C25}"
 EndProject
@@ -37,6 +37,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MementoPattern", "MementoPa
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IteratorPattern", "IteratorPattern\IteratorPattern.csproj", "{04898517-CD60-419B-9C4D-2BE8A99C40FC}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisitorPattern", "VisitorPattern\VisitorPattern.csproj", "{02F820F2-CE4C-4E47-8CE8-4E7C0A234D42}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -111,6 +113,10 @@ Global
 		{04898517-CD60-419B-9C4D-2BE8A99C40FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{04898517-CD60-419B-9C4D-2BE8A99C40FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{04898517-CD60-419B-9C4D-2BE8A99C40FC}.Release|Any CPU.Build.0 = Release|Any CPU
+		{02F820F2-CE4C-4E47-8CE8-4E7C0A234D42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{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
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 6 - 0
VisitorPattern/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>

+ 24 - 0
VisitorPattern/Customer.cs

@@ -0,0 +1,24 @@
+using System.Text;
+using System.Threading.Tasks;
+
+namespace VisitorPattern
+{
+
+    /// <summary>
+    /// 客户类
+    /// </summary>
+    public class Customer
+    {
+        public int Id { get; set; }
+
+        public string NickName { get; set; }
+
+        public string RealName { get; set; }
+
+        public string Phone { get; set; }
+
+        public string Address { get; set; }
+
+        public string Zip { get; set; }
+    }
+}

+ 43 - 0
VisitorPattern/Distributor.cs

@@ -0,0 +1,43 @@
+using System;
+using System.Linq;
+
+namespace VisitorPattern
+{
+    /// <summary>
+    /// 收发货员
+    /// 对销售订单,进行发货处理
+    /// 对退货订单,进行收货处理
+    /// </summary>
+    public class Distributor : Visitor
+    {
+        public int Id { get; set; }
+        public string Name { get; set; }
+
+        public override void Visit(SaleOrder saleOrder)
+        {
+            Console.WriteLine($"开始为销售订单【{saleOrder.Id}】进行发货处理:", saleOrder.Id);
+
+            Console.WriteLine($"一共打包{saleOrder.OrderItems.Sum(line => line.Qty)}件商品。");
+            Console.WriteLine($"收货人:{saleOrder.Customer.RealName}");
+            Console.WriteLine($"联系电话:{saleOrder.Customer.Phone}");
+            Console.WriteLine($"收货地址:{saleOrder.Customer.Address}");
+            Console.WriteLine($"邮政编码:{saleOrder.Customer.Zip}");
+
+            Console.WriteLine($"订单【{saleOrder.Id}】发货完毕!" );
+            Console.WriteLine("==========================");
+        }
+
+        public override void Visit(ReturnOrder returnOrder)
+        {
+            Console.WriteLine($"收到来自【{returnOrder.Customer.NickName}】的退货订单【{returnOrder.Id}】,进行退货收货处理:");
+
+            foreach (var item in returnOrder.OrderItems)
+            {
+                Console.WriteLine($"【{item.Product.Name}】商品* {item.Qty}" );
+            }
+
+            Console.WriteLine($"退货订单【{returnOrder.Id}】收货处理完毕!" );
+            Console.WriteLine("==========================");
+        }
+    }
+}

+ 25 - 0
VisitorPattern/Order.cs

@@ -0,0 +1,25 @@
+using System;
+using System.Collections.Generic;
+
+namespace VisitorPattern
+{
+    /// <summary>
+    /// 订单抽象类
+    /// </summary>
+    public abstract class Order
+    {
+        public int Id { get; set; }
+
+        public Customer Customer { get; set; }
+
+        public DateTime CreatorDate { get; set; }
+
+        /// <summary>
+        /// 单据品相
+        /// </summary>
+        public List<OrderLine> OrderItems { get; set; }
+
+        public abstract void Accept(Visitor visitor);
+
+    }
+}

+ 21 - 0
VisitorPattern/OrderCenter.cs

@@ -0,0 +1,21 @@
+using System.Collections.Generic;
+
+namespace VisitorPattern
+{
+    /// <summary>
+    /// 订单中心
+    /// </summary>
+    public class OrderCenter : List<Order>
+    {
+        public void Accept(Visitor visitor)
+        {
+            var iterator = this.GetEnumerator();
+
+            while (iterator.MoveNext())
+            {
+                iterator.Current.Accept(visitor);
+            }
+        }
+
+    }
+}

+ 11 - 0
VisitorPattern/OrderLine.cs

@@ -0,0 +1,11 @@
+namespace VisitorPattern
+{
+    public class OrderLine
+    {
+        public int Id { get; set; }
+
+        public Product Product { get; set; }
+
+        public int Qty { get; set; }
+    }
+}

+ 40 - 0
VisitorPattern/Picker.cs

@@ -0,0 +1,40 @@
+using System;
+
+namespace VisitorPattern
+{
+    /// <summary>
+    /// 捡货员
+    /// 对销售订单,从仓库捡货。
+    /// 对退货订单,将收到的货品归放回仓库。
+    /// </summary>
+    public class Picker : Visitor
+    {
+        public int Id { get; set; }
+        public string Name { get; set; }
+
+        public override void Visit(SaleOrder saleOrder)
+        {
+            Console.WriteLine($"开始为销售订单【{saleOrder.Id}】进行销售捡货处理:");
+            foreach (var item in saleOrder.OrderItems)
+            {
+                Console.WriteLine($"【{item.Product.Name}】商品* {item.Qty}");
+            }
+
+            Console.WriteLine($"订单【{saleOrder.Id}】捡货完毕!");
+
+            Console.WriteLine("==========================");
+        }
+
+        public override void Visit(ReturnOrder returnOrder)
+        {
+            Console.WriteLine($"开始为退货订单【{returnOrder.Id}】进行退货捡货处理:");
+            foreach (var item in returnOrder.OrderItems)
+            {
+                Console.WriteLine($"【{item.Product.Name}】商品* {item.Qty}");
+            }
+
+            Console.WriteLine($"退货订单【{returnOrder.Id}】退货捡货完毕!", returnOrder.Id);
+            Console.WriteLine("==========================");
+        }
+    }
+}

+ 14 - 0
VisitorPattern/Product.cs

@@ -0,0 +1,14 @@
+namespace VisitorPattern
+{
+    /// <summary>
+    /// 产品类
+    /// </summary>
+    public class Product
+    {
+        public int Id { get; set; }
+
+        public string Name { get; set; }
+
+        public virtual decimal Price { get; set; }
+    }
+}

+ 59 - 0
VisitorPattern/Program.cs

@@ -0,0 +1,59 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+/// <summary>
+/// 访问者模式
+/// </summary>
+namespace VisitorPattern
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            Customer customer = new Customer
+            {
+                Id = 1,
+                NickName = "圣杰",
+                RealName = "圣杰",
+                Address = "深圳市南山区",
+                Phone = "135****9358",
+                Zip = "518000"
+            };
+
+            Product productA = new Product { Id = 1, Name = "小米5", Price = 1899 };
+            Product productB = new Product { Id = 2, Name = "小米5手机防爆膜", Price = 29 };
+            Product productC = new Product { Id = 3, Name = "小米5手机保护套", Price = 69 };
+
+            OrderLine line1 = new OrderLine { Id = 1, Product = productA, Qty = 1 };
+            OrderLine line2 = new OrderLine { Id = 1, Product = productB, Qty = 2 };
+            OrderLine line3 = new OrderLine { Id = 1, Product = productC, Qty = 3 };
+
+            //先买了个小米5和防爆膜
+            SaleOrder order1 = new SaleOrder { Id = 1, Customer = customer, CreatorDate = DateTime.Now, OrderItems = new List<OrderLine> { line1, line2 } };
+
+            //又买了个保护套
+            SaleOrder order2 = new SaleOrder { Id = 2, Customer = customer, CreatorDate = DateTime.Now, OrderItems = new List<OrderLine> { line3 } };
+
+            //把保护套都退了
+            ReturnOrder returnOrder = new ReturnOrder { Id = 3, Customer = customer, CreatorDate = DateTime.Now, OrderItems = new List<OrderLine> { line3 } };
+
+            OrderCenter orderCenter = new OrderCenter { order1, order2, returnOrder };
+
+
+            Picker picker = new Picker { Id = 110, Name = "捡货员110" };
+
+            Distributor distributor = new Distributor { Id = 111, Name = "发货货员111" };
+
+            //捡货员访问订单中心
+            orderCenter.Accept(picker);
+
+            //发货员访问订单中心
+            orderCenter.Accept(distributor);
+
+            Console.ReadLine();
+        }
+    }
+}

+ 36 - 0
VisitorPattern/Properties/AssemblyInfo.cs

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

+ 13 - 0
VisitorPattern/ReturnOrder.cs

@@ -0,0 +1,13 @@
+namespace VisitorPattern
+{
+    /// <summary>
+    /// 退货单
+    /// </summary>
+    public class ReturnOrder : Order
+    {
+        public override void Accept(Visitor visitor)
+        {
+            visitor.Visit(this);
+        }
+    }
+}

+ 13 - 0
VisitorPattern/SaleOrder.cs

@@ -0,0 +1,13 @@
+namespace VisitorPattern
+{
+    /// <summary>
+    /// 销售订单
+    /// </summary>
+    public class SaleOrder : Order
+    {
+        public override void Accept(Visitor visitor)
+        {
+            visitor.Visit(this);
+        }
+    }
+}

+ 11 - 0
VisitorPattern/Visitor.cs

@@ -0,0 +1,11 @@
+namespace VisitorPattern
+{
+    /// <summary>
+    /// 访问者
+    /// </summary>
+    public abstract class Visitor
+    {
+        public abstract void Visit(SaleOrder saleOrder);
+        public abstract void Visit(ReturnOrder returnOrder);
+    }
+}

+ 62 - 0
VisitorPattern/VisitorPattern.csproj

@@ -0,0 +1,62 @@
+<?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>{02F820F2-CE4C-4E47-8CE8-4E7C0A234D42}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>VisitorPattern</RootNamespace>
+    <AssemblyName>VisitorPattern</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="Customer.cs" />
+    <Compile Include="Distributor.cs" />
+    <Compile Include="Order.cs" />
+    <Compile Include="OrderCenter.cs" />
+    <Compile Include="OrderLine.cs" />
+    <Compile Include="Picker.cs" />
+    <Compile Include="Product.cs" />
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="ReturnOrder.cs" />
+    <Compile Include="SaleOrder.cs" />
+    <Compile Include="Visitor.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="App.config" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+</Project>