大话设计模式 抽象工厂

//源代码地址:http://download.csdn.net/detail/a17909863341/5321145
package org.ustc.abstractfactory;

public class DataAccess {
	public static final String db = "org.ustc.abstractfactory.Access";
	public static final String USER = "User";
	public static final String DEPARTMENT = "Department";
	public static UserInterface createUser(){
		UserInterface user = null;
		try {
			user = (UserInterface) Class.forName(db+USER).newInstance();//反射
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
	public static DepartmentInterface createDepart(){
		DepartmentInterface depart = null;
		try {
			depart = (DepartmentInterface) Class.forName(db+DEPARTMENT).newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return depart;
	}
	
}



package org.ustc.abstractfactory;

public class SimpleFactory {//简单工厂
	public static String USER = "sql";
	public static UserInterface createUser(){
		UserInterface user = null;
		switch(USER.charAt(0)){
		case 's':
			user = new SqlUser();
			break;
		case 'a':
			user = new AccessUser();
			break;
		}
		return user;
	}
	public static DepartmentInterface createDepartment(){
		DepartmentInterface depart = null;
		switch(USER.charAt(0)){
		case 's':
			depart = new SqlDepartment();
			break;
		case 'a':
			depart = new AccessDepartment();
			break;
		}
		return depart;
	}
}



package org.ustc.abstractfactory;

public interface AbstractFactory {//抽象工厂
	public UserInterface createUser();
	public DepartmentInterface createDepartment();
}
package org.ustc.abstractfactory;

public class AccessFactory implements AbstractFactory{

	@Override
	public UserInterface createUser() {
		// TODO Auto-generated method stub
		return new AccessUser();
	}

	@Override
	public DepartmentInterface createDepartment() {
		// TODO Auto-generated method stub
		return new AccessDepartment();
	}
	
}



package org.ustc.abstractfactory;

public class SqlFactory implements AbstractFactory {

	@Override
	public UserInterface createUser() {
		// TODO Auto-generated method stub
		return new SqlUser();
	}

	@Override
	public DepartmentInterface createDepartment() {
		// TODO Auto-generated method stub
		return new SqlDepartment();
	}

}



package org.ustc.abstractfactory;

public interface DepartmentInterface {
	public void getDepartmentById(int id) ;
	public void InsertDepartment(Department dept);
}

package org.ustc.abstractfactory;

public interface UserInterface {
	public void getUserById(int id) ;
	public void InsertUser(User user);
}

简单工厂代码结构图











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
简单工厂模式(Simple Factory Pattern)是一种创建型设计模式,它通过一个工厂类来创建不同类型的对象,而无需暴露对象创建的逻辑给客户端。在Python中,简单工厂模式可以通过一个工厂类来创建不同的产品对象。下面是一个简单的示例: ```python class Product: def operation(self): pass class ConcreteProductA(Product): def operation(self): print("Performing operation A.") class ConcreteProductB(Product): def operation(self): print("Performing operation B.") class SimpleFactory: @staticmethod def create_product(product_type): if product_type == "A": return ConcreteProductA() elif product_type == "B": return ConcreteProductB() else: raise ValueError("Invalid product type.") # 使用简单工厂创建产品对象 factory = SimpleFactory() product_a = factory.create_product("A") product_a.operation() product_b = factory.create_product("B") product_b.operation() ``` 在上述示例中,`Product` 是一个抽象产品类,`ConcreteProductA` 和 `ConcreteProductB` 是具体产品类。`SimpleFactory` 是工厂类,通过 `create_product` 方法根据不同的产品类型创建相应的产品对象。 通过简单工厂模式,客户端无需知道具体的产品类,只需要通过工厂类来创建产品对象。这样可以降低客户端与具体产品类的耦合度,并且当需要新增产品时,只需要修改工厂类即可。 希望这个简单的示例能帮助你理解简单工厂模式在Python中的应用。如果有任何进一步的问题,请随时提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值