学习设计模式(二):工厂模式

版权声明:个人博客:https://blog.csdn.net/qq_22754377 转载请注明出处 https://blog.csdn.net/qq_22754377/article/details/82118316

1.工厂模式介绍与设计思路:

维基百科定义:工厂方法模式(英语:Factory method pattern)是一种实现了“工厂”概念的面向对象设计模式。就像其他创建型模式一样,它也是处理在不指定对象具体类型的情况下创建对象的问题。工厂方法模式的实质是“定义一个创建对象的接口,但让实现这个接口的类来决定实例化哪个类。工厂方法让类的实例化推迟到子类中进行。”

2.三种工厂模式的代码实现

  1. 简单工厂模式
/**
* @Description: 衣服抽象接口
* @Param:
* @return:
* @Author: Wtk
* @Date: 2018/8/26
*/
public interface Clothes {
    void print();
}

下面是具体实现衣服接口的三种不同的具体衣服

/**
 * @program: DesignPatterns
 * @description:
 * @author: Mr.Wang
 * @create: 2018-08-26
 **/
public class CoatClothes implements Clothes {
    @Override
    public void print() {
        System.out.println("this is coat");
    }
}
/**
 * @program: DesignPatterns
 * @description:
 * @author: Mr.Wang
 * @create: 2018-08-26
 **/
public class SweaterClothes implements Clothes {
    @Override
    public void print() {
        System.out.println("this is sweater");
    }
}
/**
 * @program: DesignPatterns
 * @description:
 * @author: Mr.Wang
 * @create: 2018-08-26
 **/
public class TshirtClothes implements Clothes {
    @Override
    public void print() {
        System.out.println("this is T-shirt");
    }
}

衣服工厂,根据输入的参数,实例化某一个衣服类

/**
 * @program: DesignPatterns
 * @description: 衣服工厂,决定实例化哪个工厂
 * @author: Mr.Wang
 * @create: 2018-08-26
 **/
public class ClothesFactory {
    private Clothes clothes;
    public Clothes getClothesFactory(String text){
        if(text.equals("T-shirt")){
            TshirtClothes tshirtClothes = new TshirtClothes();
            return tshirtClothes;
        }
        else if(text.equals("sweater")){
            SweaterClothes sweaterClothes = new SweaterClothes();
            return sweaterClothes;
        }
        else if(text.equals("coat")){
            CoatClothes coatClothes = new CoatClothes();
            return coatClothes;
        }
        else {
            System.out.println("no this type");
            return null;
        }

    }

}
/**
  @program: DesignPatterns
  @description: 简单工厂测试
  @author: Mr.Wang
  @create: 2018-08-26
 **/
public class SimpleFactoryMain {
    public static  void main(String[] args){
        ClothesFactory clothesFactory = new ClothesFactory();
        Clothes clothes =clothesFactory.getClothesFactory("coat1");
        clothes.print();

    }
}

2.工厂模式

/**
 * 衣服抽象类
 * @author wtk
 */
public interface Clothes {
    void print();
}

具体衣服实现类

/**
 * @program: DesignPatterns
 * @description:
 * @author: Mr.Wang
 * @create: 2018-08-26
 **/
public class CoatClothes implements Clothes {
    @Override
    public void print() {
        System.out.println("this is coat");
    }
}
/**
 * @program: DesignPatterns
 * @description:
 * @author: Mr.Wang
 * @create: 2018-08-26
 **/
public class SweaterClothes implements Clothes {
    @Override
    public void print() {
        System.out.println("this is sweater");
    }
}
/**
 * @program: DesignPatterns
 * @description:
 * @author: Mr.Wang
 * @create: 2018-08-26
 **/
public class TshirtClothes implements Clothes {
    @Override
    public void print() {
        System.out.println("this is T-shirt");
    }
}

抽象工厂

/**
 * 抽象工厂类
 * @author wtk
 */
public interface ClothesFactory {
    Clothes createFactory();
}

具体工厂实现类

/**
 * @program: DesignPatterns
 * @description:
 * @author: Mr.Wang
 * @create: 2018-08-26
 **/
public class CoatFactory implements ClothesFactory {
    @Override
    public Clothes createFactory() {
        return new CoatClothes();
    }
}
/**
 * @program: DesignPatterns
 * @description:
 * @author: Mr.Wang
 * @create: 2018-08-26
 **/
public class SweaterFactory implements ClothesFactory {
    @Override
    public Clothes createFactory() {
        return new SweaterClothes();
    }
}
/**
 * @program: DesignPatterns
 * @description:
 * @author: Mr.Wang
 * @create: 2018-08-26
 **/
public class TshirtFactory implements ClothesFactory {
    @Override
    public Clothes createFactory() {
        return new TshirtClothes();
    }
}

测试代码

/**
 * @program: DesignPatterns
 * @description: 工厂模式测试
 * @author: Mr.Wang
 * @create: 2018-08-26
 **/
public class FactoryMain {
    public static void main(String[] args){
        CoatFactory coatFactory =new CoatFactory();
        Clothes clothes =coatFactory.createFactory();
        clothes.print();

    }

}

源码地址:https://github.com/wangtengke/DesignPatterns
个人博客:https://blog.csdn.net/qq_22754377

猜你喜欢

转载自blog.csdn.net/qq_22754377/article/details/82118316
今日推荐