设计模式以及网络编程

设计模式

设计模式:是经过大量的经验积累得出的一个开发模板。
设计模式:

  • 创建型 创建对象
  • 结构型 对象的组成
  • 行为型 对象的功能

创建型模式:

  • 简单工厂模式
  • 工厂方法模式
  • 单例模式:
    • 饿汉式
    • 懒汉式
简单工厂模式
abstract class Animal1{
    abstract void eat();
}

class Cat1 extends Animal1{
    public void eat(){
        System.out.println("猫吃鱼");
    }
}

class Dog1 extends Animal1{
    public void eat(){
        System.out.println("狗吃肉");
    }
}

class AnimalFactory1{
    public static Animal createAnimal(String type){
        if("dog".equals(type)){
            return new Dog();
        }else if("cat".equals(type)){
            return new Cat();
        }else{
            return null;
        }
    }
}

public class AnimalDemo1 {
    public static void main(String[] args) {
        Animal a1 = AnimalFactory1.createAnimal("dog");
        Animal a2 = AnimalFactory1.createAnimal("cat");
        a1.eat();
        a2.eat();
        Animal a3 = AnimalFactory1.createAnimal("name");
        if(a3!=null){
            a3.eat();
        }else{
            System.out.println("无法创建该动物");
        }
    }
}

工厂方式模式
abstract class Animal1{
    abstract void eat();
}

interface Factory1{
    abstract Animal1 createAnimal();
}

class Cat1Factory implements Factory1{
    @Override
    public Animal1 createAnimal() {
        return new Cat1();
    }
}

class Dog1Factory implements Factory1{
    @Override
    public Animal1 createAnimal() {
        return new Dog1();
    }
}

class Cat1 extends Animal1{
    public void eat(){
        System.out.println("猫吃鱼");
    }
}

class Dog1 extends Animal1{
    public void eat(){
        System.out.println("狗吃肉");
    }
}

public class AnimalDemo1 {
    public static void main(String[] args) {
        //我现在需要一只狗
        Animal1 dog = new Dog1Factory().createAnimal();
        dog.eat();

        //我现在需要一只猫
        Animal1 cat = new Cat1Factory().createAnimal();
        cat.eat();
    }
}

单例模式
单例模式:保证类在内存中只有一个对象
单例模式:

饿汉式:类一加载就创建对象
懒汉式:用的时候,再去创建对象
懒加载(延迟加载)
线程安全问题
是否有多线程环境
是否有共享数据
是否有多条语句操作共享数据
饿汉式
如何保证内存中只有一个对象呢:

构造方法私有
在成员位置上自己创建一个对象
提供一个公共的方法给外界去访问
也就是说,随着类的加载,对象就已经创建好了
这样的特点,在单例模式中我们称之为:饿汉式

class Student1{
    Student1(){}

    private static Student1 s = new Student1();

    public static Student1 getStudent1(){
        return s;
    }
}

public class StudentDemo1 {
    public static void main(String[] args) {
        //这样创造出来的对象不是单例的
        Student1 s1 = new Student1();
        Student1 s2 = new Student1();
        System.out.println(s1==s2);//false

        //这样创造出来的对象是单例的
        Student1 s3 = Student1.getStudent1();
        Student1 s4 = Student1.getStudent1();
        System.out.println(s3==s4);//true
    }
}

懒汉式
class Teacher1{
    private Teacher1(){}

    private static Teacher1 teacher1 = null;

    static synchronized Teacher1 getTeacher1(){
        if (teacher1==null){
            teacher1 = new Teacher1();
        }
        return teacher1;
    }
}

public class TeacherDemo1 {
    public static void main(String[] args) {
        Teacher1 t1 = Teacher1.getTeacher1();
        Teacher1 t2 = Teacher1.getTeacher1();
        System.out.println(t1==t2);//true
    }
}

网络编程

网络OSI参考模型

网络编程三要素

网络编程三要素
1、IP地址
2、端口
3、协议

Socket

Socket:网络套接字

UDP

 UDP协议发送数据
1、创建发送端Socket对象
2、创建数据,并且把数据打包
3、调用Socket对象的发送方法发送数据包
4、释放资源,关闭Socket

public class SendDemo1 {
    public static void main(String[] args) throws IOException {
        //创建发送端的Socket对象
        DatagramSocket ds = new DatagramSocket();

        //创建数据,并且把数据打包
        byte[] bytes = "helloworld".getBytes();

        //获取数组的长度
        int length = bytes.length;

        //IP地址对象
        InetAddress address = InetAddress.getByName("192.168.3.24");

        //设置端口号
        int port = 10086;

        //DatagramPacket(byte[] buf,int length,InetAddress address,int port)
        DatagramPacket dp = new DatagramPacket(bytes, length, address, port);

        //调用Socket对象的发送方法发送数据包
        //void send(DatagramPacket p)
        ds.send(dp);

        //关闭资源
        ds.close();
    }
}

UDP协议接收数据
1、创建接收端的Socket对象
2、创建一个数据包(接收容器)
3、调用Socket对象接收方法接收数据
4、解析数据包,并显示在控制台
5、释放资源,关闭Socket

public class ReceiveDemo1 {
    public static void main(String[] args) throws IOException {
        //创建一个接收端Socket对象
        //DatagramSocket(int port)
        //构造数据报套接字并将其绑定到本地主机上的指定端口
        DatagramSocket ds = new DatagramSocket(10086);

        //创建一个数据包(接收容器)
        byte[] bytes = new byte[1024];

        //数组长度
        int length = bytes.length;

        //构造一个DatagramPacket用于接收长度的数据包length
        DatagramPacket dp = new DatagramPacket(bytes, length);

        //调用Socket对象接收方法接收数据
        //void receive(DatagramPacket p)    从此套接字中接收数据包
        ds.receive(dp);//该方法阻塞,直到接收到数据包

        //InetAddress getAddress()
        //返回该数据包发送或接收数据包的计算机IP地址
        InetAddress address = dp.getAddress();
        String ip = address.getHostAddress();

        //解析数据包,并显示在控制台
        //byte[] getDate()
        //返回数据缓冲区
        byte[] data = dp.getData();
        //int getLength()
        //返回要发送的数据的长度或接收到的数据长度
        int length1 = dp.getLength();
        String s = new String(data, 0, length1);
        System.out.println(ip+"发送数据为:"+s);

        //释放资源
        ds.close();
    }
}

TCP

 //客户端   接收
    @Test
    public void client()  {
        Socket socket = null;
        OutputStream outputStream = null;
        try {
            //1.创建socket对象,指明服务器端的ip和端口号
            InetAddress inet = InetAddress.getByName("192.168.3.27");
            socket = new Socket(inet, 888);
            //2.获取一个输出流,用于输出数据
            outputStream = socket.getOutputStream();
            //3.写出数据的操作
            outputStream.write("你好,我是客户端".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
    //服务端   发送
    @Test
    public void server(){
        ServerSocket serverSocket = null;
        InputStream inputStream = null;
        try {
            //1.创建服务器端的ServerSocket,指明自己的端口号
            serverSocket = new ServerSocket(888);
            //2.调用accept()表示接受来自于客户端的socket
            Socket accept = serverSocket.accept();
            //3.获取输入流中的数据,并且读取
            inputStream = accept.getInputStream();
            byte[] bytes = new byte[1024];
            int len;
            while((len = inputStream.read(bytes) )!= -1){
                String s = new String(bytes, 0, len);
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 TCP以及UDP区别

三次握手

四次挥手 

 感谢观看!我是酷酷的涛!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值