Learning Design Patterns Series - Simple Factory pattern (Java)
Advertisements
For example:
The first step: one apple and banana class category, which have the same put off the method get (), code as follows:
package com.wds.simplefactory;
/**
* Banana class
* 2010-6-26 In the afternoon 09:46:59
*/
public class Banana{
/**
* Acquisition
* 2010-6-26 In the afternoon 09:47:16
*/
public void get(){
System.out.println(" Acquisition of bananas ");
}
}
Apple class as follows:
/**
* Caoguo class
* 2010-6-26 In the afternoon 09:45:41
*/
public class Apple{
/**
* Acquisition of apples
* 2010-6-26 In the afternoon 09:46:20
*/
public void get(){
System.out.println(" Acquisition of apples ");
}
}
Main class MainClass:
/**
* The main class
* 2010-6-26 In the afternoon 09:45:29
*/
public class MainClass {
/**
* 2010-6-26 In the afternoon 09:45:29
* @param args
*/
public static void main(String[] args) {
/*
* Instantiate a Apple
*/
Apple apple=new Apple();
/*
* Instantiate a Banana
*/
Banana banana=new Banana();
apple.get();
banana.get();
}
}
Step two: former first completed a preliminary operations, but the apples and bananas in the two classes Du a pick of Method get (),, so we collected from two classes in a Fruit interfaces, code:
/**
* Fruit Interface
* 2010-6-26 In the afternoon 09:50:57
*/
public interface Fruit {
/**
* The removal of
*/
public void get();
}
Then apple and banana class implements Fruit class interface code as follows:
/**
* Apple class
* 2010-6-26 In the afternoon 09:45:41
*/
public class Apple implements Fruit{
/**
* Acquisition of apples
* 2010-6-26 In the afternoon 09:46:20
*/
public void get(){
System.out.println(" Acquisition of apples ");
}
}
/**
* Banana class
* 2010-6-26 In the afternoon 09:46:59
*/
public class Banana implements Fruit{
/**
* Acquisition
* 2010-6-26 In the afternoon 09:47:16
*/
public void get(){
System.out.println(" Acquisition of bananas ");
}
}
Master class on instances of Apple and Banana change the code as follows:
/**
* The main class
* 2010-6-26 In the afternoon 09:45:29
*/
public class MainClass {
/**
* 2010-6-26 In the afternoon 09:45:29
* @param args
*/
public static void main(String[] args) {
// Use the interface to instantiate a Apple
Fruit apple=new Apple();
// Use the interface to instantiate a Banana
Fruit banana=new Banana();
apple.get();
banana.get();
}
}
The third step: Apple and Banana You as a common interface, and simple factory pattern, defined by a class devoted to Fuzechuangjian instances of other classes, were created in the Shi Li usually share common parent class. So we create a fruit factory class to create Apple and Banana FruitFactory the two classes.
FruitFactory code:
/**
* Fruit factory class
* 2010-6-26 In the afternoon 09:53:48
*/
public class FruitFactory {
/**
* Obtain an instance of the Apple class
*/
public static Fruit getApple(){
return new Apple();
}
/**
* Obtain an instance of a Banana
* 2010-6-26 In the afternoon 09:54:47
* @return
*/
public static Fruit getBanan(){
return new Banana();
}
}
Then there is the main class with the changes in the code below
MainClass
/**
* The main class
* 2010-6-26 In the afternoon 09:45:29
*/
public class MainClass {
/**
* 2010-6-26 In the afternoon 09:45:29
* @param args
*/
public static void main(String[] args) {
Fruit apple=FruitFactory.getApple();
Fruit banana=FruitFactory.getBanan();
apple.get();
banana.get();
}
}
The fourth step, as in the factory class, is a get method, just return the contents are different, we change another way, only get one way, according to different parameters decided to return to the class different, FruitFactory code:
/**
* Fruit factory class
* 2010-6-26 In the afternoon 09:53:48
*/
public class FruitFactory {
/**
* get Method, get all the product object
* Exception I to ignore, when we write added
*/
public static Fruit getFruit(String type){
if(type.equalsIgnoreCase("apple")){
return Apple.class.newInstance();
}else if(type.equalsIgnoreCase("banana")){
return Banana.class.newInstance();
}else{
System.out.println("can not find class");
return null;
}
}
The fifth step, because the factory class to judge fruit more statements, we created for a more general way, the code is as follows:
/**
* Fruit factory class
* 2010-6-26 In the afternoon 09:53:48
*/
public class FruitFactory {
/**
* get Method, get all the product object
* @throws ClassNotFoundException
* @throws IllegalAccessException
* @throws InstantiationException
*/
public static Fruit getFruit(String type) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
// Here, the reason why plus com.wds.simplefacotry, Because Apple class and Banana Class and package with a FruitFactory class below , The package name is com.wds.simplefactory
Class fruit=Class.forName("com.wds.simplefactory."+type);
//System.out.println(FruitFactory.class.getResource(""));
//Class fruit=Class.forName(FruitFactory.class.getClassLoader().getResource("")+type);
return (Fruit) fruit.newInstance();
}
}
Thus, the factory class has been completed, then the main class code change, change as follows:
/**
* The main class
* 2010-6-26 In the afternoon 09:45:29
*/
public class MainClass {
/**
* 2010-6-26 In the afternoon 09:45:29
* @param args
*/
public static void main(String[] args) {
try {
Fruit banana=FruitFactory.getFruit("Banana");
Fruit apple = FruitFactory.getFruit("Apple");
apple.get();
banana.get();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Related Posts of Learning Design Patterns Series - Simple Factory pattern (Java)
-
hibernate study of the second
Persistence of three main points: 1, a statement for persistent fields accessors (accessors) and whether the variable signs (mutators) Property statement is not necessarily required for the public's. Hibernate can be default, protected or private ...
-
Real design pattern
I hate the thought of the time to teach some provide a lot of code, because I think the thought process of acceptance should be a pleasure to read as a novel process, rather than spend a lot of brain power for a certain process details And this world ...
-
Software development sunflower Baodian [reprint]
Master the ability to reuse code very familiar with the new API's fast. This is because, he once used a lot of the API, have a lot of reusable code. He knows what is available and what is deficient. He has been using Qt, also used by gtk +, also used
-
hibernate (jpa) composite primary key annotation statement Ways
In the design of the database tables are designed with a composite primary key of the table, that table's record by more than one field joint identification, such as: Table CREATE TABLE TB_HOUR_DATA ( STAT_DATE DATE NOT NULL, PATH_ID NUMBER(20) NOT NULL,
-
log4j easy application in java
JAVA development, frequently used the log output, in a so-called most of the software company will have its own set of configuration style, re-read the configuration file to initialize property of the log, it will be good, but sometimes may not need to fu
-
jboss ejb3 Message Driven Bean
Super Medium ejb hate. . . . . . . . . . . . . . . . . . . ================================================ To configure a Message Driven Bean in a different application server parameters are not the same. Currently only passed the test jboss. Message Dri
-
Some interview questions java
The first is the company give you a chance to meet, it is necessary to know to meet from time to equal the interview, and have a lot of companies to see you at the first time will give you a ready point of doing something trivial, these questions, althoug
-
hibernate generic generic DAO
package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *
-
First Hibernate Example
Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co












