一个追求传统编程的程序猿写出的代码会有什么样的问题?

115 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路

一、需求

妈妈在早餐后给三个孩子分发餐后水果 ,盘子里装有三个苹果: 红富士、青苹果、金帅,孩子们口味不同: 莉莉喜欢甜的,安迪喜欢酸的,露娜喜欢软的。

二、需求实现

 
public class Apple {
    private String title;
    private String color;
    private String origin;
 
    public Apple() {
    }
 
    public Apple(String title, String color, String origin) {
        this.title = title;
        this.color = color;
        this.origin = origin;
    }
 
    public String getTitle() {
        return title;
    }
 
    public void setTitle(String title) {
        this.title = title;
    }
 
    public String getColor() {
        return color;
    }
 
    public void setColor(String color) {
        this.color = color;
    }
 
    public String getOrigin() {
        return origin;
    }
 
    public void setOrigin(String origin) {
        this.origin = origin;
    }
}
package com.imooc.spring.ioc.entity;
 
public class Child {
    private String name;
    private Apple apple;
 
    public Child(){
 
    }
 
    public Child(String name, Apple apple) {
        this.name = name;
        this.apple = apple;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Apple getApple() {
        return apple;
    }
 
    public void setApple(Apple apple) {
        this.apple = apple;
    }
 
    public void eat(){
        System.out.println(name + "吃到了" + apple.getOrigin() + "种植的" + apple.getTitle());
    }
}
package com.imooc.spring.ioc;
 
import com.imooc.spring.ioc.entity.Apple;
import com.imooc.spring.ioc.entity.Child;
 
public class Application {
    public static void main(String[] args) {
        Apple apple1 = new Apple("红富士", "红色", "欧洲");
        Apple apple2 = new Apple("青苹果", "绿色", "中亚");
        Apple apple3 = new Apple("金帅", "黄色", "中国");
 
        Child lily = new Child("莉莉", apple3);
        Child andy = new Child("安迪", apple2);
        Child luna = new Child("露娜", apple1);
 
        lily.eat();
        andy.eat();
        luna.eat();
    }
}

三、代码分析

问题一: 文本的信息写死在代码中,如果属性发生变化,那么就要修改源代码,这是很不合理的。

问题二: 对象的数量都是固定写死的,如果需要新增对象,这会让可扩展性不足。

问题三: 对象是硬性关联,通过构造方法参数传入到对应参数中进行赋值,也就意味着,孩子和苹果的关系在编译时就已经确定了。

四、解决方法

利用IOC容器降低对象之间的直接耦合,将对象都交由Spring IOC创建和管理。

以下是代码实现:

<bean id="sourApple" class="com.imooc.spring.ioc.entity.Apple">
    <property name="title" value="青苹果"></property>
    <property name="origin" value="中亚"></property>
    <property name="color" value="绿色"></property>
</bean>

<bean id="softApple" class="com.imooc.spring.ioc.entity.Apple">
    <property name="title" value="金帅"></property>
    <property name="origin" value="中国"></property>
    <property name="color" value="黄色"></property>
</bean>

<bean id="lily" class="com.imooc.spring.ioc.entity.Child">
    <property name="name" value="莉莉"/>
    <property name="apple" ref="sweetApple"/>
</bean>

<bean id="andy" class="com.imooc.spring.ioc.entity.Child">
    <property name="name" value="安迪"/>
    <property name="apple" ref="sourApple"/>
</bean>

<bean id="luna" class="com.imooc.spring.ioc.entity.Child">
    <property name="name" value="露娜"/>
    <property name="apple" ref="softApple"/>
</bean>

</beans>
package com.imooc.spring.ioc;
 
import com.imooc.spring.ioc.entity.Apple;
import com.imooc.spring.ioc.entity.Child;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class SpringApplication {
    public static void main(String[] args) {
        //创建Spring IoC容器,并根据配置文件在容器中实例化对象
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Apple sweetApple = context.getBean("sweetApple" , Apple.class);
        System.out.println(sweetApple.getTitle());
        
        //从IoC容器中提取beanId=lily的对象
        Child lily = context.getBean("lily", Child.class);
        Child andy = context.getBean("andy", Child.class);
        Child luna = context.getBean("luna", Child.class);
 
![image.png](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/73e67d4a7ed34d40ba4e8da0f250381c~tplv-k3u1fbpfcp-watermark.image?)
        lily.eat();
        andy.eat();
        luna.eat();
    }
}

最后代码运行所得结果如下:

image.png