V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
dumbbell5kg
V2EX  ›  程序员

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

  •  
  •   dumbbell5kg · Oct 17, 2022 · 2079 views
    This topic created in 1294 days ago, the information mentioned may be changed or developed.
    • 之前看 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 replies    2022-10-17 13:44:32 +08:00
    Leviathann
        1
    Leviathann  
       Oct 17, 2022
    delegate
    kosmosr
        2
    kosmosr  
       Oct 17, 2022
    就是通过接口吧?
    justplaymore
        3
    justplaymore  
       Oct 17, 2022
    proxy pattern: https://refactoring.guru/design-patterns/proxy
    接口的命名一般是用来描述抽象的能力的,具体看你希望给这个接口赋予什么能力。
    wolfie
        4
    wolfie  
       Oct 17, 2022
    这就是普通多态,没用到设计模式。
    lllllliiii
        5
    lllllliiii  
       Oct 17, 2022
    包装 代理 组合?
    dumbbell5kg
        6
    dumbbell5kg  
    OP
       Oct 17, 2022
    @justplaymore 感谢,这正是我想知道的
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2465 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 05:54 · PVG 13:54 · LAX 22:54 · JFK 01:54
    ♥ Do have faith in what you're doing.