SimipleFactory 简单工厂模式

本文介绍了一个简单的工厂模式应用案例,通过定义水果接口及其多种实现类(如苹果、葡萄和草莓),展示了如何根据不同需求创建不同类型的对象。同时,还提供了一个异常处理机制来应对非法请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Fruti.java

package com.tcs.SimipleFactory;
/**
* The interface of Fruit difine the interface which all fruit must implement its method;
*/
public interface Fruit {
void grow();
void harvest();
void plant();
}

Apple.java
package com.tcs.SimipleFactory;
/**
* Apple implements Fruit and also it has its own attribute and method, such as treeAge and method log();
*/

public class Apple implements Fruit {
//Apple 出了继承Fruit之外,还有自己的一个属性 treeAge。

private int treeAge;



public void grow() {
log("Apple is growing");

}

public void harvest() {
log("Apple has been harvested");

}

public void plant() {
log("Apple has been planted");

}

public static void log(String s){
System.out.println(s);
}

/**
* @return the treeAge
*/
public final int getTreeAge() {
return treeAge;
}

/**
* @param treeAge the treeAge to set
*/
public final void setTreeAge(int treeAge) {
this.treeAge = treeAge;
}
}


Grape.java
package com.tcs.SimipleFactory;
/**
* Grape implements Fruit and also it has its own attribute and method, such as seedLess and method log();
*/

public class Grape implements Fruit {
private boolean seedLess;
public void grow() {
log("Grape is growing");

}

public void harvest() {
log("Grape has been harvested");

}

public void plant() {
log("Grape has been planted");

}

public static void log(String s){
System.out.println(s);
}

/**
* @return the seedLess
*/
public final boolean isSeedLess() {
return seedLess;
}

/**
* @param seedLess the seedLess to set
*/
public final void setSeedLess(boolean seedLess) {
this.seedLess = seedLess;
}



}

StrawBerry.java
package com.tcs.SimipleFactory;
/*
*StrawBerry implements Fruit
*
*/
public class StrawBerry implements Fruit {

public void grow() {
log("StrawBerry is growing");

}

public void harvest() {
log("StrawBerry has been harvested");

}

public void plant() {
log("StrawBerry has been planted");

}

public static void log(String s){
System.out.println(s);
}
}

FruitGardener.java
package com.tcs.SimipleFactory;
/*
*
*Gardener will create some fruit by his client's request,Also if the request is a bad one.He will throws it a BadFruitException which he create it own
*/
public class FruitGardener {
public static Fruit factory(String which) throws BadFruitException {
if (which.equalsIgnoreCase("apple")) {
return new Apple();
} else if (which.equalsIgnoreCase("strawberry")) {
return new StrawBerry();
} else if (which.equalsIgnoreCase("grape")) {
return new Grape();
} else {
throw new BadFruitException("Bad Fruit Request");
}
}
}

BadFruitException.java
package com.tcs.SimipleFactory;
/*
*This exception was create by Gardener and handing Exception
*
*/
public class BadFruitException extends Exception {
public BadFruitException(String msg) {
super(msg);
}
}


TestMain.java
package com.tcs.SimipleFactory;
/*
*when test this at client,Customer just call FruitGardener's method factory.Then there will be much fruit to plant grow and harvest.Also hand some error request
*
*
*/
public class TestMain {

/**
* @param args
*/
public static void main(String[] args) {
try {
FruitGardener.factory("apple").grow();//call method which you want to call
FruitGardener.factory("strawberry").harvest();//call method which you want to call
FruitGardener.factory("grape").plant();//call method which you want to call
FruitGardener.factory("bad request"); //test when bad fruit
} catch (BadFruitException e) {
e.printStackTrace();
}

}

}
资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 以下是关于西林电气EH600LC系列变频器说明书的要点概述: 这是一份针对使用EH600LC系列变频器的用户编写的中文说明书,主要目的是指导用户如何正确地安装、操作以及维护该系列变频器。 从说明书的部分内容来看,变频器的基本参数如下:输入电压为三相AC380V,频率为50Hz或60Hz;输出频率范围是0Hz至650Hz;以EH640LC型号为例,输入电流为5.0A,输出电流为3.7A;该系列变频器可能涵盖1.2kW至40kW的可变功率范围。 变频器的型号标识为EH600LC系列,包含多个不同规格的产品,以满足多样化的应用需求。产品序列号显示为“SHENZHENXILINELECTRICTECHNOLOGIESCO.,LTD.MADEINCHINA”,表明该产品由西林电气公司生产,产地是中国深圳。 在操作功能方面,变频器具备如“JOG”(点动运行)等多种操作功能,还可能有频率、电流、电压等参数的设定与调整功能,同时提供了模拟信号接口、数字信号接口等多种控制接口。控制接口有FD.10、FB.11等代码标识,例如FB.11=0或FB.11=1可能表示数字输入信号的不同状态。 变频器设有多个LED指示灯,比如LED、F4.00、F6.01等,用于指示工作状态,包括电源状态、运行状态、故障状态等,也可能有用于显示特定故障或警告的LED指示灯。频率调节范围可在0.01Hz到20.00Hz、50.00Hz、650.00Hz等不同区间灵活调整,设定频率时可设置0.1s、0.5s、1.0s等不同的加速和减速时间。 说明书还对变频器的输入输出接口进行了说明,X1、X2、X3等标记代表不同的接口端子,其中包含用于24V直流电源输入的端子E24VX4CM。变频器可能支持通过DI(数字输入)信号等外部控制信号进
标题基于SpringBoot+Vue的社区便民服务平台研究AI更换标题第1章引言介绍社区便民服务平台的研究背景、意义,以及基于SpringBoot+Vue技术的研究现状和创新点。1.1研究背景与意义分析社区便民服务的重要性,以及SpringBoot+Vue技术在平台建设中的优势。1.2国内外研究现状概述国内外在社区便民服务平台方面的发展现状。1.3研究方法与创新点阐述本文采用的研究方法和在SpringBoot+Vue技术应用上的创新之处。第2章相关理论介绍SpringBoot和Vue的相关理论基础,以及它们在社区便民服务平台中的应用。2.1SpringBoot技术概述解释SpringBoot的基本概念、特点及其在便民服务平台中的应用价值。2.2Vue技术概述阐述Vue的核心思想、技术特性及其在前端界面开发中的优势。2.3SpringBoot与Vue的整合应用探讨SpringBoot与Vue如何有效整合,以提升社区便民服务平台的性能。第3章平台需求分析与设计分析社区便民服务平台的需求,并基于SpringBoot+Vue技术进行平台设计。3.1需求分析明确平台需满足的功能需求和性能需求。3.2架构设计设计平台的整体架构,包括前后端分离、模块化设计等思想。3.3数据库设计根据平台需求设计合理的数据库结构,包括数据表、字段等。第4章平台实现与关键技术详细阐述基于SpringBoot+Vue的社区便民服务平台的实现过程及关键技术。4.1后端服务实现使用SpringBoot实现后端服务,包括用户管理、服务管理等核心功能。4.2前端界面实现采用Vue技术实现前端界面,提供友好的用户交互体验。4.3前后端交互技术探讨前后端数据交互的方式,如RESTful API、WebSocket等。第5章平台测试与优化对实现的社区便民服务平台进行全面测试,并针对问题进行优化。5.1测试环境与工具介绍测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值