소스 검색

添加项目文件。

向羽 孟 8 달 전
부모
커밋
b07e9fe0ee
7개의 변경된 파일336개의 추가작업 그리고 0개의 파일을 삭제
  1. 6 0
      ConsoleApp1/App.config
  2. 55 0
      ConsoleApp1/ConsoleApp1.csproj
  3. 54 0
      ConsoleApp1/Program.cs
  4. 33 0
      ConsoleApp1/Properties/AssemblyInfo.cs
  5. 85 0
      ConsoleApp1/guachazhe_weituo.cs
  6. 81 0
      ConsoleApp1/guanchazhe_jieko.cs
  7. 22 0
      观察者模式.sln

+ 6 - 0
ConsoleApp1/App.config

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

+ 55 - 0
ConsoleApp1/ConsoleApp1.csproj

@@ -0,0 +1,55 @@
+<?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>{484AC5E7-C0B7-4125-BE9A-14A00D713EF2}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>ConsoleApp1</RootNamespace>
+    <AssemblyName>ConsoleApp1</AssemblyName>
+    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
+    <Deterministic>true</Deterministic>
+  </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="guachazhe_weituo.cs" />
+    <Compile Include="guanchazhe_jieko.cs" />
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="App.config" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+</Project>

+ 54 - 0
ConsoleApp1/Program.cs

@@ -0,0 +1,54 @@
+using ConsoleApp;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+//using static ConsoleApp1.guanchazhe_jieko;
+
+namespace ConsoleApp1
+{
+    // 使用观察者模式的示例
+    //public class Program
+    //{
+    //    public static void Main(string[] args)
+    //    {
+    //        ISubject subject = new ConcreteSubject();
+
+    //        IObserver observer1 = new ConcreteObserver("Observer 1");
+    //        IObserver observer2 = new ConcreteObserver("Observer 2");
+
+    //        subject.RegisterObserver(observer1);
+    //        subject.RegisterObserver(observer2);
+
+    //        subject.SetState("New state");
+    //        subject.NotifyObservers();
+    //    }
+    //}
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            // 创建主题对象
+            Subject subject = new Subject();
+
+            // 创建观察者对象
+            ConcreteObserverA observerA = new ConcreteObserverA();
+            ConcreteObserverB observerB = new ConcreteObserverB();
+
+            // 注册观察者
+            subject.RegisterObserver(observerA);
+            subject.RegisterObserver(observerB);
+
+            // 改变状态并通知观察者
+            subject.SetState("New State 1");
+            subject.SetState("New State 2");
+
+            // 注销观察者(可选)
+            subject.UnregisterObserver(observerA);
+
+            // 再次改变状态,只通知剩余的观察者
+            subject.SetState("New State 3");
+        }
+    }
+}

+ 33 - 0
ConsoleApp1/Properties/AssemblyInfo.cs

