抽象工厂模式
来源:互联网 发布:mac上微信接收的文件 编辑:程序博客网 时间:2024/05/17 04:16
抽象工厂模式(别名:配套)
提供一个创建一系列或相互依赖对象的接口,而无需指定它们具体的类。
一 、 概述
当系统准备为用户提供一系列相关的对象,又不想让用户代码和创建这些对象的类形成耦合时,就可以使用抽象工厂方法模式来设计系统。抽象工厂模式的关键是在一个抽象类或接口中定义若干个抽象方法,这些抽象方法分别返回某个类的实例,该抽象类或接口让其子类或实现该接口的类重写这些抽象方法为用户提供一系列相关的对象。
二、抽象工厂模式的结构与使用
模式的结构中包括四种角色:
1.抽象产品(Product):
UpperClothes.java
public abstract class UpperClothes{
public abstract intgetChestSize();
public abstract intgetHeight();
public abstract String getName();
}
Trousers.java
public abstract class Trousers{
public abstract intgetWaistSize();
public abstract intgetHeight();
public abstract String getName();
}
2.具体产品(ConcreteProduct)_1:WesternUpperClothes.java
public class WesternUpperClothes extendsUpperClothes{
private intchestSize;
private int height;
private String name;
WesternUpperClothes(Stringname,intchestSize,int height){
this.name=name;
this.chestSize=chestSize;
this.height=height;
}
public intgetChestSize(){
return chestSize;
}
public intgetHeight(){
return height;
}
public String getName(){
return name;
}
}
2.具体产品(ConcreteProduct)_2:CowboyUpperClothes.java
public class CowboyUpperClothes extendsUpperClothes{
private intchestSize;
private int height;
private String name;
CowboyUpperClothes(Stringname,intchestSize,int height){
this.name=name;
this.chestSize=chestSize;
this.height=height;
}
public intgetChestSize(){
return chestSize;
}
public intgetHeight(){
return height;
}
public String getName(){
return name;
}
}
2.具体产品(ConcreteProduct)_3:WesternTrousers.java
public class WesternTrousers extends Trousers{
private intwaistSize;
private int height;
private String name;
WesternTrousers(Stringname,intwaistSize,int height){
this.name=name;
this.waistSize=waistSize;
this.height=height;
}
public intgetWaistSize(){
return waistSize;
}
public intgetHeight(){
return height;
}
public String getName(){
return name;
}
}
2.具体产品(ConcreteProduct)_4:CowboyTrousers.java
public class CowboyTrousers extends Trousers{
private intwaistSize;
private int height;
private String name;
CowboyTrousers(Stringname,intwaistSize,int height){
this.name=name;
this.waistSize=waistSize;
this.height=height;
}
public intgetWaistSize(){
return waistSize;
}
public intgetHeight(){
return height;
}
public String getName(){
return name;
}
}
3.抽象工厂(AbstractFactory):ClothesFactory.java
public abstractclass ClothesFactory{
public abstract UpperClothescreateUpperClothes(intchestSize,int height);
public abstract Trousers createTrousers(intwaistSize,int height);
}
4.具体工厂(ConcreteFactory):
BeijingClothesFactory.java
public class BeijingClothesFactory extendsClothesFactory {
public UpperClothescreateUpperClothes(intchestSize,int height){
return new WesternUpperClothes("北京牌西服上衣",chestSize,height);
}
public Trousers createTrousers(intwaistSize,int height){
return new WesternTrousers("北京牌西服裤子",waistSize,height);
}
}
ShanghaiClothesFactory.java
public class ShanghaiClothesFactory extendsClothesFactory {
public UpperClothescreateUpperClothes(intchestSize,int height){
return new WesternUpperClothes("上海牌牛仔上衣",chestSize,height);
}
public Trousers createTrousers(intwaistSize,int height){
return new WesternTrousers("上海牌牛仔裤",waistSize,height);
}
}
5.应用_1:Shop.java
public class Shop{
UpperClothes cloth;
Trousers trouser;
public void giveSuit(ClothesFactoryfactory,intchestSize,intwaistSize,int height){
cloth=factory.createUpperClothes(chestSize,height);
trouser=factory.createTrousers(waistSize,height);
showMess();
}
private void showMess(){
System.out.println("<套装信息>");
System.out.println(cloth.getName()+":");
System.out.print("胸围:"+cloth.getChestSize());
System.out.println("身高:"+cloth.getHeight());
System.out.println(trouser.getName()+":");
System.out.print("腰围:"+trouser.getWaistSize());
System.out.println("身高:"+trouser.getHeight());
}
}
5.应用_2:Application.java
public classApplication{
public static void main(String args[]){
Shop shop=new Shop();
ClothesFactory factory=newBeijingClothesFactory();
shop.giveSuit(factory,110,82,170);
factory=new ShanghaiClothesFactory();
shop.giveSuit(factory,120,88,180);
}
}
三、抽象工厂模式的优点
- 工厂模式 -- 抽象工厂
- 工厂模式-抽象工厂
- 工厂模式-抽象工厂
- 工厂模式/抽象工厂模式
- 工厂模式&&抽象工厂模式
- 工厂模式---抽象工厂模式
- 工厂模式&抽象工厂模式
- 工厂模式,抽象工厂模式
- 【模式】抽象工厂模式
- 简单工厂、工厂模式、抽象工厂模式
- 简单工厂、工厂模式、抽象工厂模式
- 简单工厂&工厂模式&抽象工厂模式
- 工厂方法,抽象工厂模式
- 2,工厂模式--抽象工厂
- C#工厂模式-抽象工厂
- 工厂模式与抽象工厂
- 抽象工厂设计模式
- 抽象工厂模式
- 负载均衡技术:L4-F7 SWITCH
- Windows Embedded Compact 7中的dll编程(上)
- new/delete和malloc/free
- EMD
- php post json数据遇“&”字符被截断的解决方案
- 抽象工厂模式
- AVFoundation总结
- 常用的工业传感器
- 重载(Overload)和重写(Override)的区别
- spoj 345
- ndk中输出log
- CMap用法的精辟解释 .
- 分类器是如何做检测的?——CascadeClassifier中的detectMultiScale函数解读
- OUTLOOK邮箱使用总结