栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

20220530

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

20220530

文章目录
装饰模式
定义抽象父类

public abstract class InputStream {
    
    public abstract int read();
    public abstract int read(byte[] buffer);
}

定义子类的原始输入流

import java.util.Arrays;

public class FileInputStream extends InputStream{
    @Override
    public int read() {
        System.out.println("Read a single byte [a] originally.");
        return 97;
    }

    @Override
    public int read(byte[] buffer) {
        buffer[0] = 97;
        buffer[1] = 98;
        buffer[2] = 99;
        System.out.println("Read a byte array originally: "+ Arrays.toString(buffer));
        return 3;
    }
}

定义子类缓冲输入流

public class BufferedInputStream extends InputStream{

    private InputStream is;

    public BufferedInputStream(InputStream is) {
        this.is = is;
    }

    
    @Override
    public int read() {
        System.out.println("offer a 8kb size buffer, enhance the reading performance.");
        return is.read();
    }

    @Override
    public int read(byte[] buffer) {
        System.out.println("offer a 8kb size buffer, enhance the reading performance.");
        return is.read(buffer);
    }
}

装饰模式的测试执行类

public class DecoratorPatternDemo1 {
    public static void main(String[] args) {
        
        InputStream is  = new BufferedInputStream(new FileInputStream());
        System.out.println(is.read());
        System.out.println(is.read(new byte[3]));
    }
}

文章目录
工厂模式
抽象父类

public abstract class GameMachine {
    private String name;
    private double price;

    public abstract void start();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

子类一

public class PS5 extends GameMachine{

    @Override
    public void start() {
        System.out.println("["+getName()+"] start running Redemption1~~~");
    }
}

子类二

public class XboxS extends GameMachine{
    @Override
    public void start() {
        System.out.println("["+getName()+"] start running Redemption2~~~");
    }
}

工厂类

public class Factory {
    

    public static GameMachine createGamer(String info){
        switch (info){
            case "PS5":
                GameMachine gm = new PS5();
                gm.setName("PlayStation 5");
                gm.setPrice(9999.9);
                return gm;
            case "xbox":
                GameMachine gm1 = new XboxS();
                gm1.setName("Xbox 1S");
                gm1.setPrice(10000.0);
                return gm1;
            default:
                return null;
        }
    }
}

执行体

public class FactoryDemo1 {
    public static void main(String[] args) {
        GameMachine gm1 = Factory.createGamer("xbox");

        GameMachine gm2 = Factory.createGamer("PS5");

        gm1.start();
        gm2.start();

    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939608.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号