How to use generic class with specific objects in a static context?

前端 未结 2 948
难免孤独
难免孤独 2020-12-25 08:30

I\'m gonna try to explain at my best.

I use Play Framework 2, and I will do a lot of CRUD actions. Some of them will be identitcal, so I\'d like to KISS and DRY so a

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-25 09:14

    (Disclaimer: I have no experience with playframework.)

    The following idea might help:

    public interface IOpImplementation {
        public static Result list();
        public static Result details(Long id);
        public static Result create();
        public static Result update(Long id);
        public static Result delete(Long id);
    }
    
    public abstract class CrudController extends Controller {
        protected static Model.Finder finder = null;
        protected static Form form = null;
    
        protected static IOpImplementation impl;
    
        public static Result list() {
            return impl.list();
        }
    
        public static Result details(Long id) {
            return impl.details(id);
        }
        // other operations defined the same way
    }
    
    public class Cities extends CrudController {
    
        public static Cities() {
            impl = new CitiesImpl();
        }
    
    }
    

    This way you can create a hierarchy of implementations.

    (This must be some fancy-named design pattern, but I don't know the name ATM.)

提交回复
热议问题