首页 > 其他分享 >实验2:简单工厂模式

实验2:简单工厂模式

时间:2024-10-23 19:21:23浏览次数:1  
标签:Woman java Person System 模式 工厂 person 实验 public

[实验任务一]:女娲造人

使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,如果传入参数R,则返回一个Robot对象。请用程序设计实现上述场景。

实验要求:

1.画出对应的类图;

2.提交源代码;

3.注意编程规范。

类图:

 

 

2、代码

Main.java

package org.example;

import java.util.Scanner;
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner type = new Scanner(System.in);
        System.out.print("请输入参数: ");
        String s = type.nextLine();
        Person person = Nvwa.getPerson(s);
        person.create();
        type.close();
    }
}

Man.java

package org.example;

//具体产品类:Man(男人)类
public class Man implements Person{
    public Man() {
    }

    @Override
    public void create() {
        System.out.println("造男人");
    }
}

Nvwa.java

package org.example;

//工厂类:Nvwa(女娲)类
public class Nvwa {
    public static Person getPerson(String person) throws Exception {
        if (person.equalsIgnoreCase("M")){
            return new Man();
        }else if (person.equalsIgnoreCase("W")){
            return new Woman();
        }else if (person.equalsIgnoreCase("R")){
            return new Robot();
        }else {
            throw new Exception("对不起,不能造该类人");
        }
    }
}

person接口

package org.example;

//抽象产品类:Person接口
public interface Person {
    void create();
}

Robot.java

package org.example;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner type = new Scanner(System.in);
        System.out.print("请输入参数: ");
        String s = type.nextLine();
        Person person = Nvwa.getPerson(s);
        person.create();
        type.close();
    }
}

Woman.java

package org.example;

//具体产品类:Woman(女人)类
public class Woman implements Person{
    public Woman() {
    }

    @Override
    public void create() {
        System.out.println("造女人");
    }
}

代码运行:

 

 

标签:Woman,java,Person,System,模式,工厂,person,实验,public
From: https://www.cnblogs.com/yuanxinglan/p/18498121

相关文章

  • 实验3
    task1点击查看代码#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:%......
  • 实验2:简单工厂模式
    [实验任务一]:女娲造人使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,如果传入参数R,则返回一个Robot对象。请用程序设计实现上述场景。  1. 类图   2.源代码//抽象产品类:Person接口publicinterfaceP......
  • 20222404 2024-2025-1《网络与系统攻防》 实验二
    1.实验内容(一)本周课程内容了解后门概念,了解后门案例,后门会对系统安全造成的影响。对后门技术进行普及,包括各种进程隐藏技术。了解netcat、meterpreter,veil等常见工具。进一步学习shellcode注入的逻辑和多种情况。(二)问题回答(1)例举你能想到的一个后门进入到你系统中的可能......
  • 实验2 类和对象_基础编程1
    实验任务1代码:t.h:1#pragmaonce23#include<string>45classT{6public:7T(intx=0,inty=0);8T(constT&t);9T(T&&t);10~T();11voidadjust(intratio);12voiddisplay()const;13private......
  • 实验2 类和对象_基础编程1
    task1: t.h:#pragmaonce#include<string>//类T:声明classT{//对象属性、方法public:T(intx=0,inty=0);//普通构造函数T(constT&t);//复制构造函数T(T&&t);//移动构造函数~T();//析构函数void......
  • Java设计模式的学习之适配器模式
    适配器模式目录适配器模式适配器模式的主要角色包括:适配器模式的两种主要形式:应用场景:优点:缺点:适配器模式(AdapterPattern)是一种结构型设计模式,其主要目的是将一个类的接口转换成客户端期望的另一个接口,使得原本因接口不兼容而不能一起工作的类可以一起工作。适配器模式通常用......
  • OpenSSL异步模式流程梳理
    源码来源于OpenSSLMasterCommitIDd550d2aae531c6fa2e10b1a30d2acdf373663889。总览核心入口函数为ssl_start_async_job,以SSL_do_handshake为入口举例分析,同时通过标注步骤【1~N】,来明确阅读的顺序。步骤【1】到步骤【18】为一个阶段步骤【19】到步骤【23】为一个阶......
  • 实验三
    任务一验证性实验源码#include<stdio.h>charscore_to_grade(intscore);//函数声明intmain(){intscore;chargrade;while(scanf("%d",&score)!=EOF){grade=score_to_grade(score);//函数调用printf("分数:......
  • 实验3
    TASK11#include<stdio.h>23charscore_to_grade(intscore);45intmain(){6intscore;7chargrade;89while(scanf("%d",&score)!=EOF){10grade=score_to_grade(score);11printf(......
  • 产品有了模式切换功能,众口不再难调!
    在科技飞速发展的今天,我们的产品迎来了重大突破——模式切换功能。这一创新之举,让众口不再难调。无论你是追求高效便捷的商务人士,还是喜欢沉浸式体验的娱乐爱好者,亦或是注重简约实用的日常用户,都能在不同模式中找到最适合自己的状态。轻松切换,满足你的各种需求。从此,不再为......