发布网友 发布时间:2022-05-12 05:25
共1个回答
热心网友 时间:2023-11-20 13:41
小弟不才,希望借此篇文章,让大虾们共同努力,整理出一份较为完整且容易理解的用Java实现的设计模式源码! /** * Design Pattern In Java * Name:Factory * 目的:利用工厂模式创建两个产品系列Mac和Win * Mac:MacRam,MacCpu * Win:WinRam,WinCpu * A:abstract * C:Concret * Author:blackphoenix * Modify Date:2002-08-17 */ /** * 抽象产品类 Ram Cpu */ abstract class AProctRam { public String getProct() { return this+" Proct"; } } abstract class AProctCpu { public String getProct() { return this+" Proct"; } } /** * 具体产品 MacRam,WinRam */ class CProctMacRam extends AProctRam { public String toString() { return "MacRam"; } } class CProctWinRam extends AProctRam { public String toString() { return "WinRam"; } } /** * 具体产品 MacCpu,WinCpu */ class CProctMacCpu extends AProctCpu { public String toString() { return "MacCpu"; } } class CProctWinCpu extends AProctCpu { public String toString() { return "WinCpu"; } } /** *生成一种新的产品系列时,接口改动的代码 */ /* class CProctNewRam extends AProctRam { public String toString() { return "NewRam"; } } class CProctNewCpu extends AProctCpu { public String toString() { return "NewCpu"; } } */ /** * 抽象工厂 AFactory */ interface AFactory { public AProctRam CreateProctRam(); public AProctCpu CreateProctCpu(); } /** * 具体工厂 CFactoryMac * 创建Mac产品系列 × MacRam MacCpu */ class CFactoryMac implements AFactory { public AProctRam CreateProctRam() { return new CProctMacRam(); } public AProctCpu CreateProctCpu() { return new CProctMacCpu(); } } /** * 具体工厂 CFactoryWin * 创建Win产品系列 × WinRam WinCpu */ class CFactoryWin implements AFactory { public AProctRam CreateProctRam() { return new CProctWinRam(); } public AProctCpu CreateProctCpu() { return new CProctWinCpu(); } } /** * 要生成一种新的产品系列时,对客户要派发一个新的产品工厂 * 同时要派生出一种新的具体产品 * CFactoryNew * NewMem,NewCpu */ /* class CFactoryNew implements AFactory { public AProctRam CreateProctRam() { return new CProctNewRam(); } public AProctCpu CreateProctCpu() { return new CProctNewCpu(); } } */ /** * 客户端 * 1.使用FactoryMac创建Mac系列产品:ProctMacRam,ProctMacCpu × 2.使用FactoryWin创建Win系列产品:ProctWinRam,ProctWinCpu * 3.只要知道CFactoryMac,CFactoryWin(具体工厂)和 * AProctRam,AProctCpu(抽象产品)即可,至于具体产品的创建 * 细节客户不用关心 */ class Factory { private static void printProct(String v) { System.out.println(v); } public static void main(String[] args) { AProctRam ProctMacRam=null,ProctWinRam=null; AProctCpu ProctMacCpu=null,ProctWinCpu=null; CFactoryMac FactoryMac=new CFactoryMac(); CFactoryWin FactoryWin=new CFactoryWin(); ProctMacRam=FactoryMac.CreateProctRam(); ProctMacCpu=FactoryMac.CreateProctCpu(); ProctWinRam=FactoryWin.CreateProctRam(); ProctWinCpu=FactoryWin.CreateProctCpu(); printProct(ProctMacRam.getProct()); printProct(ProctMacCpu.getProct()); printProct(ProctWinRam.getProct()); printProct(ProctWinCpu.getProct()); /** *生成一种新的产品系列时,客户端改动的代码 */ /* AProctRam ProctNewRam=null; AProctCpu ProctNewCpu=null; CFactoryNew FactoryNew=new CFactoryNew(); ProctNewRam=FactoryNew.CreateProctRam(); ProctNewCpu=FactoryNew.CreateProctCpu(); printProct(ProctNewRam.getProct()); printProct(ProctNewCpu.getProct()); */ } }