java设计模式之---------------抽象工厂模式

来源:互联网 发布:java中的compareto 编辑:程序博客网 时间:2024/06/10 05:15

抽象工厂模式是基于工厂模式的扩展,有一个专门的工厂生产类来生产工厂。比如形状类工厂,颜色类工厂。

/***
 * 创建一个形状接口
 * @author long
 *
 */
public interface Shape {
    void draw();
}

/***
 * 创建一个正方形类,实现了Shape接口
 * @author long
 *
 */
public class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Square::draw() method.");
    }
}

/***
 * 创建一个圆形类,实现Shape接口
 * @author long
 *
 */
public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Circle::draw() method.");
    }
}

/***
 * 创建一个矩形类,实现Shape接口
 * @author long
 *
 */
public class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Rectangle::draw() method.");
    }
}


/***
 * 为颜色创建一个接口
 * @author long
 *
 */
public interface Color {
    void fill();
}

/***
 * 创建Blue类,实现Color接口
 * @author long
 *
 */
public class Blue implements Color {
    @Override
    public void fill() {
        System.out.println("Inside Blue::fill() method.");
    }
}

/***
 * 创建Green类,实现了Color接口
 * @author long
 *
 */
public class Green implements Color {
    @Override
    public void fill() {
        System.out.println("Inside Green::fill() method.");
    }
}

/***
 * 创建Red类,实现了Color接口
 * @author long
 *
 */
public class Red implements Color {
    @Override
    public void fill() {
        System.out.println("Inside Red::fill() method.");
    }
}


/***
 * 为Color和Shape对象创建抽象类来获取工厂
 * @author long
 *
 */
public abstract class AbstractFactory {
    abstract Color getColor(String color);
    abstract Shape getShape(String shape);
}

/***
 * 创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。
 * @author long
 *
 */
public class ColorFactory extends AbstractFactory {
    @Override
    public Color getColor(String color) {
        if(color == null){
            return null;
        }
        if(color.equalsIgnoreCase("RED")){
            return new Red();
        }else if(color.equalsIgnoreCase("GREEN")){
            return new Green();
        }else if(color.equalsIgnoreCase("BLUE")){
            return new Blue();
        }
        return null;
    }

    @Override
    Shape getShape(String shape) {
        return null;
    }
}

/***
 * 创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。
 * @author long
 *
 */
public class ShapeFactory extends AbstractFactory{
    @Override
    public Shape getShape(String shapeType) {
        if(shapeType == null){
            return null;
        }
        if(shapeType.equalsIgnoreCase("CIRCLE")){
            return new Circle();
        }else if(shapeType.equalsIgnoreCase("SQUARE")){
            return new Square();
        }else if(shapeType.equalsIgnoreCase("RECTANGLE")){
            return new Rectangle();
        }
        return null;
    }
    @Override
    Color getColor(String color) {
        return null;
    }
}

/***
 * 创建一个工厂生成器,生成具体的工厂
 * @author long
 *
 */
public class FactoryProducer {
    public static AbstractFactory getFactory(String choice){
        if(choice.equalsIgnoreCase("SHAPE")){
            return new ShapeFactory();
        }else if(choice.equalsIgnoreCase("COLOR")){
            return new ColorFactory();
        }
        return null;
    }
}

/***
 * 具体的测试类
 * @author long
 *
 */
public class Test {
    public static void main(String[] args) {
        //获取形状工厂
        AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
        //获取颜色工厂
        AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
        
        //获取形状
        Shape shape1 = shapeFactory.getShape("circle");
        Shape shape2 = shapeFactory.getShape("square");
        Shape shape3 = shapeFactory.getShape("rectangle");
        shape1.draw();
        shape2.draw();
        shape3.draw();
        
        Color red = colorFactory.getColor("red");
        Color blue = colorFactory.getColor("blue");
        Color green = colorFactory.getColor("green");
        red.fill();
        blue.fill();
        green.fill();
    }
}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 牙活动了怎么办还疼 30岁掉了一颗牙怎么办? 在淘宝上交话费交错了怎么办 演出队在小区旁边扰民怎么办 雷雨天加了油怎么办 戴ok镜眼睛重影怎么办 乌龟背上长白色的花纹怎么办? 全自动洗衣机里面掉个硬币怎么办 跆拳道课上孩子乱动说话怎么办? 孩子不愿意上跆拳道课了怎么办 车座位里面倒了汤怎么办 腿被棍子打肿了怎么办 刚买的手机碎屏怎么办 被木棍么么打到头项怎么办 大王卡用到40g怎么办 王卡40g用完了怎么办 父亲把母亲打成重伤怎么办 狗狗脖子摔歪了怎么办 吃鸡游戏中重伤怎么办 吃鸡游戏摔伤了怎么办 现在没能力偿还网贷怎么办 上海找夜场工作被骗了怎么办? 买了烂尾的楼盘怎么办 孕七个月蛀牙疼怎么办 宝宝只吃一边奶怎么办 实房付了定金后悔了怎么办 开车时遇到意外事故时怎么办 没干过影楼门市怎么办 uplay下载游戏速度0k怎么办 缓刑期间被别人打成轻伤怎么办 缓刑期间被别人打了怎么办 有缓刑被别人打怎么办 检察院不予立案怎么办公安局取保 监外执行过了又犯罪怎么办 监视居住期间再次犯罪怎么办 前妻有贷款跑了怎么办 未成年犯有缓刑罪继续犯罪怎么办 英朗出现检修esc怎么办 照相时奖状拿反了怎么办 wps文档加密后忘记密码怎么办 wps表格文件密码忘记了怎么办