Java factory model learning, java factory Model
There are three factory models: simple factory, factory method, and abstract factory. The abstract factory is used for multiple product families. The product family is an organic whole composed of different products, and different products depend on each other. For example, a computer has a CPU, motherboard, memory, and hard disk. These products form a product family. For example, if Dell and HP want to assemble these products differently, they need to use abstract factories. The following is a code Demonstration:
Simple factory, which can be implemented using reflection:
First, define the abstract interface of the product.
package com.demo.SimpleFactory;public interface CPU { public void run();}
Define a specific product
package com.demo.SimpleFactory;public class Intel implements CPU { @Override public void run() { // TODO Auto-generated method stub System.out.println("This is Intel!"); }}
package com.demo.SimpleFactory;public class Amd implements CPU { @Override public void run() { // TODO Auto-generated method stub System.out.println("This is Amd!"); }}
Define factory class
package com.demo.SimpleFactory;public class Factory { public static CPU create(String str) { CPU c = null; try { c = (CPU) Class.forName("com.demo.SimpleFactory." + str) .newInstance(); } catch (Exception e) { e.printStackTrace(); } return c; }}
The test is as follows:
package com.demo.SimpleFactory;public class Test { public static void main(String[] args){ CPU c=Factory.create("Amd"); c.run(); }}
Print result:
This is Amd!
Factory method
First, define the abstract interface of the product.
package com.demo.SimpleFactory;public interface CPU { public void run();}
Define a specific product
package com.demo.SimpleFactory;public class Intel implements CPU { @Override public void run() { // TODO Auto-generated method stub System.out.println("This is Intel!"); }}
package com.demo.SimpleFactory;public class Amd implements CPU { @Override public void run() { // TODO Auto-generated method stub System.out.println("This is Amd!"); }}
Define the abstract factory. Its sub-classes are responsible for the specific reality.
package com.demo.FactoryMethod;public interface FactoryMethod { public CPU create();}
Specific subclass Factory
package com.demo.FactoryMethod;public class AmdFactory implements FactoryMethod { @Override public CPU create() { // TODO Auto-generated method stub return new Amd(); }}
package com.demo.FactoryMethod;public class IntelFactory implements FactoryMethod { public CPU create() { return new Intel(); }}
Test
package com.demo.FactoryMethod;public class Test { public static void main(String[] args) { FactoryMethod af=new IntelFactory(); CPU c=af.create(); c.run(); }}
Print result:
This is Intel!
Abstract Factory
First, define the abstract interface of the product.
package com.demo.SimpleFactory;public interface CPU { public void run();}
Define a specific product
package com.demo.SimpleFactory;public class Intel implements CPU { @Override public void run() { // TODO Auto-generated method stub System.out.println("This is Intel!"); }}
package com.demo.SimpleFactory;public class Amd implements CPU { @Override public void run() { // TODO Auto-generated method stub System.out.println("This is Amd!"); }}
package com.demo.AbstractFactory;public interface Memory { public void read(String str);}
package com.demo.AbstractFactory;public class KingstonMemory implements Memory { public KingstonMemory() { // TODO Auto-generated constructor stub } @Override public void read(String str) { // TODO Auto-generated method stub System.out.println("Kingston Read data is:"+str); }}
package com.demo.AbstractFactory;public class AdataMemory implements Memory { public AdataMemory() { // TODO Auto-generated constructor stub } @Override public void read(String str) { // TODO Auto-generated method stub System.out.println("Adata read data is:"+str); }}
Define Abstract Factory
package com.demo.AbstractFactory;public interface AbstractFactory { public CPU createCPU(); public Memory createMemory();}
Different subclass produces different product families
package com.demo.AbstractFactory;public class DellFactory implements AbstractFactory { @Override public CPU createCPU() { // TODO Auto-generated method stub return new Intel(); } @Override public Memory createMemory() { // TODO Auto-generated method stub return new KingstonMemory(); }}
package com.demo.AbstractFactory;public class HPFactory implements AbstractFactory { @Override public CPU createCPU() { // TODO Auto-generated method stub return new Amd(); } @Override public Memory createMemory() { // TODO Auto-generated method stub return new AdataMemory(); }}
Test
package com.demo.AbstractFactory;public class Test { public static void main(String[] args){ AbstractFactory hp=new HPFactory(); CPU cpu=hp.createCPU(); Memory memory=hp.createMemory(); cpu.run(); memory.read("Pass"); }}
Print result:
This is Amd!
Adata read data is: Pass
What is the factory mode in java?
Are you asking about the simple factory model?
Simple Factory Pattern, also known as Static Factory Pattern ). Here are two examples to quickly understand the simple factory mode in Java:
The nvwa
Saying: "The world is open, there are no people, And the nvwa community is a human ." Nvwa needs to create people by nature, but before nvwa creates a person, the concept of man only exists in nvwa's thoughts.
This is the application of the simple factory model.
First, there are several important roles in this idea: nvwa itself, the concept of abstract people, and the specific people created by nvwa.
1) nvwa is a factory, that is, the core role of the simple factory model.
2) Retired individuals, including Zhang San and Li Si. These people are specific product roles in the simple factory model.
3.) Abstract people are an idea that only existed in nvwa's mind at the earliest. All specific people created by nvwa according to this idea comply with the definition of this abstract person. In other words, this abstract idea specifies the interfaces (features or functions) that all specific people must have)
Note: nvwa, Zhang San, and Li Si belong to creating dependencies. The arrows should be dotted lines (implementation represents associations ). Zhang San and Li Si have an implementation relationship with people, and the arrow at the triangle header should also be a dotted line (solid line represents an inheritance relationship ).
After understanding the above things, let's take a look at the following example. I believe that after reading this article, I will have a good understanding of the java simple factory model:
There is a farm company that specializes in selling all kinds of fruits to the market. In this system, we need to describe the following fruits:
Grape
Strawberry
Apple
Different from other plants, fruit can finally be picked and eaten. A natural practice is to establish an interface applicable to various fruits to distinguish them from plants on other farms,
The following Code declares an interface for the fruit class:
Public interface Fruit
{
// Planting
Public void plant ();
// Growth
Public void grow ();
// Gains
Public void harvest ();
}
The fruit interface specifies the interfaces that all fruits must implement, including the methods required by any fruit class, plant (), grow (), and harvest ();
Apple is a kind of fruit, so it implements all the methods declared by the fruit interface. In addition, because Apple is a perennial plant, a treeAge exists to describe the age of the apple. The Code is as follows:
/**
*
* Class Name: Apple
* Category Description: Farm Apple category
*
*/
Public class Apple implements Fruit
{
/**
* Apple is a perennial plant.
*/
Private int treeAge;
/* (Non-Javadoc)
* @ See fac. Fruit # grow ()
*/
@ Override
Public void grow ()
{
System. out. println ("Apple started to grow... ");
}
/* (Non-Javadoc)
* @ See fac. Fruit # harvest ()
*/
@ Override
Public void harvest ()
{
System. out. println ("Apple started to harvest... ");
}
/* (Non-Javadoc)
* @ See fac. Fruit # ...... the remaining full text>
What are the advantages of using the factory mode in java?
If the object of A needs to be generated in many places, you need to write A lot of a = new ().
If you need to modify it, you need to modify it in many places.
However, if you use the factory mode, you only need to modify the factory code. The factory can be referenced in other places, so that only one place can be modified. If other code does not move, it is decoupled.