设计模式整理(一) 之 抽象工厂

类别:Java 点击:0 评论:0 推荐:

一、结构图
        
二、适用性
        1、一个应用独立于一产品创建、组合的时候;
        2、一个应用要用多个产品中某一产品来进行配置的时候;
        3、当你要强调一系列相关的产品对象的设计以便进行联合使用的时候;
        4、当你提供一个产品类库,而只想显示它们的接口而不是实现的时候。

三、目的
        提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
四、个人感受
        抽象工厂模式应该多用于组合,当用不同对象构建另一对象的时候可以把这些全部抽象出来,以便于更多样化的组合;这样保证生成对象的多样性,程序的扩展性增强;此外、可以把一些方法的实现封装起来,形成接口,通过配置或者其他方式,来进行接口的实现。等等。
五、java实现
1、RoomMaker类
public class RoomMaker  {


    public Room CreateRoom(String roomType) {
        if(roomType.equals("LivingRoom")) {
            return new LivingRoom();
        } else if(roomType.equals("BedRoom")) {
            return new BedRoom();
        } else {
            return new LivingRoom();
        }
    }

    public static void main(String[] args) {
        RoomMaker myMaker = new RoomMaker();
        //-----  Create Living Room
        Room myLivingRoom = myMaker.CreateRoom("LivingRoom");
        //-----  Create a door in living room
        Door livingDoor = myLivingRoom.makeDoor();
        System.out.println("Living room door name is:" + livingDoor.getName() );
        //-----  Create a wall in living room
        Wall livingWall = myLivingRoom.makeWall();
        System.out.println("Living room wall name is:" + livingWall.getName() );

        //-----  Create Bed Room
        Room myBedRoom = myMaker.CreateRoom("BedRoom");
        //-----  Create a door in bedroom
        Door BedDoor = myBedRoom.makeDoor();
        System.out.println("Bed room door name is:" + BedDoor.getName() );
        //-----  Create a wall in bedroom
        Wall BedWall = myBedRoom.makeWall();
        System.out.println("Bed room wall name is:" + BedWall.getName() );

    }
   
}
2、Room类
public abstract  class Room  {
    public abstract Wall makeWall();
    public abstract Door makeDoor();
}
3、Wall类
public abstract class Wall  {
    //private Wall wall;
    public abstract String getName();
}
4、Door类
public abstract class Door  {
    //private Door door;
    public abstract String getName() ;
}
5、BedRoom类
public class BedRoom  extends Room {
    public BedRoom() {
        System.out.println("Initiated a bedroom !");
    }
    public Door makeDoor() {
        return new BedRoomDoor();
    }
    public Wall makeWall() {
        return new BedRoomWall();
    }
}
6、LivingRoom类
public class LivingRoom  extends Room {
    public LivingRoom() {
        System.out.println("Initiated a living room !");
    }
    public Door makeDoor() {
        return new LivingRoomDoor();
    }
    public Wall makeWall() {
        return new LivingRoomWall();
    }
}
7、BedRoomDoor类
public class BedRoomDoor extends Door {
    private String doorName;
    public BedRoomDoor() {
        doorName = "BedRoomDoor";
    }
    public String getName() {
        return doorName;
    }
}
8、BedRoomWall类
public class BedRoomWall extends Wall {
    private String wallName;
    public BedRoomWall() {
        wallName = "BedRoomWall";
    }
    public String getName() {
        return wallName;
    }
}
9、LivingRoomDoor类
public class LivingRoomDoor extends Door {
    private String doorName;
    public LivingRoomDoor() {
        doorName = "LivingRoomDoor";
    }
    public String getName() {
        return doorName;
    }
}
10、LivingRoomWall类
public class LivingRoomWall  extends Wall {
    private String wallName;
    public LivingRoomWall() {
        wallName = "LivingRoomWall";
    }
    public String getName() {
        return wallName;
    }
}

本文地址:http://com.8s8s.com/it/it12780.htm