惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tenable Blog
P
Privacy International News Feed
L
LINUX DO - 热门话题
T
Threatpost
Latest news
Latest news
C
Cybersecurity and Infrastructure Security Agency CISA
Cisco Talos Blog
Cisco Talos Blog
Cyberwarzone
Cyberwarzone
Spread Privacy
Spread Privacy
Recent Announcements
Recent Announcements
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
NISL@THU
NISL@THU
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
V
Vulnerabilities – Threatpost
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
H
Help Net Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cisco Blogs
Security Latest
Security Latest
博客园 - 叶小钗
博客园 - Franky
The Hacker News
The Hacker News
Engineering at Meta
Engineering at Meta
Scott Helme
Scott Helme
S
Securelist
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
A
About on SuperTechFans
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园_首页
B
Blog RSS Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
月光博客
月光博客
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tor Project blog
A
Arctic Wolf

博客园 - Star.Stroll

【Chrome扩展】Reload image 重载图片 [原创]RegularJS [原创]虚拟化技术真的能让企业降低成本吗? [原创]纯Java获得本地MAC地址 [原创]GCC动态链接库的编译与使用简例 [原创]Cygwin的中文支持(解决乱码) [原创]程序员日志:自制随笔程序 [原创]程序员日志:程序员的快捷方式 [原创]Java Socket(套接字)自学笔记1 [原创]透明的DIV提示框(兼容IE与Firefox) 深入HashCode方法 JavaScript代码实例:拖动对象 Drag Object (兼容:IE、Firefox、Opera ... ) Tomcat中用web.xml控制Web应用详解(2) Tomcat中用web.xml控制Web应用详解(1) java.sql.ResultSet 新增、更新、刪除資料 [原创]Object.clone()的作用与用法 Java 范型编程 Singleton模式(单态模式) Builder模式
[原创]一个简单例子解释 Java 工厂模式
Star.Stroll · 2008-01-31 · via 博客园 - Star.Stroll

    其实工厂模式结构并不复杂,其目的只有一个就是解耦.废话少说看例子吧.这个HelloWorld就足够说明工厂模式在Java语言里的实现方法.

/**
 * IOC模式简单实例
 
*/


/**
 * 运行类
 
*/

public class MainClass {
    
/**
     * 主函数
     
*/

    
public static void main(String[] args) {
        
try {
            PrinterFactory.createPrinter().printByString(
"Hello World~!");
        }
 catch (Exception ex) {
            System.out.println(ex.toString());
        }

    }

}


/**
 * Printer接口
 
*/

interface IF_Printer {
    
/**
     * 接口printByString方法声明
     
*/

    
public void printByString(String str);
}


/**
 * MyPrinter实现Printer接口
 
*/

class MyPrinter implements IF_Printer {
    
public void printByString(String str) {
        System.out.println(str);
    }

}


/**
 * IF_Printer对象工厂,用于创建实现接口的类对象
 
*/

class PrinterFactory {
    
/**
     * 工厂方法,返回IF_Printer接口实例
     
*/

    
public static IF_Printer createPrinter() throws InstantiationException,
            ClassNotFoundException, IllegalAccessException 
{
        String str 
= "MyPrinter";//通过字符串寻找实现接口的类,字符串可从文件中读取获得,从而实现IOC模式
        return (IF_Printer) Class.forName(str).newInstance();//返回IF_Printer接口实例
    }

}

另外这里还有一个更为完整的例子,可用Eclipse打开
https://files.cnblogs.com/starstroll/ModeTrain2.rar