How can I design this better? (Avoiding a switch statement with Object Oriented design)

前端 未结 6 842
不思量自难忘°
不思量自难忘° 2020-12-30 11:23

I know a little bit about Object Oriented design, but I\'m not sure how to go about using those principles in my code. Here\'s what I\'m working on:

    pub         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 12:03

    I would refactor to take advantage of an interface. I would probably make it something like:

    public interface IQuery
    {
        void Execute(Agency agency, Citation query);
    }
    
    public class OracleQuery : IQuery
    {
        // Implementation
    }
    
    public class PickQuery : IQuery
    {
        // Implementation
    }
    

    You could then change the Agency class to store an instance of an IQuery object rather than (or in addition to) the ClientDb object:

    public class Agency
    {
        public IQuery Query { get; set; }
    }
    

    And then in your initialization code (where you would normally set the ClientDb property), you could set the instance to the appropriate IQuery implementation:

    agency.Query = new PickQuery();
    

提交回复
热议问题