How to ensure that builder pattern is completed?

后端 未结 7 1759
鱼传尺愫
鱼传尺愫 2021-02-05 13:38

EDIT: I am not worried about being called in the wrong order since this is enforced through using multiple interfaces, I am just worried about the terminal method getting called

7条回答
  •  耶瑟儿~
    2021-02-05 14:18

    Apart from using Diezel to generate the whole set of interfaces, is to force them getting the "token" object:

    
        Grant.permissionTo( permissionManager.User( userId ).permissionTo( Right.READ ).item( docId ).asOf( new Date() ) );
    
    

    users won't be able to finish the statement until the last/exit method returns the right type. The Grant.permissionTo can be a static method, statically imported, a simple constructor. It will get all it needs to actually register the permission into the permissionManager, so it doesn't need to be configured, or obtained via configuration.

    Folks at Guice uses another pattern. They define a "callable", that is used to configure permission (in Guice it's all about binding instead).

    
        public class MyPermissions extends Permission{
    
        public void configure(){
        grantUser( userId ).permissionTo( Right.READ ).item( docId ).asOf( new Date() );
        }
    
        }
    
        permissionManager.add(new MyPermissions() );
    
    

    grantUser is a protected method. permissionManager can ensure that MyPermissions only contains fully qualified permissions.

    For a single permission this is worst than the first solution, but for a bunch of permission it's cleaner.

提交回复
热议问题