大话设计模式_抽象工厂模式

本文通过实例展示了如何利用抽象工厂模式和反射技术在不同的数据库系统间进行灵活的操作,具体包括用户和部门的增删查操作在SQL Server与Access数据库之间的实现。

以一种方法的两种数据库实现为例子.


第一种:抽象工厂

package com.wzs.three;

/**
 * 大话设计模式--page128 抽象工厂
 * 
 * @author Administrator
 * 
 */
public class AbstractFactory {
	public static void main(String[] args) {
		// IFactory factory = new SqlServerFactory();
		IFactory factory = new AccessFactory();

		User user = new User();
		IUser iu = factory.createUser();
		iu.insert(user);
		iu.getUser(1);

		Department department = new Department();
		IDepartment id = factory.createDepartment();
		id.insert(department);
		id.getDepartment(1);

	}
}

/*
 * 用户
 */
class User {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

/*
 * 部门
 */
class Department {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

/*
 * 抽象工厂
 */
interface IFactory {
	IUser createUser();

	IDepartment createDepartment();
}

/*
 * sql 工厂
 */
class SqlServerFactory implements IFactory {
	@Override
	public IUser createUser() {
		return new SqlServerUser();
	}

	@Override
	public IDepartment createDepartment() {
		return new SqlServerDepartment();
	}
}

/*
 * access 工厂
 */
class AccessFactory implements IFactory {
	@Override
	public IUser createUser() {
		return new AccessUser();
	}

	@Override
	public IDepartment createDepartment() {
		return new AccessDepartment();
	}

}

/*
 * 用户方法抽象接口
 */
interface IUser {
	void insert(User user);

	User getUser(int id);
}

/*
 * sql用户方法
 */
class SqlServerUser implements IUser {

	@Override
	public User getUser(int id) {
		System.out.println("在sql server中得到id=" + id + "的User记录.");
		return null;
	}

	@Override
	public void insert(User user) {
		System.out.println("在sql server中插入User新记录.");
	}

}

/*
 * access用户方法
 */
class AccessUser implements IUser {

	@Override
	public User getUser(int id) {
		System.out.println("在Access中得到id=" + id + "的User记录.");
		return null;
	}

	@Override
	public void insert(User user) {
		System.out.println("在Access中插入新User记录.");
	}

}

/*
 * 部门抽象方法接口
 */
interface IDepartment {
	void insert(Department department);

	Department getDepartment(int id);
}

/*
 * sql部门方法
 */
class SqlServerDepartment implements IDepartment {

	@Override
	public Department getDepartment(int id) {
		System.out.println("在sql server中得到id=" + id + "的Department记录.");
		return null;
	}

	@Override
	public void insert(Department department) {
		System.out.println("在sql server中插入新Department记录.");
	}
}

/*
 * access部门方法
 */
class AccessDepartment implements IDepartment {

	@Override
	public Department getDepartment(int id) {
		System.out.println("在Access中得到id=" + id + "的Department记录.");
		return null;
	}

	@Override
	public void insert(Department department) {
		System.out.println("在Access中插入新Department记录.");
	}

}


第二种:反射+抽象工厂

package com.wzs.two;

/**
 * 大话设计模式--page128 反射+抽象工厂
 * 
 * @author Administrator
 * 
 */
public class AbstractFactory {
	public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		// IFactory factory = new SqlServerFactory();

		User user = new User();
		IUser iu = DataAccess.createUser();
		iu.insert(user);
		iu.getUser(1);

		Department department = new Department();
		IDepartment id = DataAccess.createDepartment();
		id.insert(department);
		id.getDepartment(1);

	}
}

class DataAccess {
	private static String db = "Access";

	public static IUser createUser() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		return (IUser) Class.forName("com.wzs.two." + db + "User").newInstance();
	}

	public static IDepartment createDepartment() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		return (IDepartment) Class.forName("com.wzs.two." + db + "Department").newInstance();
	}
}

class DataSqlServer {
	private static String db = "SqlServer";

	public static IUser createUser() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		return (IUser) Class.forName("com.wzs.two." + db + "User").newInstance();
	}

	public static IDepartment createDepartment() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		return (IDepartment) Class.forName("com.wzs.two." + db + "Department").newInstance();
	}
}

/*
 * 用户
 */
class User {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

/*
 * 部门
 */
class Department {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

/*
 * 用户方法抽象接口
 */
interface IUser {
	void insert(User user);

	User getUser(int id);
}

/*
 * sql用户方法
 */
class SqlServerUser implements IUser {

	@Override
	public User getUser(int id) {
		System.out.println("在sql server中得到id=" + id + "的User记录.");
		return null;
	}

	@Override
	public void insert(User user) {
		System.out.println("在sql server中插入User新记录.");
	}

}

/*
 * access用户方法
 */
class AccessUser implements IUser {

	@Override
	public User getUser(int id) {
		System.out.println("在Access中得到id=" + id + "的User记录.");
		return null;
	}

	@Override
	public void insert(User user) {
		System.out.println("在Access中插入新User记录.");
	}

}

/*
 * 部门抽象方法接口
 */
interface IDepartment {
	void insert(Department department);

	Department getDepartment(int id);
}

/*
 * sql部门方法
 */
class SqlServerDepartment implements IDepartment {

	@Override
	public Department getDepartment(int id) {
		System.out.println("在sql server中得到id=" + id + "的Department记录.");
		return null;
	}

	@Override
	public void insert(Department department) {
		System.out.println("在sql server中插入新Department记录.");
	}
}

/*
 * access部门方法
 */
class AccessDepartment implements IDepartment {

	@Override
	public Department getDepartment(int id) {
		System.out.println("在Access中得到id=" + id + "的Department记录.");
		return null;
	}

	@Override
	public void insert(Department department) {
		System.out.println("在Access中插入新Department记录.");
	}

}


考虑柔性负荷的综合能源系统低碳经济优化调度【考虑碳交易机制】(Matlab代码实现)内容概要:本文围绕“考虑柔性负荷的综合能源系统低碳经济优化调度”展开,重点研究在碳交易机制下如何实现综合能源系统的低碳化与经济性协同优化。通过构建包含风电、光伏、储能、柔性负荷等多种能源形式的系统模型,结合碳交易成本与能源调度成本,提出优化调度策略,以降低碳排放并提升系统运行经济性。文中采用Matlab进行仿真代码实现,验证了所提模型在平衡能源供需、平抑可再生能源波动、引导柔性负荷参与调度等方面的有效性,为低碳能源系统的设计与运行提供了技术支撑。; 适合人群:具备一定电力系统、能源系统背景,熟悉Matlab编程,从事能源优化、低碳调度、综合能源系统等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①研究碳交易机制对综合能源系统调度决策的影响;②实现柔性负荷在削峰填谷、促进可再生能源消纳中的作用;③掌握基于Matlab的能源系统建模与优化求解方法;④为实际综合能源项目提供低碳经济调度方案参考。; 阅读建议:建议读者结合Matlab代码深入理解模型构建与求解过程,重点关注目标函数设计、约束条件设置及碳交易成本的量化方式,可进一步扩展至多能互补、需求响应等场景进行二次开发与仿真验证。
简单工厂模式(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中的应用。如果有任何进一步的问题,请随时提问!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值