package factory;
public class FactoryMethod {
public static void main(String[] args) {
Application application = new Application();
ProductA productA=application.getObject();
productA.method1();
}
}
class ProductA{
public void method1(){
System.out.println("ProductA.method1 executed");
}
}
class Application{
private ProductA createProduct(){
return new ProductA();
}
ProductA getObject(){
ProductA productA = createProduct();
return productA;
}
}
package factory;
public class FactoryMethod {
public static void main(String[] args) {
Application application = new ConcreateProduct1();
Product product =application.getObject();
product.method1();
}
}
interface Product{
public void method1();
}
class ProductA implements Product{
@Override
public void method1(){
System.out.println("ProductA.method1 executed");
}
}
class Product1 implements Product{
@Override
public void method1(){
System.out.println("Product1.method1 executed");
}
}
class SimpleFactory{
public static Product createProduct(String type){
if(type.equals("0")){
return new ProductA();
}else if(type.equals("1")){
return new Product1();
}else{
return null;
}
}
}
abstract class Application{
abstract Product createProduct( ) ;
Product getObject(){
Product product = createProduct();
return product;
}
}
class ConcreateProductA extends Application{
@Override
Product createProduct( ) {
return new ProductA();
}
}
class ConcreateProduct1 extends Application{
@Override
Product createProduct( ) {
return new Product1();
}
}
应用场景
1.当你不知道该使用对象的确切类型的时候
2.当你希望为库或框架提供扩展其内部组件的方法时
主要优点:
1.将具体产品和创建者解耦
2.符合单一职责原则
3.符合开闭原则
源码中的应用:
Java api
//静态工厂方法
Calendar.getInstance()
java.text.NumberFormat.getInstance()
java.util.ResourceBundle.getBundle()
//工厂方法
java.net.URLStreamHandlerFactory
javax.xml.bind.JAXBContext.createMarshaller
抽象工厂模式
package factory;
public class AbstractFactoryTest {
public static void main(String[] args) {
IDatabasedUtils iDatabasedUtils = new MyOracleBaseUtils();
IConnection connection=iDatabasedUtils.getConnection();
connection.connect();
ICommand command =iDatabasedUtils.getCommand();
command.command();
}
}
// 变化:mysql oracle
// connection , command,
interface IConnection{
void connect();
}
class MySqlConnection implements IConnection{
@Override
public void connect() {
System.out.println("mysql connected.");
}
}
class MyOracleConnection implements IConnection{
@Override
public void connect() {
System.out.println("myoracle connected.");
}
}
interface ICommand{
void command();
}
class MysqlCommand implements ICommand{
@Override
public void command() {
System.out.println("mysql command.");
}
}
class MyOracleCommand implements ICommand{
@Override
public void command() {
System.out.println("myOracle command.");
}
}
interface IDatabasedUtils{
IConnection getConnection();
ICommand getCommand();
}
class MysqlDataBaseUtils implements IDatabasedUtils{
@Override
public IConnection getConnection() {
return new MySqlConnection();
}
@Override
public ICommand getCommand() {
return new MysqlCommand();
}
}
class MyOracleBaseUtils implements IDatabasedUtils
{
@Override
public IConnection getConnection() {
return new MyOracleConnection();
}
@Override
public ICommand getCommand() {
return new MyOracleCommand();
}
}
模式定义:
提供一个创建一系列相关或互相依赖对象的接口,而无需制定他们具体的类
优点:
1.可以确信你从工厂得到的产品彼此是兼容的
2.可以避免具体产品和客户端代码之间的紧密耦合
3.符合单一职责原则
4.符合开闭原则
JDK源码中的应用
1.java.sql.connection
2.java.sql.Driver