zoukankan      html  css  js  c++  java
  • c# 初识Actor Model

    最近学了点 c# dataflow的一些东西,然后国外有个人,用dataflow来实现了,一个Actor模型;

    这里做个比较,算是初识我们的actor模型,然后我们再进一步的深入了解一哈;

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading.Tasks.Dataflow;
    
    namespace Actor_Model
    {
        //actor model 的基本使用和示例;
        //https://blog.jayway.com/2013/11/15/an-actor-model-implementation-in-c-using-tpl-dataflow/#comment-463337
    
        public abstract class Message { }
    
        //把所有的action 都通过meg的 形式表达出来;
    
        //吧我们的每一动作都看成了信息;让后将信息传递出去,真正的执行,交给我们的具体的actor去执行滴呀;
        public class Deposit : Message
        {
    
            public decimal Amount { get; set; }
        }
        //To check the balance we send a QueryBalance message
        public class QueryBalance : Message
        {
            //The result of the query should be sent as a message to another actor
            //也就是说查询出来的信息又交给了另外一个actor 处理了滴呀;信息之间的传递;
            public Actor Receiver { get; set; }
        }
    
        //The final message is the result of the balance chec
        public class Balance : Message
        {
            public decimal Amount { get; set; }
        }
    
        public abstract class Actor
        {
    
            private readonly ActionBlock<Message> _action;
    
            public Actor()
            {
                _action = new ActionBlock<Message>((msg =>
                  {
                      dynamic self = this;
                      dynamic mess = msg;
                      self.Handle(mess);
    
                  }));
    
            }
    
            public void Send(Message msg)
            {
                _action.Post(msg);
            }
    
            public Task Completion
            {
                get
                {
                    _action.Complete();
                    return _action.Completion;
                }
            }
    
        }
    
    
        //Each account has a balance. We want to be able to deposit money and check the balance
        //实际动作的执行者;
        public class AccountActor : Actor
        {
            private decimal _balance;
    
            //存钱
            public void Handle(Deposit msg)
            {
                _balance += msg.Amount; //deposite money 
            }
            //查询余额
            public void Handle(QueryBalance msg)
            {
                //查询出来的余额,叫给我们的额新actor去处理;
                //相当于创建一个新的acto
                msg.Receiver.Send(new Balance { Amount = _balance });
            }
    
    
    
        }
        //We also need an actor that outputs the result of the balance query
        public class OutputActor : Actor
        {
            public void Handle(Balance msg)
            {
                Console.WriteLine("Balance is {0}", msg.Amount);
            }
    
        }
    
        class Program
        {
            static void Main(string[] args)
            {
    
                var account = new AccountActor();
                var output = new OutputActor();
    
                account.Send(new Deposit { Amount = 50 });
                account.Send(new QueryBalance { Receiver = output });
    
                account.Completion.Wait();
                output.Completion.Wait();
    
                Console.WriteLine("Done!");
                Console.ReadLine();
    
    
            }
        }
    }

    看完基本了解actor,但是还是很不透彻~

  • 相关阅读:
    day4递归原理及实现
    day4装饰器
    day4迭代器&生成器&正则表达式
    open()函数文件操作
    Python中的内置函数
    function(函数)中的动态参数
    copy深浅拷贝
    collections模块
    set集合
    字典dict常用方法
  • 原文地址:https://www.cnblogs.com/mc67/p/7610748.html
Copyright © 2011-2022 走看看