java代码例子
㈠ 求java工厂模式的一个简单代码例子,尽量简单
这个应该比较简单一点。
某系统日志记录器要求支持多种日志记录方式,如文件记录、数据库记录等,且用户可以根据要求动态选择日志记录方式。现使用工厂方法模式设计该系统,并写出相应Java代码。
interface Log{
public void writeLog();
}
class FileLog implements Log{
public void writeLog(){
System.out.println("文件记录");
}
}
class DatabaseLog implements Log{
public void writeLog(){
System.out.println("数据库记录");
}
}
interface LogFactory{
public Log createLog();
}
class FileLogFactory implements LogFactory{
public Log createLog(){
return new FileLog();
}
}
class DatabaseLogFactory implements LogFactory{
public Log createLog(){
return new DatabaseLog();
}
}
public class Client{
public static void main(String[] args) {
try{
Log log;
LogFactory factory;
//这里可以改成使用DOM和Java反射机制读取XML文件,获取工厂类名
factory=new DatabaseLogFactory ();
log=factory.createLog();
log.writeLog();
}
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
㈡ java中的单例模式的代码怎么写
单例模式(Singleton) ,属于最常见的设计模式之一,大部分系统都会用到,目的是为了维护系统中唯一的一个实例。
可分为eager模式,示例代码如下:
Java代码
1.class EagerSingleton{
2. private static final EagerSingleton m_instance = new EagerSingleton();
3. private EagerSingleton(){}
4. public static EagerSingleton getInstance(){
5. return m_instance;
6. }
7.}
class EagerSingleton{
private static final EagerSingleton m_instance = new EagerSingleton();
private EagerSingleton(){}
public static EagerSingleton getInstance(){
return m_instance;
}
}
和 lazy模式,示例代码如下:
Java代码
1.class LazySingleton{
2. private static LazySingleton m_instance = null;
3. private LazySingleton(){}
4. public synchronized static getInstance(){
5. if(m_instance == null){
6. m_instance = new LazySingleton();
7. }
8. return m_instance;
9. }
10.}
class LazySingleton{
private static LazySingleton m_instance = null;
private LazySingleton(){}
public synchronized static getInstance(){
if(m_instance == null){
m_instance = new LazySingleton();
}
return m_instance;
}
}
java源码中,Runtime.getRuntime()就是单例的一个例子。
单例模式的精神就是整个系统中维护一个实例,推广开来,如果在一个系统中需要维护多个示例,那么就产生了多例模式(multiton)。
多例模式(Multiton) ,通过聚集对象了保留自身的多个示例,根据客户端的参数返回所需要的实例。
示例代码如下:
Java代码
1.class Multiton{
2. private final int INSTANCE_SIZE = 10;
3. private static Map instances = new HashMap(INSTANCE_SIZE);
4. private String name;
5. private Multiton(){}
6. private Multiton(String name){
7. this.name = name;
8. }
9. public synchronized static getInstance(String name){
10. if(instances.containsKey(name)){
11. return instances.get(name);
12. }
13. else{
14. ins = new Multiton(name);
15. instances.put(name, ins);
16. return ins;
17. }
18. }
19.}
class Multiton{
private final int INSTANCE_SIZE = 10;
private static Map instances = new HashMap(INSTANCE_SIZE);
private String name;
private Multiton(){}
private Multiton(String name){
this.name = name;
}
public synchronized static getInstance(String name){
if(instances.containsKey(name)){
return instances.get(name);
}
else{
ins = new Multiton(name);
instances.put(name, ins);
return ins;
}
}
}
[nextpage]
一个实用的例子就是KeyGenerator, 示例代码如下:
Java代码
1.class KeyGenerator{
2. private final int POOL_SIZE = 20;
3. private static Map instances = new HashMap(16);
4. private KeyInfo keyinfo;
5. private KeyGenerator(){}
6. private KeyGenerator(String keyName){
7. this.keyinfo = new KeyInfo(POOL_SIZE, keyName);
8. }
9. public synchronized static getInstance(String keyName){
10. if(instances.containsKey(keyName)){
11. return (KeyGenerator)instances.get(keyName);
12. }
13. else{
14. keyGen = new KeyGenerator(keyName);
15. instances.put(name, keyGen);
16. return keyGen;
17. }
18. }
19. public synzhronized int getNextKey(){
20. return keyinfo.getNextKey();
21. }
22. }
class KeyGenerator{
private final int POOL_SIZE = 20;
private static Map instances = new HashMap(16);
private KeyInfo keyinfo;
private KeyGenerator(){}
private KeyGenerator(String keyName){
this.keyinfo = new KeyInfo(POOL_SIZE, keyName);
}
public synchronized static getInstance(String keyName){
if(instances.containsKey(keyName)){
return (KeyGenerator)instances.get(keyName);
}
else{
keyGen = new KeyGenerator(keyName);
instances.put(name, keyGen);
return keyGen;
}
}
public synzhronized int getNextKey(){
return keyinfo.getNextKey();
}
}
㈢ 用java编写一个简单例子,题目如下
package test;
public class Student {
private String name;
private String id;
private String clazz;
private int age;
private String address;
/**
* sayHello方法
*/
public void sayHello() {
System.out.println("学号为" + this.id + "的同学的具体信息如下:");
System.out.println("姓名:" + this.name);
System.out.println("班级:" + this.clazz);
System.out.println("年龄:" + this.age);
System.out.println("家庭住址:" + this.address);
}
/**
* 测试方法
*
* @param args
*/
public static void main(String[] args) {
// 第一问
Student student = new Student();
student.setAddress("网络知道");
student.setAge(1);
student.setClazz("一班");
student.setId("071251000");
student.setName("lsy605604013");
student.sayHello();
// 第二问
Student studentNew = new Student();
studentNew.setAddress("搜搜知道");
studentNew.setAge(2);
studentNew.setClazz("二班");
studentNew.setId("071251001");
studentNew.setName("lady");
if (student.getAge() < studentNew.getAge())
studentNew.sayHello();
else if (student.getAge() > studentNew.getAge())
student.sayHello();
else
System.out.println("两人一样大");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
㈣ 求一个简单的java代码
public class Message {
public static void main(String[] args){
String name;
int age;
System.out.println("请输入姓名,回车结束:"); //提示输入
Scanner sc = new Scanner(System.in);
name = sc.nextLine(); //为变量赋值
System.out.println("请输入年龄,回车结束:");
age = sc.nextInt();
System.out.println("姓名:"+name+"\n年龄:"+age); //打印姓名及年龄
}
}
//不知道这样行么?