In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to implement design patterns in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
/ * *
* Design Pattern In Java
* Name:Factory
* objective: to create two product lines, 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 class Ram Cpu
, /
Abstract class AProductRam {
Public String getProduct () {
Return this+ "Product"
}
}
Abstract class AProductCpu {
Public String getProduct () {
Return this+ "Product"
}
}
/ * *
* specific product MacRam,WinRam
, /
Class CProductMacRam extends AProductRam {
Public String toString () {
Return "MacRam"
}
}
Class CProductWinRam extends AProductRam {
Public String toString () {
Return "WinRam"
}
}
/ * *
* specific product MacCpu,WinCpu
, /
Class CProductMacCpu extends AProductCpu {
Public String toString () {
Return "MacCpu"
}
}
Class CProductWinCpu extends AProductCpu {
Public String toString () {
Return "WinCpu"
}
}
/ * *
* the code for interface changes when a new product family is generated
, /
/ *
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 Mac product family
× MacRam MacCpu
, /
Class CFactoryMac implements AFactory {
Public AProductRam CreateProductRam () {
Return new CProductMacRam ()
}
Public AProductCpu CreateProductCpu () {
Return new CProductMacCpu ()
}
}
/ * *
* specific factory CFactoryWin
* create Win product family
× WinRam WinCpu
, /
Class CFactoryWin implements AFactory {
Public AProductRam CreateProductRam () {
Return new CProductWinRam ()
}
Public AProductCpu CreateProductCpu () {
Return new CProductWinCpu ()
}
}
/ * *
* to generate a new product line, dispatch a new product factory to the customer
* at the same time to derive a new specific product
* 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 series products: ProductMacRam,ProductMacCpu
× 2. Use FactoryWin to create Win series products: ProductWinRam,ProductWinCpu
* 3. As long as you know CFactoryMac,CFactoryWin (specific factory) and
* AProductRam,AProductCpu (abstract product) is fine, as for the creation of specific products
* 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 ())
/ * *
* the code changed by the client when a new product line is generated
, /
/ *
AProductRam ProductNewRam=null
AProductCpu ProductNewCpu=null
CFactoryNew FactoryNew=new CFactoryNew ()
ProductNewRam=FactoryNew.CreateProductRam ()
ProductNewCpu=FactoryNew.CreateProductCpu ()
PrintProduct (ProductNewRam.getProduct ())
PrintProduct (ProductNewCpu.getProduct ())
, /
}
}
Thank you for reading! This is the end of this article on "how to use Java to achieve design patterns". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.