抽象工厂模式的适用:
一个系统要独立于它的产品的创建、组合和表示时。
一个系统要由多个产品系列中的一个来配置时。
当你要强调一系列相关的产品对象的设计以便进行联合使用时。
当你提供一个产品类库,而只想显示它们的接口而不是实现时。
示例说明:
有三种抽象的产品:墙、门、房间。
对这三种抽象产品有两组具体实现:卧室和起居室。
那么,我们的抽象工厂就可以根据客户的指令(即调用参数)去生产卧室和起居室的房间(墙和门包括在房间里)。
测试启动类:
package com.alex.designpattern.abstractfactory;
/** *//**
* 抽象工厂模式
* <p>
* 提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
*
* @author <a href="
huqimailto:huqiyes@gmail.com">huqi</a>
* @serialData 2007
*/
public class Test ...{
public static void main(String[] args) throws Exception ...{
// ----- Create Living Room
Room myLivingRoom = AbstractRoomFactory.createRoom("LivingRoom");
AbstractRoomFactory.showRoom(myLivingRoom);
// ----- Create Bed Room
Room myBedRoom = AbstractRoomFactory.createRoom("BedRoom");
AbstractRoomFactory.showRoom(myBedRoom);
}
}
抽象产品:
package com.alex.designpattern.abstractfactory;
/** *//**
* The Abstract Class of Wall
*/
public interface Wall ...{
public String getName();
}
package com.alex.designpattern.abstractfactory;
/** *//**
* The Abstract Class of Door
*/
public interface Door ...{
public String getName();
}
package com.alex.designpattern.abstractfactory;
/** *//**
* The Abstract Class of Room
*/
public interface Room ...{
public Wall getWall();
public Door getDoor();
}
实际产品_1
package com.alex.designpattern.abstractfactory;
/** *//**
* A concrete Room - Bed Room
*/
public class BedRoom implements Room ...{
public BedRoom() ...{
System.out.println("Initiated a bedroom.");
}
public Door getDoor() ...{
return new BedRoomDoor();
}
public Wall getWall() ...{
return new BedRoomWall();
}
}
package com.alex.designpattern.abstractfactory;
/** *//**
* A concrete Door for Bed Room
*/
public class BedRoomDoor implements Door ...{
private String doorName;
public BedRoomDoor() ...{
doorName = "BedRoomDoor";
}
public String getName() ...{
return doorName;
}
}
package com.alex.designpattern.abstractfactory;
/** *//**
* A concrete Wall for Bed Room
*/
public class BedRoomWall implements Wall ...{
private String wallName;
public BedRoomWall() ...{
wallName = "BedRoomWall";
}
public String getName() ...{
return wallName;
}
}
实际产品_2
package com.alex.designpattern.abstractfactory;
/** *//**
* A concrete Room - Living Room
*/
public class LivingRoom implements Room ...{
public LivingRoom() ...{
System.out.println("Initiated a livingroom.");
}
public Door getDoor() ...{
return new LivingRoomDoor();
}
public Wall getWall() ...{
return new LivingRoomWall();
}
}
package com.alex.designpattern.abstractfactory;
/** *//**
* A concrete Door for Living Room
*/
public class LivingRoomDoor implements Door ...{
private String doorName;
public LivingRoomDoor() ...{
doorName = "LivingRoomDoor";
}
public String getName() ...{
return doorName;
}
}
package com.alex.designpattern.abstractfactory;
/** *//**
* A concrete Wall for Living Room
*/
public class LivingRoomWall implements Wall ...{
private String wallName;
public LivingRoomWall() ...{
wallName = "LivingRoomWall";
}
public String getName() ...{
return wallName;
}
}
最后,当然就是抽象工厂咯,不过要注意,这个抽象工厂的意思并不是这个“工厂”是抽象的,而是这个“工厂”加工的“产品”是来自于抽象的。
package com.alex.designpattern.abstractfactory;
/** *//**
* A Room Maker to test our simple Room Abstract Factory.
*
* @author <a href="
huqimailto:huqiyes@gmail.com">huqi</a>
* @serialData 2007
*/
public abstract class AbstractRoomFactory ...{
public static Room createRoom(String roomType) throws Exception ...{
if (roomType.equals("LivingRoom")) ...{
return new LivingRoom();
} else if (roomType.equals("BedRoom")) ...{
return new BedRoom();
} else ...{
throw new Exception("No this type room: " + roomType);
}
}
public static void showRoom(Room room) ...{
Door door = room.getDoor();
System.out.println("Room door name is: " + door.getName());
Wall wall = room.getWall();
System.out.println("Room wall name is: " + wall.getName());
}
}
本文转自胡奇 51CTO博客,原文链接:http://blog.51cto.com/huqicto/280900,如需转载请自行联系原作者