Version02:
敏小言说,你这输入输出流啥的,我看不懂呀,我不管,你就给我提供一个方法就行了(相当于帮敏小言屏蔽一些网络的细节)
小胡说,那行,你先别急,我封装封装:
package entity;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
//@Builder
//@Data
//@NoArgsConstructor
//@AllArgsConstructor
public class Pilliow implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String clothesColor;
public Pilliow(Integer id, String clothesColor) {
super();
this.id = id;
this.clothesColor = clothesColor;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getClothesColor() {
return clothesColor;
}
public void setClothesColor(String clothesColor) {
this.clothesColor = clothesColor;
}
@Override
public String toString() {
return "Pilliow [id=" + id + ", clothesColor=" + clothesColor + "]";
}
}
然后,小胡把id给敏小言发过去时,自己就开始阻塞等待:
什么时候把Pillow发送来呢?
什么时候把Pillow发送来呢?什么时候把Pillow发送来呢?
什么时候把Pillow发送来呢?什么时候把Pillow发送来呢?什么时候把Pillow发送来呢?
…
然后敏小言那边作为服务端,调用服务(方法)用id查找到那个Pillow后,就开始把这个Pillow给小胡发过去。
然后小胡收到了Pillow后,就去把Pillow塞到柜子里等顾客去取就行了。
其中有些偷懒细节,看注释:
package client;
import entity.Pilliow;
import server.Stub;
public class Client {
public static void main(String[] args) throws Exception {
Stub stub = new Stub();
System.out.println(stub.getPilliowByPilliowId(221));
}
}
package server;
import entity.Pilliow;
public interface PilliowService {
Pilliow getPilliowByPilliowId(Integer id);
}
package server;
import entity.Pilliow;
public class PilliowServiceImpl implements PilliowService{
@Override
public Pilliow getPilliowByPilliowId(Integer id) {
System.out.println("客户端敏小言查询了" + "id为:" + id + "状态下的小胡");
return new Pilliow(id, "black 卫衣");
}
}
package server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import entity.Pilliow;
public class Server {
private static boolean flag = true;
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(2221);
while(flag){
Socket socket = serverSocket.accept();
process(socket);
socket.close();
}
}
private static void process(Socket socket) throws Exception {
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
int id = dataInputStream.readInt();
PilliowServiceImpl serviceImpl = new PilliowServiceImpl();
Pilliow pilliow = serviceImpl.getPilliowByPilliowId(id);
dataOutputStream.writeInt(pilliow.getId());
dataOutputStream.writeUTF(pilliow.getClothesColor());
dataOutputStream.flush();
}
}
package server;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import entity.Pilliow;
public class Stub {
public Pilliow getPilliowByPilliowId(Integer id) throws Exception{
Socket socket = new Socket("127.0.0.1", 2221);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
dataOutputStream.writeInt(221);
socket.getOutputStream().write(byteArrayOutputStream.toByteArray());
socket.getOutputStream().flush();
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
int receivedId = dataInputStream.readInt();
String clothesColor = dataInputStream.readUTF();
Pilliow pillow = new Pilliow(receivedId, clothesColor);
dataOutputStream.close();
socket.close();
return pillow;
}
}
但是此时缺陷依旧:
- 现在这个代理Stub只能代理这一个方法,也就是通过id找抱枕,那我想找其他的东西怎么办,你这代理就找不了了,这不坏事了嘛
所以Version03应运而生
…未完待续



