V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
dumbbell5kg
V2EX  ›  程序员

请教一个 Java 设计模式的问题

  •  
  •   dumbbell5kg · 2022 年 10 月 17 日 · 1993 次点击
    这是一个创建于 1205 天前的主题,其中的信息可能已经有所发展或是发生改变。
    • 之前看 spring 代码的时候发现了这样的写法:
    GenericApplicationContext implements BeanFactory{
        BeanFactoryImp beanFactoryImp;
        
        @Override
    	public Object getBean(String name) throws BeansException {
    		return getBeanFactoryImp().getBean(name);
    	}
    }
    
    • 我觉得这样写的好处是通过 GenericApplicationContext 来使用 BeanFactory 提供的 getBean()等方法时,不需要写成这样 new GenericApplicationContext().getBeanFactory().getBean(name),而直接是 new GenericApplicationContext().getBean(name)。

    • 我现在也有个场景想要使用这种技巧,但是我不知道接口名字,也就是上面代码的 BeanFactory 该怎么命名,所以这种技巧是一个什么模式吗,我想用这种模式的名字来命名我的接口类,比如 BeanFactoryFacade ?

    6 条回复    2022-10-17 13:44:32 +08:00
    Leviathann
        1
    Leviathann  
       2022 年 10 月 17 日
    delegate
    kosmosr
        2
    kosmosr  
       2022 年 10 月 17 日
    就是通过接口吧?
    justplaymore
        3
    justplaymore  
       2022 年 10 月 17 日
    proxy pattern: https://refactoring.guru/design-patterns/proxy
    接口的命名一般是用来描述抽象的能力的,具体看你希望给这个接口赋予什么能力。
    wolfie
        4
    wolfie  
       2022 年 10 月 17 日
    这就是普通多态,没用到设计模式。
    lllllliiii
        5
    lllllliiii  
       2022 年 10 月 17 日
    包装 代理 组合?
    dumbbell5kg
        6
    dumbbell5kg  
    OP
       2022 年 10 月 17 日
    @justplaymore 感谢,这正是我想知道的
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   1497 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 16:50 · PVG 00:50 · LAX 08:50 · JFK 11:50
    ♥ Do have faith in what you're doing.