创建型模式之抽象工厂
2010-12-08 21:57
246 查看
设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。 毫无疑问,设计模式于己于他人于系统都是多赢的,设计模式使代码编制真正工程化,设计模式是软件工程的基石,如同大厦的一块块砖石一样。
下面简单介绍一些基本的设计模式:
创建型模式分为AbstractFactory ( 抽象工厂 ) ,FactoryMethod ( 工厂方法 ) ,Singleton ( 单态模式 ) ,Builder ( 建造者模式 ) Prototype ( 原型模式 )。
抽象工厂
概述:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
适用性:
1.一个系统要独立于它的产品的创建、组合和表示时。
2.一个系统要由多个产品系列中的一个来配置时。
3.当你要强调一系列相关的产品对象的设计以便进行联合使用时。
4.当你提供一个产品类库,而只想显示它们的接口而不是实现时。
参与者:
1.AbstractFactory
声明一个创建抽象产品对象的操作接口。
2.ConcreteFactory
实现创建具体产品对象的操作。
3.AbstractProduct
为一类产品对象声明一个接口。
4.ConcreteProduct
定义一个将被相应的具体工厂创建的产品对象。
实现AbstractProduct接口。
5.Client
仅使用由AbstractFactory和AbstractProduct类声明的接口
例子:
AbstractFactory
ConcreteFactory
AbstractProduct
ConcreteProduct
Client
result:
下面简单介绍一些基本的设计模式:
创建型模式分为AbstractFactory ( 抽象工厂 ) ,FactoryMethod ( 工厂方法 ) ,Singleton ( 单态模式 ) ,Builder ( 建造者模式 ) Prototype ( 原型模式 )。
抽象工厂
概述:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
适用性:
1.一个系统要独立于它的产品的创建、组合和表示时。
2.一个系统要由多个产品系列中的一个来配置时。
3.当你要强调一系列相关的产品对象的设计以便进行联合使用时。
4.当你提供一个产品类库,而只想显示它们的接口而不是实现时。
参与者:
1.AbstractFactory
声明一个创建抽象产品对象的操作接口。
2.ConcreteFactory
实现创建具体产品对象的操作。
3.AbstractProduct
为一类产品对象声明一个接口。
4.ConcreteProduct
定义一个将被相应的具体工厂创建的产品对象。
实现AbstractProduct接口。
5.Client
仅使用由AbstractFactory和AbstractProduct类声明的接口
例子:
AbstractFactory
public interface IAnimalFactory { ICat createCat(); IDog createDog(); }
ConcreteFactory
public class BlackAnimalFactory implements IAnimalFactory { public ICat createCat() { return new BlackCat(); } public IDog createDog() { return new BlackDog(); } } public class BlackAnimalFactory implements IAnimalFactory { public ICat createCat() { return new BlackCat(); } public IDog createDog() { return new BlackDog(); } }
AbstractProduct
public interface ICat { void eat(); } public interface IDog { void eat(); }
ConcreteProduct
public class BlackCat implements ICat { public void eat() { System.out.println("The black cat is eating!"); } } public class WhiteCat implements ICat { public void eat() { System.out.println("The white cat is eating!"); } } public class BlackDog implements IDog { public void eat() { System.out.println("The black dog is eating"); } } public class WhiteDog implements IDog { public void eat() { System.out.println("The white dog is eating!"); } }
Client
public static void main(String[] args) { IAnimalFactory blackAnimalFactory = new BlackAnimalFactory(); ICat blackCat = blackAnimalFactory.createCat(); blackCat.eat(); IDog blackDog = blackAnimalFactory.createDog(); blackDog.eat(); IAnimalFactory whiteAnimalFactory = new WhiteAnimalFactory(); ICat whiteCat = whiteAnimalFactory.createCat(); whiteCat.eat(); IDog whiteDog = whiteAnimalFactory.createDog(); whiteDog.eat(); }
result:
The black cat is eating! The black dog is eating! The white cat is eating! The white dog is eating!
相关文章推荐
- 《设计模式 系列》- 创建型模式 - 抽象工厂
- 创建型模式 ABSTRACT FACTORY---抽象工厂
- 设计模式——创建型设计模式总结(简单工厂、普通工厂、抽象工厂、建造者、原型和单例)
- 设计模式笔记--创建型模式之一 抽象工厂
- 创建型模式-抽象工厂
- 二十三种设计模式之创建型模式之抽象工厂
- 创建型模式-抽象工厂
- 抽象工厂——对象创建型模式
- 设计模式-创建型模式-抽象工厂
- 创建型模式 抽象工厂
- 【设计模式】创建型模式-抽象工厂
- 抽象工厂方法模式(创建型)
- 一起来学设计模式-----创建型模式之抽象工厂
- 设计模式--创建型--抽象工厂
- 设计模式——创建型设计模式总结(简单工厂、普通工厂、抽象工厂、建造者、原型和单例)
- ABSTRACT FACTORY抽象工厂 - 对象创建型模式(四)
- 创建型模式-抽象工厂 Creational Patterns -Abstract Factory
- 设计模式-创建型模式:抽象工厂 AbstractFactory
- 设计模式二:abstract factory 抽象工厂——对象创建型模式
- 【转】设计模式——创建型设计模式总结(简单工厂、普通工厂、抽象工厂、建造者、原型和单例)