@@ -0,0 +1,33 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// 有关程序集的一般信息由以下
+// 控制。更改这些特性值可修改
+// 与程序集关联的信息。
+[assembly: AssemblyTitle("ConsoleApp1")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("ConsoleApp1")]
+[assembly: AssemblyCopyright("Copyright ©  2024")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// 将 ComVisible 设置为 false 会使此程序集中的类型
+//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
+//请将此类型的 ComVisible 特性设置为 true。
+[assembly: ComVisible(false)]
+
+// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
+[assembly: Guid("484ac5e7-c0b7-4125-be9a-14a00d713ef2")]
+
+// 程序集的版本信息由下列四个值组成: 
+//
+//      主版本
+//      次版本
+//      生成号
+//      修订号
+//
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

+ 85 - 0
ConsoleApp1/guachazhe_weituo.cs

@@ -0,0 +1,85 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ConsoleApp
+{
+    // 定义观察者接口
+    public interface IObserver
+    {
+        void OnUpdate(string message);
+    }
+
+    // 主题类
+    public class Subject
+    {
+        private List<IObserver> observers = new List<IObserver>();
+        private string state;
+
+        // 委托类型,用于表示通知方法
+        public delegate void NotifyHandler(string message);
+        public event NotifyHandler OnNotify;
+
+        // 注册观察者
+        public void RegisterObserver(IObserver observer)
+        {
+            observers.Add(observer);
+            // 使用委托订阅事件(这一步是可选的,因为我们会直接调用OnNotify)
+            // OnNotify += observer.OnUpdate; // 这种方式需要在注销时取消订阅
+        }
+
+        // 注销观察者
+        public void UnregisterObserver(IObserver observer)
+        {
+            observers.Remove(observer);
+            // 使用委托取消订阅事件(如果使用上面注释的代码)
+            // OnNotify -= observer.OnUpdate;
+        }
+
+        // 通知所有观察者
+        protected virtual void NotifyObservers()
+        {
+            string message = $"输出信息: {state}";
+
+            // 使用事件通知(这种方式更符合事件的使用习惯)
+            OnNotify?.Invoke(message);
+        }
+
+        // 设置状态并通知观察者
+        public void SetState(string newState)
+        {
+            state = newState;
+            NotifyObservers();
+        }
+
+        // 初始化事件(可选,可以在构造函数中做)
+        public Subject()
+        {
+            //注册通知观察者的委托事件
+            OnNotify += (message) => {
+                foreach (var observer in observers)
+                {
+                    observer.OnUpdate(message);
+                }
+            };
+        }
+    }
+
+    public class ConcreteObserverA : IObserver
+    {
+        public void OnUpdate(string message)
+        {
+            Console.WriteLine("观察者A输出: " + message);
+        }
+    }
+
+    public class ConcreteObserverB : IObserver
+    {
+        public void OnUpdate(string message)
+        {
+            Console.WriteLine("观察者B输出: " + message);
+        }
+    }
+}

+ 81 - 0
ConsoleApp1/guanchazhe_jieko.cs

@@ -0,0 +1,81 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ConsoleApp1
+{
+    internal class guanchazhe_jieko
+    {
+        // 主题接口
+        public interface ISubject
+        {
+            void RegisterObserver(IObserver observer);
+            void RemoveObserver(IObserver observer);
+            void NotifyObservers();
+            string GetState();
+            void SetState(string state);
+        }
+
+        // 观察者接口
+        public interface IObserver
+        {
+            void Update(string message);
+        }
+
+        // 主题类
+        public class ConcreteSubject : ISubject
+        {
+            private readonly List<IObserver> _observers = new List<IObserver>();
+            private string _subjectState;
+
+            public void RegisterObserver(IObserver observer)
+            {
+                _observers.Add(observer);
+            }
+
+            public void RemoveObserver(IObserver observer)
+            {
+                _observers.Remove(observer);
+            }
+
+            public void NotifyObservers()
+            {
+                foreach (var observer in _observers)
+                {
+                    observer.Update(_subjectState);
+                }
+            }
+
+            // 主题状态的设置和获取方法
+            public string GetState()
+            {
+                return _subjectState;
+            }
+
+            public void SetState(string state)
+            {
+                _subjectState = state;
+            }
+        }
+
+        // 观察者类
+        public class ConcreteObserver : IObserver
+        {
+            private string _name;
+            private string _observerState;
+
+            public ConcreteObserver(string name)
+            {
+                this._name = name;
+            }
+
+            public void Update(string message)
+            {
+                _observerState = message;
+                Console.WriteLine("Observer {0} received message: {1}", _name, _observerState);
+            }
+        }
+    }
+}

+ 22 - 0
观察者模式.sln

@@ -0,0 +1,22 @@
+
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.12.35521.163 d17.12
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{484AC5E7-C0B7-4125-BE9A-14A00D713EF2}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{484AC5E7-C0B7-4125-BE9A-14A00D713EF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{484AC5E7-C0B7-4125-BE9A-14A00D713EF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{484AC5E7-C0B7-4125-BE9A-14A00D713EF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{484AC5E7-C0B7-4125-BE9A-14A00D713EF2}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal