知識就是力量

靜態(tài)工廠方法模式

// 二者共同的接口
public interface Human {
    public void eat();
    public void sleep();
    public void beat();
}

// 創(chuàng)建實現(xiàn)類 Male
public class Male implements Human {
    public void eat() {
        System.out.println("Male can eat."); 
    }
    public void sleep() {
        System.out.println("Male can sleep.");
    }
    public void beat() {
        System.out.println("Male can beat.");
    }
} 

// 創(chuàng)建實現(xiàn)類 Female
public class Female implements Human {
    public void eat() {
        System.out.println("Female can eat."); 
    }
    public void sleep() {
        System.out.println("Female can sleep.");
    }
    public void beat() {
        System.out.println("Female can beat.");
    }
} 

// 多個工廠方法
public class HumanFactory {
    public static Male createMale() {
        return new Male();
    }
    public static Female createFemale() {
        return new Female();
    }
}

// 工廠測試類
public class FactoryTest {
    public static void main(String[] args) {
        Human male = HumanFactory.createMale();
        male.eat();
        male.sleep();
        male.beat();
    }
}

抽象工廠模式

// 抽象食物
interface Food {
    public String getFoodName();
}

// 抽象餐具
interface TableWare {
    public String getToolName();
}

// 抽象工廠
interface KitchenFactory {
    public Food getFood();
    public TableWare getTableWare();
}

// 具體食物 Apple 的定義如下
class Apple implements Food {
    public String getFoodName() {
        return "apple";
    }
}

// 具體餐具 Knife 的定義如下
class Knife implements TableWare { 
    public String getToolName() {
        return "knife";
    }
}

// 以具體工廠 AKitchen 為例
class AKitchen implements KitchenFactory {
    public Food getFood() {
       return new Apple();
    }

    public TableWare getTableWare() {
       return new Knife();
    }
}

// 吃貨要開吃了
public class Foodaholic {
    public void eat(KitchenFactory k) {
       System.out.println("A foodaholic is eating "+ k.getFood().getFoodName()
              + " with " + k.getTableWare().getToolName() );
    }

    public static void main(String[] args) {
       Foodaholic fh = new Foodaholic();
       KitchenFactory kf = new AKitchen();
       fh.eat(kf);
    }
}

適配器模式

// 國標(biāo)插頭
public interface CnPluginInterface {
    void chargeWith2Pins();
}

// 實現(xiàn)國標(biāo)插座的充電方法
public class CnPlugin implements CnPluginInterface {
    public void chargeWith2Pins() {
        System.out.println("charge with CnPlugin");
    }
}

// 在國家中內(nèi)充電
public class Home {
    private CnPluginInterface cnPlugin;

    public Home() { }

    public void setPlugin(CnPluginInterface cnPlugin) {
        this.cnPlugin = cnPlugin;
    }

    // 充電
    public void charge() {
        // 國標(biāo)充電
        cnPlugin.chargeWith2Pins();
    }
}

// 英標(biāo)插頭
public interface EnPluginInterface {
    void chargeWith3Pins();
}

// 實現(xiàn)英標(biāo)插座的充電方法
public class EnPlugin implements EnPluginInterface {
    public void chargeWith3Pins() {
        System.out.println("charge with EnPlugin");
    }
}

// 適配器
public class PluginAdapter implements CnPluginInterface {
    private EnPluginInterface enPlugin;

    public PluginAdapter(EnPluginInterface enPlugin) {
         this.enPlugin = enPlugin;
    }

    // 這是重點,適配器實現(xiàn)了英標(biāo)的插頭,然后重載國標(biāo)的充電方法為英標(biāo)的方法
    @Override
    public void chargeWith2Pins() {
        enPlugin.chargeWith3Pins();
    }
}

// 適配器測試類
public class AdapterTest {
    public static void main(String[] args) {
        EnPluginInterface enPlugin = new EnPlugin();
        Home home = new Home();
        PluginAdapter pluginAdapter = new PluginAdapter(enPlugin);
        home.setPlugin(pluginAdapter);
        // 會輸出 “charge with EnPlugin”
        home.charge();
    }
}

裝飾者模式

// 抽象類 Girl
public abstract class Girl {
    String description = "no particular";

    public String getDescription(){
        return description;
    }
}

// 美國女孩
public class AmericanGirl extends Girl {
    public AmericanGirl() {
        description = "+AmericanGirl";
    }
}

