After reading the design model of the four-person gang, I always thought it was really hard to understand this book. When it was so painful, I just found that using Java to understand the design pattern was originally the best way, however, I have suffered from the fact that only the C ++ design mode implements the source code on the Internet, but there is no complete source code for the Java design mode. I hope this articleArticleLet the prawns work together to prepare a complete and easy-to-understand source code of the Design Pattern Implemented in Java!
/**
* Design Pattern in Java
* Name: Factory
* Objective: to create two product series Mac and win using the factory Model
* Mac: macram, maccpu
* Win: winram, wincpu
* A: Abstract
* C: concret
* Author: blackphoenix
* Modify Date: 2002-08-17
*/
/**
* Abstract product Ram CPU
*/
Abstract class aproductram {
Public String getproduct (){
Return this + "product ";
}
}
Abstract class aproductcpu {
Public String getproduct (){
Return this + "product ";
}
}
/**
* Specific product macram and winram
*/
Class cproductmacram extends aproductram {
Public String tostring (){
Return "macram ";
}
}
Class cproductwinram extends aproductram {
Public String tostring (){
Return "winram ";
}
}
/**
* Specific product maccpu and wincpu
*/
Class cproductmaccpu extends aproductcpu {
Public String tostring (){
Return "maccpu ";
}
}
Class cproductwincpu extends aproductcpu {
Public String tostring (){
Return "wincpu ";
}
}
/**
* When a new product series is generated, the interface changesCode
*/
/*
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 ();
}
/**
* Specific factory cfactorymac
* Create a Mac product series
* Macram maccpu
*/
Class cfactorymac implements afactory {
Public aproductram createproductram (){
Return new cproductmacram ();
}
Public aproductcpu createproductcpu (){
Return new cproductmaccpu ();
}
}
/**
* Specific factory cfactorywin
* Create win product series
× Winram wincpu
*/
Class cfactorywin implements afactory {
Public aproductram createproductram (){
Return new cproductwinram ();
}
Public aproductcpu createproductcpu (){
Return new cproductwincpu ();
}
}
/**
* To generate a new product series, assign a new product factory to the customer
* At the same time, a new product must be derived.
* Cfactorynew
* Newmem, newcpu
*/
/*
Class cfactorynew implements afactory {
Public aproductram createproductram (){
Return new cproductnewram ();
}
Public aproductcpu createproductcpu (){
Return new cproductnewcpu ();
}
}
*/
/**
* Client
* 1. Use factorymac to create Mac products: productmacram and productmaccpu
× 2. Use factorywin to create win series products: productwinram and productwincpu
* 3. As long as you know about cfactorymac, cfactorywin (specific factory) and
* Aproductram and aproductcpu (Abstract product) can be used to create a specific product.
* Customers do not need to care about details
*/
Class factory {
Private Static void printproduct (string v ){
System. Out. println (v );
}
Public static void main (string [] ARGs ){
Aproductram productmacram = NULL, productwinram = NULL;
Aproductcpu productmaccpu = NULL, productwincpu = NULL;
Cfactorymac factorymac = new cfactorymac ();
Cfactorywin factorywin = new cfactorywin ();
Productmacram = factorymac. createproductram ();
Productmaccpu = factorymac. createproductcpu ();
Productwinram = factorywin. createproductram ();
Productwincpu = factorywin. createproductcpu ();
Printproduct (productmacram. getproduct ());
Printproduct (productmaccpu. getproduct ());
Printproduct (productwinram. getproduct ());
Printproduct (productwincpu. getproduct ());
/**
* Code changed by the client when a new product series is generated
*/
/*
Aproductram productnewram = NULL;
Aproductcpu productnewcpu = NULL;
Cfactorynew factorynew = new cfactorynew ();
Productnewram = factorynew. createproductram ();
Productnewcpu = factorynew. createproductcpu ();
Printproduct (productnewram. getproduct ());
Printproduct (productnewcpu. getproduct ());
*/
}
}