日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

享元模式实例与解析实例二:共享网络设备(有外部状态)

發布時間:2024/3/13 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 享元模式实例与解析实例二:共享网络设备(有外部状态) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實例二:共享網絡設備(有外部狀態) 雖然網絡設備可以共享,但是分配給每一個終端計算機的端口(Port)是不同的,因此多臺計算機雖然可以共享同一個網絡設備,但必須使用不同的端口。我們可以將端口從網絡設備中抽取出來作為外部狀態,需要時再進行設置。

?

?

?

?

public class Switch implements NetworkDevice {private String type;public Switch(String type){this.type=type;}public String getType(){return this.type;} public void use(Port port){System.out.println("Linked by switch, type is " + this.type + ", port is " + port.getPort());} }

?

?

public class Port {private String port;public Port(String port){this.port=port; }public void setPort(String port){this.port=port;}public String getPort(){return this.port;} }

?

?

public interface NetworkDevice {public String getType();public void use(Port port); } public class Hub implements NetworkDevice {private String type;public Hub(String type){this.type=type;}public String getType(){return this.type;} public void use(Port port){System.out.println("Linked by Hub, type is " + this.type + ", port is " + port.getPort());} }

?

import java.util.*;public class DeviceFactory {private ArrayList devices = new ArrayList();private int totalTerminal=0;public DeviceFactory(){NetworkDevice nd1=new Switch("Cisco-WS-C2950-24");devices.add(nd1);NetworkDevice nd2=new Hub("TP-LINK-HF8M");devices.add(nd2);}public NetworkDevice getNetworkDevice(String type){if(type.equalsIgnoreCase("cisco")){totalTerminal++;return (NetworkDevice)devices.get(0);}else if(type.equalsIgnoreCase("tp")){totalTerminal++;return (NetworkDevice)devices.get(1);}else{return null;}}public int getTotalDevice(){return devices.size();}public int getTotalTerminal(){return totalTerminal;} }

?

public class Client {public static void main(String args[]){NetworkDevice nd1,nd2,nd3,nd4,nd5;DeviceFactory df=new DeviceFactory();nd1=df.getNetworkDevice("cisco");nd1.use(new Port("1000"));nd2=df.getNetworkDevice("cisco");nd2.use(new Port("1001"));nd3=df.getNetworkDevice("cisco");nd3.use(new Port("1002"));nd4=df.getNetworkDevice("tp");nd4.use(new Port("1003"));nd5=df.getNetworkDevice("tp");nd5.use(new Port("1004"));System.out.println("Total Device:" + df.getTotalDevice());System.out.println("Total Terminal:" + df.getTotalTerminal());} }

?

?

總結

以上是生活随笔為你收集整理的享元模式实例与解析实例二:共享网络设备(有外部状态)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。