Design model series (1) -factory implemented with Java

zhaozj2021-02-16  50

I just finished the design model of the four men, I always think that this book is really difficult, pain, just find the best way to understand the design model with Java, but I have only C design mode online now. Realize the source code, but there is no complete design mode source code for Java implementation. The younger brother is not talented, I hope to take this article, let prapons work together to organize a more complete and easy understanding of the design mode source code implemented with Java!

Also: There is no place to upload the source code on 9CBS. I hope that all source code can be summarized and packaged to provide sessions.

/ ** * Design Pattern in Java * Name: Factory * Purpose: Use factory mode to create two product series Mac and Win * Mac: Macram, Maccpu * Win: Winram, WinCPU * A: Abstract * C: Concret * Author: BlackPhoenix * MODIFY DATE: 2002-08-17 * /

/ ** * abstract Product Categories Ram Cpu * / abstract class AProductRam {public String getProduct () {return this "Product";}} abstract class AProductCpu {public String getProduct () {return this "Product";}} / ** * specific products MacRam, WinRam * / class CProductMacRam extends AProductRam {public String toString () {return "MacRam";}} class CProductWinRam extends AProductRam {public String toString () {return "WinRam";}} / ** * the specific product MacCpu, WinCpu * / class CProductMacCpu extends AProductCpu {public String toString () {return "MacCpu";}} class CProductWinCpu extends AProductCpu {public String toString () {return "WinCpu";}}

When / ** * Generate a new product series, changes to the interface code * // * class CProductNewRam extends AProductRam {public String toString () {return "NewRam";}} class CProductNewCpu extends AProductCpu {public String toString () { Return "newcpu";}} * /

/ ** * abstract factory AFactory * / interface AFactory {public AProductRam CreateProductRam (); public AProductCpu CreateProductCpu ();} / ** * Create concrete factory CFactoryMac * Mac Series × MacRam MacCpu * / class CFactoryMac implements AFactory {public AProductRam CreateProductRam () {return new CProductMacRam ();} public AProductCpu CreateProductCpu () {return new CProductMacCpu ();}} / ** * Create concrete factory CFactoryWin * Win series × WinRam WinCpu * / class CFactoryWin implements AFactory {public AProductRam CreateProductRam ( ) {Return new cproductwinram (); public aproductcpu createProductCPU () {Return New CPRoductWinCPU ();}} / ** * When generating a new product line, a new product factory should send a new product factory * at the same time a new concrete products * CFactoryNew * NewMem, NewCpu * // * class CFactoryNew implements AFactory {public AProductRam CreateProductRam () {return new CProductNewRam ();} public AProductCpu CreateProductCpu () {return new CProductNewCpu ();}} * // ** * Client * 1. Create a Mac Series using FactoryMac: ProductMacram, ProductMaccpu × 2. Create a WIN Series using FactoryWIN: ProductWinram, ProductWincpu * 3. Just know CFactory Mac, CFActorywin and * Aproductram, AproductCPU (Abstract Product), as for the creation of specific products * Detail customers don't care * /

转载请注明原文地址:https://www.9cbs.com/read-28026.html

New Post(0)