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); //列印姓名及年齡
}
}
//不知道這樣行么?