design pattern——抽象工厂模式

 针对问题:和工厂方法模式类似,用一个继承体系来实现创建对象的多变性。不同的是,抽象工厂模式针对的是产品族(许多个产品).

 

 

 

 

 

抽象工厂模式结构图:

 

 

 

 

 

 

抽象工厂模式实现代码:

/**
 * 产品A接口
 * @author bruce
 *
 */
public abstract class ProductA {
	
	public abstract void descriptionA();
}



/**
 * 产品B接口
 * @author bruce
 *
 */
public abstract class ProductB {
	
	public abstract void descriptionB();
}




/**
 * 创建者接口
 * @author bruce
 *
 */
public abstract class Creator {
	
	public abstract ProductA createProductA();
	
	public abstract ProductB createProductB();
}




/**
 * 产品A1(产品A)
 * @author bruce
 *
 */
public class ProductA1 extends ProductA{

	@Override
	public void descriptionA() {
		// TODO Auto-generated method stub
		System.out.println("ProductA:ProductA1");
	}
}

/**
 * 产品A2(产品A)
 * @author bruce
 *
 */
public class ProductA2 extends ProductA{

	@Override
	public void descriptionA() {
		// TODO Auto-generated method stub
		System.out.println("ProductA:ProductA2");
	}

}



/**
 * 产品B1(产品B)
 * @author bruce
 *
 */
public class ProductB1 extends ProductB{
	
	@Override
	public void descriptionB() {
		// TODO Auto-generated method stub
		System.out.println("ProductB:ProductB1");
	}
}


/**
 * 产品B2(产品B)
 * @author bruce
 *
 */
public class ProductB2 extends ProductB{
	
	@Override
	public void descriptionB() {
		// TODO Auto-generated method stub
		System.out.println("ProductB:ProductB2");
	}
}




/**
 * 创建类1
 * @author bruce
 *
 */
public class ConcreteCreator1 extends Creator{

	@Override
	public ProductA createProductA() {
		// TODO Auto-generated method stub
		return new ProductA1();
	}

	@Override
	public ProductB createProductB() {
		// TODO Auto-generated method stub
		return new ProductB1();
	}

}



/**
 * 创建者2
 * @author bruce
 *
 */
public class ConcreteCreator2 extends Creator{

	@Override
	public ProductA createProductA() {
		// TODO Auto-generated method stub
		return new ProductA2();
	}

	@Override
	public ProductB createProductB() {
		// TODO Auto-generated method stub
		return new ProductB2();
	}

}






/**
 * 测试
 * @author bruce
 */
public class Client {
	
	public static void main(String[] args) {
		Creator creator=new ConcreteCreator1();
		ProductA pa=creator.createProductA();
		ProductB pb=creator.createProductB();
		pa.descriptionA(); //ProductA:ProductA1
		pb.descriptionB(); //ProductB:ProductB1
		
		creator=new ConcreteCreator2();
		pa=creator.createProductA();
		pb=creator.createProductB();
		pa.descriptionA(); //ProductA:ProductA2
		pb.descriptionB(); //ProductB:ProductB2
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all. The examples are chosen so you will be able to absorb the core concepts easily and quickly. This book presents the topic of design patterns in Java in such a way that anyone can grasp the idea. By giving easy to follow examples, you will understand the concepts with increasing depth. The examples presented are straightforward and the topic is presented in a concise manner. Key features of the book: Each of the 23 patterns is described with straightforward Java code. There is no need to know advanced concepts of Java to use this book. Each of the concepts is connected with a real world example and a computer world example. The book uses Eclipse IDE to generate the output because it is the most popular IDE in this field. This is a practitioner's book on design patterns in Java. Design patterns are a popular topic in software development. A design pattern is a common, well-described solution to a common software problem. There is a lot of written material available on design patterns, but scattered and not in one single reference source. Also, many of these examples are unnecessarily big and complex. Table of Contents Chapter 1: Introduction Chapter 2: Observer Patterns Chapter 3: Singleton Patterns Chapter 4: Proxy Patterns Chapter 5: Decorator Patterns Chapter 6: Template Method Patterns Chapter 7: Strategy Patterns (Or, Policy Patterns) Chapter 8: Adapter Patterns Chapter 9: Command Patterns Chapter 10: Iterator Patterns Chapter 11: Facade Patterns Chapter 12: Factory Method Patterns Chapter 13: Memento Patterns Chapter 14: State Patterns Chapter 15: Builder Patterns Chapter 16: Flyweight Patterns Chapter 17: Abstract Factory Patterns Chapter 18: Mediator Patterns Chapter 19: Prototype Patterns Chapter 20: Chain of Responsibility Patterns Chapter 21: Composite Patterns Chapter 22: Bridge Patterns (Or Handle/Body Patterns) Chapter 23: Visitor Patterns Chapter 24: Interpreter Patterns
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值