国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

《HeadFirst設(shè)計(jì)模式(第二版)》第十一章代碼——代理模式

這篇具有很好參考價(jià)值的文章主要介紹了《HeadFirst設(shè)計(jì)模式(第二版)》第十一章代碼——代理模式。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

代碼文件目錄:

《HeadFirst設(shè)計(jì)模式(第二版)》第十一章代碼——代理模式,HeadFirst設(shè)計(jì)模式(第二版)源碼,設(shè)計(jì)模式,代理模式

?RMI:
MyRemote
package Chapter11_ProxyPattern.RMI;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface MyRemote extends Remote {
    public String sayHello() throws RemoteException;
}
MyRemoteClient
package Chapter11_ProxyPattern.RMI;

import java.rmi.Naming;

public class MyRemoteClient {
    public static void main(String[] args) {
        new MyRemoteClient().go();
    }

    public void go(){
        try{
            MyRemote service = (MyRemote) Naming.lookup("rmi://127.0.0.1/RemoteHello");
            String s = service.sayHello();
            System.out.println(s);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
MyRemoteImpl
package Chapter11_ProxyPattern.RMI;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {
    private static final long serialVersion = 1L;

    public String sayHello() throws RemoteException {
        return "Server says: Hey!";
    }

    public MyRemoteImpl()throws RemoteException{}

    public static void main(String[] args) {
        try{
            MyRemote service = new MyRemoteImpl();
            Naming.rebind("RemoteHello",service);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
能夠遠(yuǎn)程監(jiān)控的糖果機(jī):

在上一章的代碼的基礎(chǔ)上做一些修改

GumballMachine
public class GumballMachine
        extends UnicastRemoteObject implements GumballMachineRemote
    {
    private static final long serialVersionUID = 2L;
    //加上地理位置支持
    String location;
    State soldOutState;
    State noQuarterState;
    State hasQuarterState;
    State soldState;
    State winnerState;

    State state = soldOutState;
    int count = 0;

    public GumballMachine(String location,int numberGumballs) throws RemoteException {
        soldOutState = new SoldOutState(this);
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldState = new SoldState(this);
        winnerState = new WinnerState(this);

        this.location = location;
        this.count = numberGumballs;
        if (numberGumballs > 0) {
            state = noQuarterState;
        }
    }
GumballMachineRemote
package Chapter11_ProxyPattern.Origin;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface GumballMachineRemote extends Remote {
    public int getCount() throws RemoteException;
    public String getLocation() throws RemoteException;
    public State getState() throws RemoteException;
}
GumballMachineTestDrive
package Chapter11_ProxyPattern.Origin;

import java.rmi.Naming;

/**
 * @Author 竹心
 * @Date 2023/8/19
 **/

public class GumballMachineTestDrive {
    public static void main(String[] args) {
        GumballMachineRemote gumballMachine = null;
        int count;

        if (args.length < 2) {
            System.out.println("GumballMachine <name> <inventory>");
            System.exit(1);
        }

        try {
            count = Integer.parseInt(args[1]);

            gumballMachine =
                    new GumballMachine(args[0], count);
            Naming.rebind("http://" + args[0] + "/gumballmachine", gumballMachine);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
GumballMonitor
package Chapter11_ProxyPattern.Origin;

import java.rmi.RemoteException;

/**
 * @Author 竹心
 * @Date 2023/8/20
 **/

//糖果監(jiān)視器
public class GumballMonitor {
    GumballMachineRemote gumballMachine;

    public GumballMonitor(GumballMachineRemote machine){
        this.gumballMachine = machine;
    }

    public void report(){
        try{
            System.out.println("Gumball Machine: "+this.gumballMachine.getLocation());
            System.out.println("Current inventory: "+this.gumballMachine.getCount()+" gumballs");
            System.out.println("Current State: "+this.gumballMachine.getState());
        }catch (RemoteException e){
            e.printStackTrace();
        }

    }
}
GumballMonitorTestDrive
package Chapter11_ProxyPattern.Origin;

import java.rmi.Naming;

public class GumballMonitorTestDrive {
    public static void main(String[] args) {
        String[] location = {"rmi://127.0.0.1/gumballmachine",
                "rmi://127.0.0.1/gumballmachine",
                "rmi://127.0.0.1/gumballmachine"};

        if (args.length >= 0)
        {
            location = new String[1];
            location[0] = "rmi://" + args[0] + "/gumballmachine";
        }

        GumballMonitor[] monitor = new GumballMonitor[location.length];


        for (int i=0;i < location.length; i++) {
            try {
                GumballMachineRemote machine =
                        (GumballMachineRemote) Naming.lookup(location[i]);
                monitor[i] = new GumballMonitor(machine);
                System.out.println(monitor[i]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        for (int i=0; i < monitor.length; i++) {
            monitor[i].report();
        }
    }
}
五個(gè)狀態(tài)類(lèi):

同樣的修改:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-661399.html

public class HasQuarterState implements State {
    private static final long serialVersionUID = 2L;
    Random randomWinner = new Random(System.currentTimeMillis());

到了這里,關(guān)于《HeadFirst設(shè)計(jì)模式(第二版)》第十一章代碼——代理模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 《HeadFirst設(shè)計(jì)模式(第二版)》第九章代碼——迭代器模式

    ? ? ? ? 一家早餐店和一家午餐點(diǎn)準(zhǔn)備合并在一起,兩家的點(diǎn)菜的菜單實(shí)現(xiàn)方式如下: ? ? ? ? 首先,他們的菜單選項(xiàng)都基于同一個(gè)類(lèi): 菜單選項(xiàng)類(lèi) 早餐店初始菜單 午餐店初始菜單: 可以得知:前者使用List來(lái)實(shí)現(xiàn),后者使用數(shù)組來(lái)實(shí)現(xiàn)。 這時(shí)候,如果不采取任何方法加以

    2024年02月12日
    瀏覽(20)
  • 《HeadFirst設(shè)計(jì)模式(第二版)》第五章代碼——單例模式

    《HeadFirst設(shè)計(jì)模式(第二版)》第五章代碼——單例模式

    代碼文件目錄: ?初始版本: 三種解決多線程問(wèn)題的方法: Notes:

    2024年02月13日
    瀏覽(24)
  • 《HeadFirst設(shè)計(jì)模式(第二版)》第八章代碼——模板方法模式

    《HeadFirst設(shè)計(jì)模式(第二版)》第八章代碼——模板方法模式

    代碼文件目錄: ? CaffeineBeverage Coffee Tea notes

    2024年02月12日
    瀏覽(25)
  • 【設(shè)計(jì)模式】第十一章:享元模式詳解及應(yīng)用案例

    【設(shè)計(jì)模式】第十一章:享元模式詳解及應(yīng)用案例

    【設(shè)計(jì)模式】七大設(shè)計(jì)原則 【設(shè)計(jì)模式】第一章:?jiǎn)卫J?【設(shè)計(jì)模式】第二章:工廠模式 【設(shè)計(jì)模式】第三章:建造者模式 【設(shè)計(jì)模式】第四章:原型模式 【設(shè)計(jì)模式】第五章:適配器模式 【設(shè)計(jì)模式】第六章:裝飾器模式 【設(shè)計(jì)模式】第七章:代理模式 【設(shè)計(jì)模式

    2024年02月13日
    瀏覽(23)
  • 設(shè)計(jì)模式詳解(十一)——組合模式

    組合模式定義 組合模式(Composite Pattern)是一種結(jié)構(gòu)型設(shè)計(jì)模式,又叫部分整體模式,它將對(duì)象組合成樹(shù)形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu)。組合模式使得用戶(hù)對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有一致性。組合模式依據(jù)樹(shù)形結(jié)構(gòu)來(lái)組合對(duì)象,用來(lái)表示部分以及整體層次。

    2024年02月22日
    瀏覽(23)
  • 設(shè)計(jì)模式淺析(十一) ·狀態(tài)模式

    設(shè)計(jì)模式淺析(十一) ·狀態(tài)模式

    日常叨逼叨 java設(shè)計(jì)模式淺析,如果覺(jué)得對(duì)你有幫助,記得一鍵三連,謝謝各位觀眾老爺???? 狀態(tài)模式 概念 狀態(tài)模式 Java中的狀態(tài)模式(State Pattern)是一種行為型設(shè)計(jì)模式,它允許一個(gè)對(duì)象在其內(nèi)部狀態(tài)改變時(shí)改變它的行為,看起來(lái)就像修改了它的類(lèi)一樣。狀態(tài)模式的核心

    2024年04月12日
    瀏覽(19)
  • 軟件設(shè)計(jì)模式系列之十一——裝飾模式

    軟件設(shè)計(jì)模式系列之十一——裝飾模式

    當(dāng)談到設(shè)計(jì)軟件系統(tǒng)時(shí),經(jīng)常需要考慮如何使系統(tǒng)更加靈活、可擴(kuò)展和易維護(hù)。設(shè)計(jì)模式是一種被廣泛采用的方法,用于解決常見(jiàn)的設(shè)計(jì)問(wèn)題,并提供了一套可重用的解決方案。裝飾模式(Decorator Pattern)是一種結(jié)構(gòu)型設(shè)計(jì)模式,它允許您在不改變對(duì)象接口的情況下動(dòng)態(tài)地添

    2024年02月08日
    瀏覽(20)
  • 設(shè)計(jì)模式(十一)享元

    設(shè)計(jì)模式(十一)享元

    運(yùn)用共享技術(shù)有效地支持大量細(xì)粒度對(duì)象的復(fù)用,享元模式是一種結(jié)構(gòu)型模式。 享元模式要求能夠共享的對(duì)象必須是細(xì)粒度對(duì)象,因此它又稱(chēng)為輕量級(jí)模式。享元模式的結(jié)構(gòu)較為復(fù)雜,一般結(jié)合工廠模式一起使用,在其結(jié)構(gòu)圖中包含了一個(gè)享元工廠類(lèi),包含以下四個(gè)角色:

    2024年02月05日
    瀏覽(23)
  • 設(shè)計(jì)模式(二十一)策略

    設(shè)計(jì)模式(二十一)策略

    定義一系列算法類(lèi),將每一個(gè)算法封裝起來(lái),并讓它們可以相互替換。策略模式讓算法獨(dú)立于使用它的客戶(hù)而變化。策略模式是一種對(duì)象行為型模式,又稱(chēng)為政策(Policy)模式。 包含以下三個(gè)角色: 1、Context(環(huán)境類(lèi)): 環(huán)境類(lèi)是使用算法的角色,它在解決某個(gè)問(wèn)題(即實(shí)

    2024年02月01日
    瀏覽(19)
  • 設(shè)計(jì)模式十一:外觀模式(Facade Pattern)

    外觀模式(Facade Pattern)是一種結(jié)構(gòu)型設(shè)計(jì)模式,它提供了一個(gè)統(tǒng)一的接口,用于訪問(wèn)系統(tǒng)中的一組復(fù)雜子系統(tǒng)。外觀模式通過(guò)將復(fù)雜子系統(tǒng)的接口封裝在一個(gè)高層接口中,簡(jiǎn)化了客戶(hù)端與子系統(tǒng)之間的交互,使得客戶(hù)端代碼更加清晰、簡(jiǎn)潔。 外觀模式的使用場(chǎng)景包括: 簡(jiǎn)化

    2024年02月13日
    瀏覽(29)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包