In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
The main purpose of factory mode is to provide transition interface for creating objects, so as to shield and isolate the specific process of creating objects, so as to improve flexibility.
Define an interface for creating an object and let its subclasses decide which class (product) to instantiate, and the factory method delays the creation of a class to its subclass.
There are three factory models:
1) simple factory model (Simple Factory)
2) Factory method mode (Factory Method)
3) Abstract factory pattern (Abstract Factory)
1. Simple factory model:
The simple factory pattern (Simple Factory) is seen as a special case of the factory method pattern (Factory Method), as can be seen from the following example.
1. Composition:
1) Factory role: this is the core of this model, which contains certain business logic and judgment logic. In java, it is often implemented by a concrete class.
2) Abstract product role: it is generally the parent class of concrete product inheritance or the interface of implementation. It is implemented by an interface or abstract class in java.
3) specific product role: the object created by the factory class is an instance of this role. It is implemented by a concrete class in java.
2. UML class diagram:
3. Related codes:
JAVA:
Factory .java:
/ / Factory role
Public class Factory {
Public static AbstractProduct getAnProduct (String s) {
If (s.equalsIgnoreCase ("product1")) {
Return new Product1 ()
} else if (s.equalsIgnoreCase ("product2")) {
Return new Product2 ()
} else {
System.out.println ("This type product is not exist in the factor yawns!")
Return null
}
}
}
AbstractProduct .java:
/ / Abstract product roles
Public interface AbstractProduct {
Public void doSomething ()
}
Product1 .java:
/ / specific product roles
Public class Product1 implements AbstractProduct {
@ Override
Public void doSomething () {
/ / TODO Auto-generated method stub
System.out.println ("doSomething for Product1 products!")
}
}
Product2 .java:
/ / specific product roles
Public class Product2 implements AbstractProduct {
@ Override
Public void doSomething () {
/ / TODO Auto-generated method stub
System.out.println ("doSomething for production 2 products!")
}
}
TestMain .java:
/ / Test class
Public class testMain {
/ * *
* @ param args
, /
Public static void main (String [] args) {
AbstractProduct p1 = Factory.getAnProduct ("product1")
P1.doSomething ()
AbstractProduct p2 = Factory.getAnProduct ("product2")
P2.doSomething ()
AbstractProduct p3 = Factory.getAnProduct ("product1")
P3.doSomething ()
AbstractProduct p4 = Factory.getAnProduct ("product2")
P4.doSomething ()
}
}
Running result:
DoSomething for Product1!!!
DoSomething for Product2!!!
DoSomething for Product1!!!
DoSomething for Product2!!!
Caterpillar:
/ / =
/ / Name: SimpleFactory.cpp
/ / Author: Yan chao
/ / Version:
/ / Copyright: Your copyright notice
/ / Description: Hello World in Clippers, Ansi-style
/ / =
# include
Using namespace std
/ / Abstract product roles
Class AbstractProduct {
Public:
AbstractProduct () {}
Virtual void doSomething () = 0
Virtual ~ AbstractProduct () {}
}
/ / specific product roles
Class Product1: public AbstractProduct {
Public:
Void doSomething () {
Cout
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.