依賴和耦合

我剛開始學習軟件工程,學習設計模式,老是會聊到設計模式,老是會說高內聚,低耦合,還有依賴注入。什麼是依賴,什麼是耦合?如今來聊一聊我對於依賴和耦合的理解。java

先看下面一段簡單代碼,編程

class Apple{
    private String name = "Apple";
}

class People{
    private List<Apple> stomach = new ArrayList<>();
    
    public void eat(Apple apple){
        stomach.add(apple);
    }

}

class MainDemo {
    public static void main(String[] args) {    
        People xiaoming= new People();
        Apple apple = new Apple();
        xiaoming.eat(apple);
    }
}

一我的類,一個蘋果類,代碼的意思是小明吃蘋果,保存在胃中。
如今又有一個需求:小明想要去吃香蕉,想要去吃梨子。用上面這一段的代碼就很差改了。要完成需求就須要作大量的變化。再加一個 Banana stomach?
這個需求是一個合理的需求,可是由於咱們這一段的代碼是不合理的,產生了依賴和耦合。設計模式

如何減小依賴,就是使用面向接口的編程。依賴一個抽象而不是依賴一個實現。app

interface Food{
    
}

class Banana implement Food{
    private String name = "Banana";
}

class Apple implement Food{
    private String name = "Apple";
}

class People{
    private List<Apple> stomach = new ArrayList<>();
    
    public void eat(Food food){
        stomach.add(food);
    }
}

class MainDemo {
    public static void main(String[] args) {
        People xiaoming= new People();
        Food apple = new Apple();
        xiaoming.eat(apple);
        Food banana = new Banana();
        xiaoming.eat(banana);
    }
}

這樣其實個人接口裏面啥也沒有,可是卻將依賴轉移到了抽象的接口。這樣我每次添加均可以增長不一樣水果。其實我這個取名是Food,那麼這個其實能夠添加其餘的種類食物。這也體現了開放-閉合原則。再看最後一個例子學習

import java.util.*;


interface Food{
    public String getName();    
}

abstract class Fruit implements Food {
    protected String type = "fruit";
    
    public abstract void fruitProperty();
}

class Banana extends Fruit {
    private String name = "Banana";
    
    public String getName(){
        return name;
    }
    
    public void fruitProperty(){}
}

class Apple extends Fruit {
    private String name = "Apple";
    
    public String getName(){
        return name;
    }
 
    public void fruitProperty(){}
    
}

abstract class Meat implements Food {
    protected String type = "meat";
            
    public abstract void meatProperty();
}

class Beef extends Meat {
    private String name = "Beef";
    
    public String getName(){
        return name;
    }
    
    public void meatProperty(){}
}

class Chicken extends Meat {
    private String name = "Chicken";
    
    public String getName(){
        return name;
    };
    
    public void meatProperty(){}
}

class People{
    private List<Food> stomach = new ArrayList<>();
    
    public void eat(Food food){
        stomach.add(food);
    }
    
    public void display(){
        for (Food f : stomach) {
            System.out.println("stomach have " + f.getName());
        }
    }
}


class MainDemo {
    public static void main(String[] args) 
    {
        People xiaoming= new People();
        Food apple = new Apple();
        xiaoming.eat(apple);
        Food banana = new Banana();
        xiaoming.eat(banana);
        Food beef = new Beef ();
        xiaoming.eat(beef);
        Food chicken = new Chicken ();
        xiaoming.eat(chicken);
        xiaoming.display();
    }
}

能夠添加任何食物,西瓜,白菜,西蘭花。而且他們之間的方法能夠是共有的,也能夠私有的。當出現幾種大類相同的事務,添加一個蔬菜抽象類,這樣簡化代碼。ui

相關文章
相關標籤/搜索