常见new的创建方法:
   Road  road=new Road();
这样带来的问题是:实现依赖,不能应对"具体的实例化类型"的变化.
解决思路:封装变化点,那里变化了,就封装那里,没有变化就不需要封装.
变化点在"对象创建",因此就封装对象创建.
要面向接口编程,依赖于接口,并非依赖于实现.最简单的解决方法:
静态工厂模式
class RoadFactory
{
     public static Road CreateRoad()
    {    
          return new Road();//可以在此可以根据需求修改  return new waterRoad();
    }



创建一个Road对象,Road  road=RoadFactory.CreateRoad();//客户程序, 这里相对稳定.

相关文章:

  • 2021-10-23
  • 2021-07-13
  • 2022-12-23
  • 2021-08-22
  • 2021-06-10
  • 2021-09-04
  • 2022-01-26
  • 2021-06-01
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-03
  • 2022-12-23
  • 2022-02-13
  • 2021-12-24
  • 2021-12-26
相关资源
相似解决方案