Java设计模式 简单工厂模式

Java设计模式 简单工厂模式

简单工厂模式可以把对象的创建和对象的使用分离分开,工厂只负责对象的创建,客户端程序调用和使用对象,客户端程序无需创建对象。对象的创建放在一起,方便维护和扩展。

简单工厂模式主要由下列几种类构成

  • Test 类:来对设计模式代码进行测试
  • Type_father 类:要生产的父类
  • Type_son 类:父类型所属的子类
  • Factory 类:生产 Type 实例

计算器实例

Test 类:用于测试

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        try {
            Scanner input = new Scanner(System.in);
            System.out.print("请输入数字A: ");
            String strNumberA = input.nextLine();
            System.out.print("请选择运算符号(+、-、*、/): ");
            String strOperate = input.nextLine();
            System.out.print("请输入数字B: ");
            String strNumberB = input.nextLine();
            String strResult = "";

            Compute compute;
            double res = 0.0;
            compute = ComputeFactory.createCount(strOperate);
            compute.setA(Double.valueOf(strNumberA));
            compute.setB(Double.valueOf(strNumberB));

            res = compute.getResult();
            strResult = String.valueOf(res);
            System.out.println("结果是" + strResult);
        } catch (Exception e) {
            System.out.println("除数不能为零");
        }
    }
}

Compute 类:用于构建要计算的实例

public class Compute {
    private double a;
    private double b;
    private double res;

    Compute() {

    }

    Compute(int a, int b) {
        this.a=a;
        this.b=b;
    }

    public double getA() {
        return a;
    }

    public double getB() {
        return b;
    }

    public void setA(double a) {
        this.a = a;
    }

    public void setB(double b) {
        this.b = b;
    }

    double getResult() {
        return res;
    }

}

ComputeFactory 类:工厂类,用于构建运算符

public class Compute {
    private double a;
    private double b;
    private double res;

    Compute() {

    }

    Compute(int a, int b) {
        this.a=a;
        this.b=b;
    }

    public double getA() {
        return a;
    }

    public double getB() {
        return b;
    }

    public void setA(double a) {
        this.a = a;
    }

    public void setB(double b) {
        this.b = b;
    }

    double getResult() {
        return res;
    }

}

AddSubDivMul 类:作为 Compute 类的子类,重写 getResult 方法来实现各种运算

public class Add extends Compute {

    @Override
    double getResult() {
        return getA() + getB();
    }
}

public class Sub extends Compute {
    @Override
    double getResult() {
        return getA() - getB();
    }
}

public class Mul extends Compute {
    @Override
    double getResult() {
        return getA() * getB();
    }
}

public class Div extends Compute {
    private static final int ERROR = Integer.MAX_VALUE;

    @Override
    double getResult() {
        if (getB() == 0) return ERROR;
        else return getA() / getB();
    }
}

UML 类图

包 Lecture2_SimpleFactory

账户创建实例

这个实例实现了根据输入的权限名称,创建一个不同的用户账户类型并给与创建提示

Test 类:创建用户账户

import java.util.Scanner;

public class Test {
    static String accountType;
    static String username;
    static String password;


    public static void main(String[] args) {
        try {
            System.out.println("请输入你想创建的账号类型");
            Scanner scanner = new Scanner(System.in);
            accountType = scanner.next();
            System.out.println("请输入要创建账号的用户名");
            username = scanner.next();
            System.out.println("请输入要创建账号的密码");
            password = scanner.next();

            Account account;
            account = AccountFactory.createAccount(accountType);
            account.setUsername(username);
            account.setPassword(password);

            int permissionLevel = account.getPermissionLevel();
            String accountInfo = account.getAccountInfo();

            System.out.println("账号创建成功!");
            System.out.println("你的权限等级是:" + permissionLevel);
            System.out.println(accountInfo);
        } catch (Exception e) {
            if (e.getMessage() == null) {
                System.out.println("此账号类型不存在!");
            }
        }

    }
}

Account 类:所有 Account 类型的父类

public class Account {
    private String username;
    private String password;
    private String userType;
    private String accountInfo;
    private int permissionLevel;

    Account() {

    }

    //账户提示信息的get方法
    public String getAccountInfo() {
        return accountInfo;
    }


    //permissionLevel的get和set方法
    public int getPermissionLevel() {
        return permissionLevel;
    }

    public void setPermissionLevel(int permissionLevel) {
        this.permissionLevel = permissionLevel;
    }

    //password的get和set方法
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    //Username的get和set方法
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    //UserType的get和set方法
    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

}

AccountFactory 类:构建 Account_type

public class AccountFactory {
    public static Account createAccount(String selectedAccountType) {
        return switch (selectedAccountType) {
            case "Tourist" -> new AccountType_Tourist();
            case "User" -> new AccountType_User();
            case "Admin" -> new AccountType_Admin();
            case "SuperAdmin" -> new AccountType_SuperAdmin();
            default -> null;
        };
    }
}

Account_type 类:创建 Account 实例

public class AccountType_Admin extends Account {
    @Override
    public int getPermissionLevel() {
        return 2;
    }

    @Override
    public String getAccountInfo() {
        return "你拥有对部分数据库进行增删查改的权限";
    }
}

public class AccountType_SuperAdmin extends Account {
    @Override
    public int getPermissionLevel() {
        return 3;
    }

    @Override
    public String getAccountInfo() {
        return "你拥有查看所有数据库,对所有数据库进行增删查改的权限";
    }
}

public class AccountType_Tourist extends Account {
    @Override
    public int getPermissionLevel() {
        return 0;
    }

    @Override
    public String getAccountInfo() {
        return "你拥有浏览的权限";
    }
}

public class AccountType_Tourist extends Account {
    @Override
    public int getPermissionLevel() {
        return 0;
    }

    @Override
    public String getAccountInfo() {
        return "你拥有浏览的权限";
    }
}

UML 类图

ch2_20191003157_2_UML

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值