This is the first day of my participation in Gwen Challenge
1. Simple factory mode
The factory pattern is a common Java design pattern that uses factory methods instead of new to generate an object, which we create without exposing logic to the client, and which points to the object through a common interface. This pattern is used to encapsulate and manage object creation and is a creation pattern. Let the subclass implement the factory interface, determine which object to create, and let the subclass do the creation.
For example, if you need a cell phone, you just go to the factory and pick it up. You don’t need to know how it was made.
Creating an interface
public interface tree {
void result(a);
}
Copy the code
Create classes to implement this interface
Apple class
public class apple implements tree{
@Override
public void result(a) {
// TODO Auto-generated method stub
System.out.println("apple tree result apple"); }}Copy the code
Banana class
public class banana implements tree{
@Override
public void result(a) {
// TODO Auto-generated method stub
System.out.println("banana tree result banana"); }}Copy the code
Cherry class
public class cherry implements tree{
@Override
public void result(a) {
// TODO Auto-generated method stub
System.out.println("cherry tree result cherry"); }}Copy the code
classDiagram
tree <|-- apple
tree <|-- banana
tree <|-- cherry
tree: +result()
class apple{
+result()
}
class banana{
+result()
}
class cherry{
+result()
}
Create factories to generate entity-class objects
public class treeFactory {
public tree choseFactoryTree(String treeType) {
if(treeType.length() <= 0 || treeType.equals("")) {
return null;
}
if(treeType.equalsIgnoreCase("apple")) {
return new apple();
}else if(treeType.equalsIgnoreCase("banana")) {
return new banana();
}else if(treeType.equalsIgnoreCase("cherry")) {
return new cherry();
}
return null; }}Copy the code
Using this factory, different fruits are obtained by different parameters
public class factoryDemo {
public static void main(String[] args) {
// Generate the factory object
treeFactory treeFactory1 = new treeFactory();
// Get the Apple object
tree tree1 = treeFactory1.choseFactoryTree("apple");
// Call apple's result method
tree1.result();
// Get the banana object
tree tree2 = treeFactory1.choseFactoryTree("banana");
// Call the banana result method
tree2.result();
// Get cherry's object
tree tree3 = treeFactory1.choseFactoryTree("cherry");
// Call cherry's result methodtree3.result(); }}Copy the code
graph TD factoryDemo --> treeFactory treeFactory --> tree tree --> appletree -->apple tree --> bananatree -->banana tree --> cherrytree -->cherry
Run the code to print the results
apple tree result apple
banana tree result banana
cherry tree result cherry
Copy the code
2. Factory method pattern
In contrast to the simple factory, the factory method pattern assigns specific manufacturing tasks to specific factories
Tree and its implementation classes apple, banana, cherry remain unchanged
Create an abstract factory that produces factory classes for different products
public interface abstractFactory {
tree getResult(a);
}
Copy the code
Create the factory that generates the Apple
public class appleFactory implements abstractFactory{
@Override
public tree getResult(a) {
// TODO Auto-generated method stub
return newapple(); }}Copy the code
Create the factory that generates banana
public class bananaFactory implements abstractFactory{
@Override
public tree getResult(a) {
// TODO Auto-generated method stub
return newbanana(); }}Copy the code
Create the factory that produces cherry
public class cherryFactory implements abstractFactory{
@Override
public tree getResult(a) {
// TODO Auto-generated method stub
return newcherry(); }}Copy the code
Call the factory to produce
public class Demo2 {
public static void main(String[] args) {
abstractFactory appleFactory = new appleFactory();
appleFactory.getResult().result();
abstractFactory bananaFactory = new bananaFactory();
bananaFactory.getResult().result();
abstractFactory cherryFactory = newcherryFactory(); cherryFactory.getResult().result(); }}Copy the code
The output
apple tree result apple
banana tree result banana
cherry tree result cherry
Copy the code
The flow chart
graph TD
demoClient --> abstractFactory
abstractFactory --> appleFactory --> appletree -->apple
abstractFactory --> bananaFactory --> bananatree -->banana
abstractFactory --> cherryFactory --> cherrytree -->cherry
3. Abstract factory pattern
Instead of the previous two design patterns just capturing a fruit, we now want to capture a new product. The abstract factory pattern adds interfaces in the pull to the factory and implements the creation of the product in the concrete sub-factory.
Define the interface to the Leaf class product
public interface leaf {
void makeLeaf(a);
}
Copy the code
Define the appleLeaf product
public class appleLeaf implements leaf{
@Override
public void makeLeaf(a) {
// TODO Auto-generated method stub
System.out.println("apple tree make appleLeft"); }}Copy the code
Define the bananaLeaf product
public class bananaLeaf implements leaf{
@Override
public void makeLeaf(a) {
// TODO Auto-generated method stub
System.out.println("banana tree make bananaLeft"); }}Copy the code
Define the cherryLeaf product
public class cherryLeaf implements leaf{
@Override
public void makeLeaf(a) {
// TODO Auto-generated method stub
System.out.println("cherry tree make cherryLeft"); }}Copy the code
Modify the abstractFactory interface add the manufacturing interface of leaf
public interface abstractFactory {
tree getResult(a);
leaf getLeaf(a);
}
Copy the code
Modified appleFactory to add leaf manufacturing
public class appleFactory implements abstractFactory{
@Override
public tree getResult(a) {
// TODO Auto-generated method stub
return new apple();
}
@Override
public leaf getLeaf(a) {
// TODO Auto-generated method stub
return newappleLeaf(); }}Copy the code
Modified bananaFactory, added leaf manufacturing
public class bananaFactory implements abstractFactory{
@Override
public tree getResult(a) {
// TODO Auto-generated method stub
return new banana();
}
@Override
public leaf getLeaf(a) {
// TODO Auto-generated method stub
return newbananaLeaf(); }}Copy the code
Modified cherryFactory to add leaf manufacturing
public class cherryFactory implements abstractFactory{
@Override
public tree getResult(a) {
// TODO Auto-generated method stub
return new cherry();
}
@Override
public leaf getLeaf(a) {
// TODO Auto-generated method stub
return newcherryLeaf(); }}Copy the code
The Demo test
public class Demo3 {
public static void main(String[] args) {
abstractFactory appleFactory = new appleFactory();
appleFactory.getResult().result();
appleFactory.getLeaf().makeLeaf();
abstractFactory bananaFactory = new bananaFactory();
bananaFactory.getResult().result();
bananaFactory.getLeaf().makeLeaf();
abstractFactory cherryFactory = newcherryFactory(); cherryFactory.getResult().result(); cherryFactory.getLeaf().makeLeaf(); }}Copy the code
The output
apple tree result apple
apple tree make appleLeft
banana tree result banana
banana tree make bananaLeft
cherry tree result cherry
cherry tree make cherryLeft
Copy the code
process
graph TD client --> abstractFactory abstractFactory --> appleFactory appleFactory --> getapple -->apple appleFactory --> makeappleLeaf -->appleLeaf abstractFactory --> bananaFactory bananaFactory --> getbanana -->banana bananaFactory --> makebananaLeaf -->bananaLeaf abstractFactory --> cherryFactory cherryFactory --> getcherry -->cherry cherryFactory --> makecherryLeaf -->cherryLeaf
I don’t have tools in front of me, so I can’t draw a pretty picture.