23.设计模式+网络编程

1. 工厂设计模式

问题:二、百度汽车工厂生产了三种汽车,bus,truck,jeep,并且以后还有扩充,有三种引擎busengine,truckengine,jeepengine,今后也会添加种类。请对此进行类设计。

由一个类的方法创建一个对象。

Calendar c = Calendar.getInstace();  // g 静态工厂模式
  • 静态工厂模式;简单工厂模式;

    • 通过静态方法去创建对象

      	/**
      	 * 第三种 静态工厂
      	 * @param aaaa
      	 * @return
      	 */
      	public static 百度汽车 newinstance(Class<? extends 百度汽车> aaaa) {
      		try {
      			百度汽车 o1 = aaaa.getConstructor().newInstance();
      			return o1;
      		} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
      				| NoSuchMethodException | SecurityException e) {
      			// TODO Auto-generated catch block
      			e.printStackTrace();
      		}
      		return null;
      	}
      
  • 工厂方法模式:

    • 非静态方法创建对象:

      	public 百度汽车 newinstance(int flg) {
      		
      		if (flg == 0) {
      			return new 公交车();
      		} else if (flg == 1) {
      			return new 吉普车();
      		} else if (flg == 2) {
      			return new 卡车();
      		} else { // 再加的话。。。继续修改
      			return null;
      		}
      	}
      
      	/**
      	 * 第二种
      	 * @return
      	 */
      	public 百度汽车 newinstance0() {
      		return new 公交车();
      	}
      	public 百度汽车 newinstance1() {
      		return new 吉普车();
      	}
      	public 百度汽车 newinstance2() {
      		return new 卡车();
      	}
      
  • 抽象工厂模式: 工厂里有抽象方法

在这里插入图片描述

2. 单例模式

在一个系统中,一个类只有一个对象。

  • 饿汉式: 防止饿,开始的时候就实例化。

    • 构造器私有化
    • 定义私有静态常量, 实例化赋值
    • 公开方法返回私有静态变量
    package com.etc.lesson23.single;
    
    public class Person {
    	// 构造私有化
    	private Person() {
    	}
    	// 定义自己类的属性
    	private static final Person p = new Person();
    	// 公开方法访问
    	public static Person getInstance() {
    		return p;
    	}
    }
    
  • 懒汉式: 用的到时候实例化。

    • 构造私有化
    • 定义私有静态变量,
    • 公开同步方法,方法中判断实例化
    public class Person1Lazy {
    
    	private Person1Lazy() {
    		
    	}
    	
    	private static Person1Lazy person;
    	
    	public static synchronized Person1Lazy getInstance() {
    		// 懒汉式
    		if (person == null) {
    			person = new Person1Lazy();
    		}
    		return person;
    	}
    }
    package com.etc.lesson23.single;
    public class TestMain2 {
    
    	public static void main(String[] args) {
    		Person p = Person.getInstance();
    		Person p2 = Person.getInstance();
    		System.out.println(p == p2); // true;
    		Math.random();
    		
    		Person1Lazy p3 = Person1Lazy.getInstance();
    		Person1Lazy p4 = Person1Lazy.getInstance();
    		System.out.println(p3 == p4);
    	}
    }
    

    3. Stream流运算:

    filter(); boolean型的判断过滤

    map(); 匹配,新值

    reduce(); 分析计算结果

    count();/ limit()/distinct();


consumer: 一个参数无返回值: forEach

Predicate: 一个参数,boolean返回值: filter

Function: 一个参数,有T类型返回值: map

BI+Function: 两个参数,有T类型返回值: reduce

4. 网络编程:

  • URL: 统一资源定位符

  • URI:统一资源标识符 包含URL

  • TCP-IP: 一种协议-保证信息的 三次握手。 :: 视频通话两个都在

    • HTTP: 基于tcp/ip网页浏览器BS系统用的: 超文本传输协议
    • FTP: 文件传输协议
  • UDP: 协议支持一个无连接的传输协议。 :: 短信群发:不管另一端有没有人


4.1 套接字编程: socket: 两端通信

  • ServerSocket: 服务端套接字: 指定端口

    • Socket accept(): 线程阻塞,等待客户端访问。 返回值就是客户端对象
  • Socket: 客户端套接字: 指定ip + 端口。 随机开本机端口访问。

    • getInputStream(); 获取输入流
    • getOutputStream(); 获取输出流
      在这里插入图片描述

Server:

package com.etc.lesson23;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestMain4 {

	public static void main(String[] args) {
		
		try {
			// 开启了服务的,绑定了1234端口			
			ServerSocket server = new ServerSocket(1234);
			ExecutorService pool = Executors.newCachedThreadPool();
			while (true) {
				// 线程阻塞- 等待客户端
				Socket client = server.accept();
				System.out.println(client.getRemoteSocketAddress() + "@" + client.getPort());
				
				InputStream is = client.getInputStream();
				BufferedReader br = new BufferedReader(new InputStreamReader(is));
				String ip = client.getRemoteSocketAddress().toString();
				pool.submit(() -> {
					try {
						while (true) {
							if (br.ready()) {
	
								String str = br.readLine();
								System.out.println(Thread.currentThread() + "\t" + ip + "@@@"+  str);
							}
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				});
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Client

package com.etc.lesson23;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class TestMain5 {

	public static void main(String[] args) {
		
		try {
			Socket socket = new Socket("192.168.13.100", 1234);
			System.out.println(socket.getInetAddress());
			
			OutputStream os = socket.getOutputStream();
			PrintWriter pw = new PrintWriter(os);
			
			pw.write("hello world\r\n");
			pw.flush();
//			pw.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

posted @ 2021-04-12 14:41  剑心空明  阅读(0)  评论(0编辑  收藏  举报  来源