标签:https cas 工厂 商务 catch TE 程序员 bst stack
抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式。抽象工厂模式可以向客户端提供一个接口,使客户端在不必指定产品的具体的情况下,创建多个产品族中的产品对象。根据里氏替换原则,任何接受父类型的地方,都应当能够接受子类型。因此,实际上系统所需要的,仅仅是类型与这些抽象产品角色相同的一些实例,而不是这些抽象产品的实例。换言之,也就是这些抽象产品的具体子类的实例。工厂类负责创建抽象产品的具体子类的实例。
抽象工厂:在上一篇中介绍了工厂模式(通过定义一个Clothes接口,具体Suit、DownJackets等去实现Clothes中的方法,然后通过ClothesFactory工厂类去生产制造所需要的服装),而当我们现在发现衣服还有男装、女装之分,这时我们可以采用抽象工厂模式。
1)定义统一服装接口Clothes,定义clothesCategory()、applicableOccasion()、applicableSex()方法;
2)定义抽象服装类(如AbstractSuit、AbstractDownJackets等)实现Clothes接口,在抽象服装类中实现clothesCategory()与applicableOccasion()方法;
3)定义具体适用人群服装类(如MaleSuit、FemaleSuit等)继承抽象服装类(AbstractSuit),实现applicableSex()方法;
注:其中抽象类(第二步中创建的几个抽象类)在抽象工厂模式中是叫做产品等级,六个实现类(第三步创建的几个类)是叫做产品族
1)定义枚举类ClothesEnum(也可以写静态类等),将所有衣服类别罗列出来(包括性别区分);
2)义服装工厂接口ClothesFactory,定义createSuit()、createDownJackets()、createSportwear()方法;
3)定义抽象服装工厂类AbstractClothesFactory实现ClothesFactory,定义createClothes(ClothesEnum clothesEnum)方法,用于产生Clothes实例;
4)定义男性服装工厂类MaleClothesFactory与女性服装工厂类FemaleClothesFactory,继承AbstractClothesFactory,分别实现createSuit()、createDownJackets()、createSportwear()方法;
/**
* @Description: 服装接口
* @Author: qiuxx
* @Date: 12:37 2018/6/18
*/
public interface Clothes {
/**
* 衣服名称
*/
public void clothingCategory();
/**
* 适用场合
*/
public void applicableOccasion();
/**
* 适用性别
*/
public void applicableSex();
}
/**
* @Description: 西装--抽象实现类
* @Author: qiuxx
* @Date: 12:39 2018/6/18
*/
public abstract class AbstractSuit implements Clothes {
@Override
public void clothingCategory() {
System.out.println("类别:西装");
}
@Override
public void applicableOccasion() {
System.out.println("适合商务洽谈、重要会议等");
}
}
/**
* @Description: 羽绒服--抽象实现类
* @Author: qiuxx
* @Date: 12:39 2018/6/18
*/
public abstract class AbstractDownJackets implements Clothes {
@Override
public void clothingCategory() {
System.out.println("类别:羽绒服");
}
@Override
public void applicableOccasion() {
System.out.println("适合寒冷的冬季,保暖、抗寒");
}
}
/**
* @Description: 运动衣--抽象实现类
* @Author: qiuxx
* @Date: 12:39 2018/6/18
*/
public abstract class AbstractSportwear implements Clothes {
@Override
public void clothingCategory() {
System.out.println("类别:运动衣");
}
@Override
public void applicableOccasion() {
System.out.println("适合运动时间穿着");
}
}
/**
* @Description: 男性西装
* @Author: qiuxx
* @Date: 12:43 2018/6/18
*/
public class MaleSuit extends AbstractSuit {
@Override
public void applicableSex() {
System.out.println("适用人群--男性");
}
}
/**
* @Description: 女性西装
* @Author: qiuxx
* @Date: 12:43 2018/6/18
*/
public class FemaleSuit extends AbstractSuit {
@Override
public void applicableSex() {
System.out.println("适用人群--女性");
}
}
/**
* @Description: 男性羽绒服
* @Author: qiuxx
* @Date: 12:43 2018/6/18
*/
public class MaleDownJackets extends AbstractDownJackets {
@Override
public void applicableSex() {
System.out.println("适用人群--男性");
}
}
/**
* @Description: 女性羽绒服
* @Author: qiuxx
* @Date: 12:43 2018/6/18
*/
public class FemaleDownJackets extends AbstractDownJackets {
@Override
public void applicableSex() {
System.out.println("适用人群--女性");
}
}
/**
* @Description: 男性运动衣
* @Author: qiuxx
* @Date: 12:43 2018/6/18
*/
public class MaleSportwear extends AbstractSportwear {
@Override
public void applicableSex() {
System.out.println("适用人群--男性");
}
}
/**
* @Description: 女性运动衣
* @Author: qiuxx
* @Date: 12:43 2018/6/18
*/
public class FemaleSportwear extends AbstractSportwear {
@Override
public void applicableSex() {
System.out.println("适用人群--女性");
}
}
/**
* 衣服枚举类--将所有的衣服类别列举出来
* JDK1.5开始引入enum类型,目的在于吸引程序员转过来
*/
public enum ClothesEnum {
MaleSuit("com.qiuxx.java.base.designpattern.abstractfactorypattern.clothesimpl.sex.MaleSuit"),
FemaleSuit("com.qiuxx.java.base.designpattern.abstractfactorypattern.clothesimpl.sex.FemaleSuit"),
MaleDownJackets("com.qiuxx.java.base.designpattern.abstractfactorypattern.clothesimpl.sex.MaleDownJackets"),
FemaleDownJackets("com.qiuxx.java.base.designpattern.abstractfactorypattern.clothesimpl.sex.FemaleDownJackets"),
MaleSportwear("com.qiuxx.java.base.designpattern.abstractfactorypattern.clothesimpl.sex.MaleSportwear"),
FemaleSportwear("com.qiuxx.java.base.designpattern.abstractfactorypattern.clothesimpl.sex.FemaleSportwear");
private String value = "";
private ClothesEnum(String value){
this.value = value;
}
public String getValue(){
return this.value;
}
}
/**
* @Description: 制衣工厂
* @Author: qiuxx
* @Date: 12:49 2018/6/18
*/
public interface ClothesFactory {
/**
* 制造西装
*/
public Clothes createSuit();
/**
* 制造羽绒服
*/
public Clothes createDownJackets();
/**
* 制造运动衣
*/
public Clothes createSportwear();
}
/**
* @Description: 抽象制衣厂
* @Author: qiuxx
* @Date: 12:55 2018/6/18
*/
public abstract class AbstractClothesFactory implements ClothesFactory {
public Clothes createClothes(ClothesEnum clothesEnum) {
Clothes clothes = null;
//如果传递不是一个Enum中具体的Element的话,则不处理
if (!clothesEnum.getValue().equals("")){
try {
//直接产生一个实例
clothes = (Clothes) Class.forName(clothesEnum.getValue()).newInstance();
} catch (Exception e) {
//以为使用了enum,这种异常请款是不会发生的,除非enum有问题
e.printStackTrace();
}
}
return clothes;
}
}
/**
* @Description: 男性服装制造工厂
* @Author: qiuxx
* @Date: 13:16 2018/6/18
*/
public class MaleClothesFactory extends AbstractClothesFactory {
@Override
public Clothes createSuit() {
return super.createClothes(ClothesEnum.MaleSuit);
}
@Override
public Clothes createDownJackets() {
return super.createClothes(ClothesEnum.MaleDownJackets);
}
@Override
public Clothes createSportwear() {
return super.createClothes(ClothesEnum.MaleSportwear);
}
}
/**
* @Description: 女性服装制造厂
* @Author: qiuxx
* @Date: 13:18 2018/6/18
*/
public class FemaleClothesFactory extends AbstractClothesFactory {
@Override
public Clothes createSuit() {
return super.createClothes(ClothesEnum.FemaleSuit);
}
@Override
public Clothes createDownJackets() {
return super.createClothes(ClothesEnum.FemaleDownJackets);
}
@Override
public Clothes createSportwear() {
return super.createClothes(ClothesEnum.FemaleSportwear);
}
}
/**
* @Description: 调用方(main方法)
* @Author: qiuxx
* @Date: 13:21 2018/6/18
*/
public class Caller {
public static void main(String[] args){
//男性服装工厂
ClothesFactory maleClothesFactory = new MaleClothesFactory();
//女性服装工厂
ClothesFactory femaleClothesFactory = new FemaleClothesFactory();
//制造男性服装
System.out.println("-------------制造男性西装-----------");
Clothes maleSuit = maleClothesFactory.createSuit();
maleSuit.clothingCategory();
maleSuit.applicableOccasion();
maleSuit.applicableSex();
//制造女性服装
System.out.println("-------------制造女性西装-----------");
Clothes femaleSuit = femaleClothesFactory.createSuit();
femaleSuit.clothingCategory();
femaleSuit.applicableOccasion();
femaleSuit.applicableSex();
}
}
博客链接:https://qiuxx404.github.io/2018/06/18/原创/设计模式/设计模式-抽象工厂模式
标签:https cas 工厂 商务 catch TE 程序员 bst stack
原文地址:https://www.cnblogs.com/qiuxx404/p/9195136.html