标签:abs running static src color code lsh height ret
提供了一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
服装类产品
/** * 服装产品 */ public interface Clothes { void printUsage(); } /** * 夹克 */ public class JackClothes implements Clothes { @Override public void printUsage() { System.out.println("我是夹克衫,穿我很潮"); } } /** * T恤 */ public class TShirtClothes implements Clothes { @Override public void printUsage() { System.out.println("我是T恤,随意"); } }
鞋类产品
/** * 鞋类产品 */ public interface Shoe { void describeSelf(); } /** * 跑鞋(具体的鞋类产品) */ public class RunningShoe implements Shoe { @Override public void describeSelf() { System.out.println("我是跑鞋,我为自己带盐"); } } /** * 篮球鞋(具体的鞋类产品) */ public class BasketballShoe implements Shoe { @Override public void describeSelf() { System.out.println("我是篮球鞋,穿我打篮球吊到爆"); } }
工厂
/** * 抽象工厂 */ public interface Factory { /** * 既生产鞋类产品 */ Shoe produceShoe(BRAND brand); /** * 又能生产服装类商品 */ Clothes produceClothes(BRAND brand); } /** * 李宁工厂,生产鞋系列产品,也生产服装系列产品 */ public class LiningFactory implements Factory { /** * 生产鞋系列产品 */ @Override public Shoe produceShoe(BRAND brand) { if (BRAND.BASKETBALL_SHOE == brand) { System.out.println("---李宁工厂生产篮球鞋---"); return new BasketballShoe(); } if (BRAND.RUNNING_SHOE == brand) { System.out.println("---李宁工厂生产跑鞋---"); return new RunningShoe(); } return null; } /** * 生产服装系列产品 */ @Override public Clothes produceClothes(BRAND brand) { if (BRAND.JACK_CLOTHES == brand) { System.out.println("---李宁工厂生产夹克---"); return new JackClothes(); } if (BRAND.TSHIRT_CLOTHES == brand) { System.out.println("---李宁工厂生产T恤---"); return new TShirtClothes(); } return null; } } /** * Nike工厂,生产鞋系列产品,也生产服装系列产品 */ public class NikeFactory implements Factory { /** * 生产鞋子系列产品 */ @Override public Shoe produceShoe(BRAND brand) { if (BRAND.RUNNING_SHOE == brand) { System.out.println("---Nike工厂生产篮球鞋---"); return new RunningShoe(); } if (BRAND.BASKETBALL_SHOE == brand) { System.out.println("---Nike工厂生产跑鞋---"); return new BasketballShoe(); } return null; } /** * 生产服装系列产品 */ @Override public Clothes produceClothes(BRAND brand) { if (BRAND.JACK_CLOTHES == brand) { System.out.println("---Nike工厂生产夹克---"); return new JackClothes(); } if (BRAND.TSHIRT_CLOTHES == brand) { System.out.println("---Nike工厂生产T恤---"); return new TShirtClothes(); } return null; } }
客户端代码
public class Client { public static void main(String[] args) { // Nike工厂,生产鞋系列产品(篮球鞋,跑鞋)和服装系列产品(夹克,T恤) Factory nikeFactory = new NikeFactory(); // 生产鞋系列产品 Shoe nikeBasketballShoe = nikeFactory.produceShoe(BRAND.BASKETBALL_SHOE);// 篮球鞋 Shoe nikeRunningshoe = nikeFactory.produceShoe(BRAND.RUNNING_SHOE);// 跑鞋 nikeBasketballShoe.describeSelf(); nikeRunningshoe.describeSelf(); // 生产服装系列产品 Clothes nikeJack = nikeFactory.produceClothes(BRAND.JACK_CLOTHES);// 夹克衫 Clothes nikeTshit = nikeFactory.produceClothes(BRAND.TSHIRT_CLOTHES);// T恤 nikeJack.printUsage(); nikeTshit.printUsage(); System.out.println("============================"); // 李宁工厂,生产鞋系列产品(篮球鞋,跑鞋)和服装系列产品(夹克,T恤) Factory liningFactory = new LiningFactory(); // 生产鞋系列产品 Shoe liningBasketballShoe = liningFactory.produceShoe(BRAND.BASKETBALL_SHOE); Shoe liningRunningShoe = liningFactory.produceShoe(BRAND.RUNNING_SHOE); liningBasketballShoe.describeSelf(); liningRunningShoe.describeSelf(); // 生产服装系列产品 Clothes liningJack = liningFactory.produceClothes(BRAND.JACK_CLOTHES); Clothes liningTshirt = liningFactory.produceClothes(BRAND.TSHIRT_CLOTHES); liningJack.printUsage(); liningTshirt.printUsage(); } }
标签:abs running static src color code lsh height ret
原文地址:https://www.cnblogs.com/rouqinglangzi/p/11122316.html