1
0
shengjie_yan 8 жил өмнө
parent
commit
00617f84c4

+ 6 - 0
DesignPattern.sln

@@ -46,6 +46,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StatePattern", "StatePattern\StatePattern.csproj", "{7C82D171-CCD0-48B7-B91E-34F86918C401}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FlyweightPattern", "FlyweightPattern\FlyweightPattern.csproj", "{6C5F00EA-31B9-4868-95C7-BE61BD972205}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -128,6 +130,10 @@ Global
 		{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
+		{6C5F00EA-31B9-4868-95C7-BE61BD972205}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{6C5F00EA-31B9-4868-95C7-BE61BD972205}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{6C5F00EA-31B9-4868-95C7-BE61BD972205}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{6C5F00EA-31B9-4868-95C7-BE61BD972205}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

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

+ 22 - 0
FlyweightPattern/Email.cs

@@ -0,0 +1,22 @@
+using System;
+
+namespace FlyweightPattern
+{
+    public class Email
+    {
+        public string Receiver { get; set; }
+        public string Sender { get; }
+        public string Subject { get; }
+        public string Template { get; }
+        public string Signature { get; }
+
+        public Email(string sender, string subject, string template, string signature)
+        {
+            Sender = sender;
+            Subject = subject;
+            Template = template;
+            Signature = signature;
+        }
+
+    }
+}

+ 61 - 0
FlyweightPattern/EmailTemplateFactory.cs

@@ -0,0 +1,61 @@
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+
+namespace FlyweightPattern
+{
+    public static class EmailTemplateFactory
+    {
+        /// <summary>
+        /// 预置模板
+        /// </summary>
+        private static readonly Dictionary<string, string> SubjectAndContentMapping = new Dictionary<string, string>()
+        {
+            {
+                "待修复漏洞通知",
+                @"尊敬的用户:云盾检测到您的服务器存在phpwindv9任务中心GET型CSRF代码执行漏洞,
+                  目前已为您研发了漏洞补丁,可在云盾控制台进行一键修复。为避免该漏洞被黑客利用,
+                  建议您尽快修复该漏洞。您可以点击此处登录云盾 - 服务器安全(安骑士)控制台进行查看和修复"
+            },
+            {
+                "阿里云ECS即将到期通知",
+                @"您有1台云服务器ECS将于一周后正式到期。未续费的云服务器ECS实例到期后将停止服务,
+                  到期后数据为您保留7天,逾期未续费实例与磁盘会被释放,数据不可恢复。
+                  为了保证您的服务正常运行,请及时续费。"
+            },
+            {"阿里云故障通告", "您的服务器存在故障,请您了解!"},
+            {"阿里云升级通知", "我们将对阿里云进行升级,会存在服务器短暂不可用情况,请知悉!"}
+        };
+
+        /// <summary>
+        /// 定义对象池
+        /// </summary>
+        static readonly ConcurrentDictionary<string, Email> EmailTemplates = new ConcurrentDictionary<string, Email>();
+
+
+        /// <summary>
+        /// 根据主题获取模板
+        /// </summary>
+        /// <param name="subject"></param>
+        /// <returns></returns>
+        public static Email GetTemplate(string subject)
+        {
+            Email email = null;
+
+            if (!EmailTemplates.ContainsKey(subject))
+            {
+                string template;
+                SubjectAndContentMapping.TryGetValue(subject, out template);
+                email = new Email("[email protected]", subject, string.IsNullOrWhiteSpace(template) ? subject : template, "阿里云计算公司");
+                EmailTemplates.TryAdd(subject, email);
+            }
+            else
+            {
+                EmailTemplates.TryGetValue(subject, out email);
+            }
+
+            return email;
+
+        }
+
+    }
+}

+ 54 - 0
FlyweightPattern/FlyweightPattern.csproj

@@ -0,0 +1,54 @@
+<?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>{6C5F00EA-31B9-4868-95C7-BE61BD972205}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>FlyweightPattern</RootNamespace>
+    <AssemblyName>FlyweightPattern</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="Email.cs" />
+    <Compile Include="EmailTemplateFactory.cs" />
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="App.config" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+</Project>

+ 32 - 0
FlyweightPattern/Program.cs

@@ -0,0 +1,32 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace FlyweightPattern
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+
+            for (int i = 0; i < 2000000; i++)
+            {
+                string receiver = $"kehu{i}@qq.com";
+                //通过简单工厂维护的对象池获取已经封装好的内部状态的对象。
+                var email = EmailTemplateFactory.GetTemplate("阿里云漏洞修复");
+                //修改外部状态
+                email.Receiver = receiver;
+                SendEmail(email);
+            }
+
+            Console.ReadLine();
+        }
+
+        private static void SendEmail(Email email)
+        {
+            Console.WriteLine($"主题为『{email.Subject}』的邮件已发送至:{email.Receiver}");
+        }
+    }
+}

+ 36 - 0
FlyweightPattern/Properties/AssemblyInfo.cs

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

+ 2 - 0
README.md

@@ -55,4 +55,6 @@
 
 21. [自动驾驶谈谈『状态模式』](http://www.jianshu.com/p/42d4ca7316ad)
 
+22. [对象复用,『享元模式』](http://www.jianshu.com/p/3fb0b559602b)
+
 ![设计模式之禅](http://upload-images.jianshu.io/upload_images/2799767-4df489c0f630a241.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)