// 國產(chǎn)妹子
public class ChineseGirl extends Girl {
    public ChineseGirl() {
        description = "+ChineseGirl";
    }
}

// 裝飾者
public abstract class GirlDecorator extends Girl {
    public abstract String getDescription();
}

// 下面以美國女孩示例
// 給美國女孩加上金發(fā)
public class GoldenHair extends GirlDecorator {

    private Girl girl;

    public GoldenHair(Girl g) {
        girl = g;
    }

    @Override
    public String getDescription() {
        return girl.getDescription() + "+with golden hair";
    }

}

// 加上身材高大的特性
public class Tall extends GirlDecorator {

    private Girl girl;

    public Tall(Girl g) {
        girl = g;
    }

    @Override
    public String getDescription() {
        return girl.getDescription() + "+is very tall";
    }

}


// 檢驗一下
public class Test {

    public static void main(String[] args) {
        Girl g1 = new AmericanGirl();
        System.out.println(g1.getDescription());

        GoldenHair g2 = new GoldenHair(g1);
        System.out.println(g2.getDescription());

        Tall g3 = new Tall(g2);
        System.out.println(g3.getDescription());

        // 你也可以一步到位
        // Girl g = new Tall(new GoldenHair(new AmericanGirl())); 
    }
}

觀察者模式

// Subject 主題接口
public interface Subject {
    public void registerObserver(Observer o);
    public void removeObserver(Observer o);
    public void notifyAllObservers();
}

// 觀察者接口
public interface Observer {
    public void update(Subject s);
}

// 視頻網(wǎng)站某狐 實現(xiàn) Subject 接口
public class VideoSite implements Subject{

    // 觀察者列表 以及 更新了的視頻列表
    private ArrayList<Observer> userList;
    private ArrayList<String> videos;

    public VideoSite(){
        userList = new ArrayList<Observer>();
        videos = new ArrayList<String>();
    }

    @Override
    public void registerObserver(Observer o) {
        userList.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        userList.remove(o);
    }

    @Override
    public void notifyAllObservers() {
        for (Observer o: userList) {
            o.update(this);
        }
    }

    public void addVideos(String video) {
        this.videos.add(video);
        notifyAllObservers();
    }

    public ArrayList<String> getVideos() {
        return videos;
    }

    public String toString(){
        return videos.toString();
    }
}

// 實現(xiàn)觀察者,即看視頻的美劇迷們
public class VideoFans implements Observer {

    private String name;

    public VideoFans(String name){
        this.name = name;
    }
    @Override
    public void update(Subject s) {
        System.out.println(this.name + ", new videos are available! ");
        // print video list
        System.out.println(s);
    }
}

//  測試一下
public class Main {

    public static void main(String[] args) {
        VideoSite vs = new VideoSite();
        vs.registerObserver(new VideoFans("LiLei"));
        vs.registerObserver(new VideoFans("HanMeimei"));
        vs.registerObserver(new VideoFans("XiaoMing"));

        // add videos
        vs.addVideos("Video 1");
        //vs.addVideos("Video 2");
    }
}

單例模式

// 靜態(tài)內(nèi)部類
public class Wife {
    private static class WifeHolder {
        private static final Wife wife = new Wife();
    }

    private Wife() { }

    public static Wife getWife() {
        return WifeHolder.wife;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 設(shè)計模式匯總 一、基礎(chǔ)知識 1. 設(shè)計模式概述 定義:設(shè)計模式(Design Pattern)是一套被反復(fù)使用、多...
    MinoyJet閱讀 4,094評論 1 15
  • 一、設(shè)計模式的分類 總體來說設(shè)計模式分為三大類: 創(chuàng)建型模式,共五種:工廠方法模式、抽象工廠模式、單例模式、建造者...
    lichengjin閱讀 1,000評論 0 8
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,628評論 19 139
  • 文章部分內(nèi)容轉(zhuǎn)載自:http://blog.csdn.net/zhangerqing 一、設(shè)計模式的分類 總體來說...
    j_cong閱讀 2,142評論 0 20
  • 4-03 【子曰:唯仁者,能好人,能惡人?!?4-04 【子曰:茍志于仁矣,無惡也?!?孔子說真正有“仁”的修養(yǎng)的...
    空空的山谷閱讀 296評論 0 0

友情鏈接更多精彩內(nèi)容