Reliable Actors 框架中的多态性技术Polymorphism in the Reliable Actors framework

Reliable Actors 框架允许使用许多在面向对象的设计中使用的相同技术来生成执行组件。The Reliable Actors framework allows you to build actors using many of the same techniques that you would use in object-oriented design. 其中一种技术是多态性技术,它允许类型和接口从多个通用父类中继承。One of those techniques is polymorphism, which allows types and interfaces to inherit from more generalized parents. Reliable Actors 框架中的继承通常遵循 .NET 模型,并会受到一些附加限制。Inheritance in the Reliable Actors framework generally follows the .NET model with a few additional constraints. 在 Java/Linux 中,它遵循 Java 模型。In case of Java/Linux, it follows the Java model.

界面Interfaces

Reliable Actors 框架要求至少定义一个要由执行组件类型实现的接口。The Reliable Actors framework requires you to define at least one interface to be implemented by your actor type. 此接口用于生成代理类,客户端可以使用此代理类与执行组件进行通信。This interface is used to generate a proxy class that can be used by clients to communicate with your actors. 只要由执行组件类型实现的每个接口及其所有父接口最终派生自 IActor(C#) 或 Actor(Java),就可以从其他接口继承接口。Interfaces can inherit from other interfaces as long as every interface that is implemented by an actor type and all of its parents ultimately derive from IActor(C#) or Actor(Java) . IActor(C#) 和 Actor(Java) 是平台定义的基接口,分别用于 .NET 和 Java 框架中的执行组件。IActor(C#) and Actor(Java) are the platform-defined base interfaces for actors in the frameworks .NET and Java respectively. 因此,使用形状的经典多态性示例可能如下所示:Thus, the classic polymorphism example using shapes might look something like this:

形状执行组件的接口层次结构

类型Types

还可以创建执行组件类型的层次结构,这些类型派生自由平台提供的执行组件基类。You can also create a hierarchy of actor types, which are derived from the base Actor class that is provided by the platform. 如果是形状,则可能具有一个 Shape(C#) 或 ShapeImpl(Java) 基类型:In the case of shapes, you might have a base Shape(C#) or ShapeImpl(Java) type:

public abstract class Shape : Actor, IShape
{
    public abstract Task<int> GetVerticeCount();

    public abstract Task<double> GetAreaAsync();
}
public abstract class ShapeImpl extends FabricActor implements Shape
{
    public abstract CompletableFuture<int> getVerticeCount();

    public abstract CompletableFuture<double> getAreaAsync();
}

Shape(C#) 或 ShapeImpl(Java) 的子类型可以重写基类型的方法。Subtypes of Shape(C#) or ShapeImpl(Java) can override methods from the base.

[ActorService(Name = "Circle")]
[StatePersistence(StatePersistence.Persisted)]
public class Circle : Shape, ICircle
{
    public override Task<int> GetVerticeCount()
    {
        return Task.FromResult(0);
    }

    public override async Task<double> GetAreaAsync()
    {
        CircleState state = await this.StateManager.GetStateAsync<CircleState>("circle");

        return Math.PI *
            state.Radius *
            state.Radius;
    }
}
@ActorServiceAttribute(name = "Circle")
@StatePersistenceAttribute(statePersistence = StatePersistence.Persisted)
public class Circle extends ShapeImpl implements Circle
{
    @Override
    public CompletableFuture<Integer> getVerticeCount()
    {
        return CompletableFuture.completedFuture(0);
    }

    @Override
    public CompletableFuture<Double> getAreaAsync()
    {
        return (this.stateManager().getStateAsync<CircleState>("circle").thenApply(state->{
          return Math.PI * state.radius * state.radius;
        }));
    }
}

请注意执行组件类型上的 ActorService 属性。Note the ActorService attribute on the actor type. 此属性告知 Reliable Actor 框架,它应自动创建用于托管此类型的执行组件的服务。This attribute tells the Reliable Actor framework that it should automatically create a service for hosting actors of this type. 在某些情况下,建议创建仅用于与子类型共享功能,并且始终不会用于实例化具体的执行组件的基类型。In some cases, you may wish to create a base type that is solely intended for sharing functionality with subtypes and will never be used to instantiate concrete actors. 在这些情况下,应使用 abstract 关键字表示始终不会基于此类型创建执行组件。In those cases, you should use the abstract keyword to indicate that you will never create an actor based on that type.

后续步骤Next steps