Abstract factory experiment record

Abstract factory learning

Abstract factory pattern

Abstract factory pattern, which abstracts the method factory pattern. All over the world have their own orchards. We abstract these orchards as a water orchard interface. There are water orchards in China, Britain and the United States, planting different fruits, such as apples, bananas and pears. Apple is abstracted here. Therefore, apple is divided into Chinese apple, British apple and American apple. There are apples, bananas and pears in China's water orchards. The abstract factory states that it produces fruits such as apples, bananas and pears. Then the specific factory is equivalent to the fruit orchards in China, Britain and the United States. Each fruit garden is responsible for producing fruits, bananas and pears.

1. Related terms

  • **Product hierarchy structure: * * product inheritance structure, similar to class inheritance. For example, apple is an abstract class, so Chinese apple, American apple and British apple are its subclasses.
  • **Product family: * * refers to a group of products produced in the same factory and located in different product grade structures. For example, apples, bananas and pears are all produced in China. The hierarchical structure of fruits is different, forming a product family.
  • **Abstract factory: * * is an interface, the core of the abstract factory pattern, which contains declarations of multiple product hierarchies. Any factory class must implement this interface.
  • **Specific factory: * * is the implementation of an abstract factory, which is responsible for instantiating product objects in a product family. For example, Chinese factories produce apples, bananas and pears.

2. Schematic diagram of product family and product grade structure

Example: the COS system needs to realize the display function of order data statistics. The chart types of data display include Pie chart, Bar chart and Line chart. The chart styles include Crystal and Flat styles. It is required to supplement the code in the document to realize the display of different graphics of various styles.

package Abstract factory.experiment;

/**
 * @author chenj
 */
public interface Graph {
    public void showCrastal();

    public void showFlat();
}
package Abstract factory.experiment;

/**
 * @program: Design pattern learning
 * @description: Histogram
 * @author: chenzou
 * @create: 2021-09-22 08:46
 **/
public class BarGraph implements Graph{
    @Override
    public void showCrastal() {
        new BarCrystalStyle().show();
    }

    @Override
    public void showFlat() {
        new BarFlatStyle().show();
    }
}
package Abstract factory.experiment;

/**
 * @program: Design pattern learning
 * @description: Linear graph
 * @author: chenzou
 * @create: 2021-09-22 08:46
 **/
public class LineGraph implements Graph{
    @Override
    public void showCrastal() {
        new LineCrytalStyle().show();
    }

    @Override
    public void showFlat() {
        new LineFlatStyle().show();
    }
}

package Abstract factory.experiment;

/**
 * @program: Design pattern learning
 * @description: Pie chart
 * @author: chenzou
 * @create: 2021-09-22 08:46
 **/
public class PieGraph implements Graph {
    @Override
    public void showCrastal() {
        new PieCrystalStyle().show();
    }

    @Override
    public void showFlat() {
        new PieFlatStyle().show();
    }
}
class PieCrystalStyle extends CrystalStyle{
    @Override
    public void show() {
        System.out.println("Here is the crystal style pie chart");
    }
}class PieFlatStyle extends FlatStyle{
    @Override
    public void show() {
        System.out.println("Here is a flat pie chart");
    }
}
package Abstract factory.experiment;

/**
 * @program: Design pattern learning
 * @description: style
 * @author: chenzou
 * @create: 2021-09-22 08:42
 **/
public interface Style {
    public void show();
}
package Abstract factory.experiment;

/**
 * @program: Design pattern learning
 * @description: Crystal style
 * @author: chenzou
 * @create: 2021-09-22 08:42
 **/
public abstract class CrystalStyle implements Style {
    @Override
    public abstract void show();
}
package Abstract factory.experiment;

/**
 * @program: Design pattern learning
 * @description: Flat style
 * @author: chenzou
 * @create: 2021-09-22 08:44
 **/
public abstract class FlatStyle implements Style{
    @Override
    public abstract void show();
}
package Abstract factory.experiment;

/**
 * @program: Design pattern learning
 * @description: Crystal style of histogram
 * @author: chenzou
 * @create: 2021-09-22 08:53
 **/
public class BarCrystalStyle extends CrystalStyle{
    @Override
    public void show() {
        System.out.println("Here is the crystal style of the histogram");
    }
}
package Abstract factory.experiment;

/**
 * @program: Design pattern learning
 * @description: Flat style of histogram
 * @author: chenzou
 * @create: 2021-09-22 08:55
 **/
public class BarFlatStyle extends FlatStyle{
    @Override
    public void show() {
        System.out.println("Here is the flat style of the histogram");
    }
}
package Abstract factory.experiment;

/**
 * @program: Design pattern learning
 * @description: Crystal style of line diagram
 * @author: chenzou
 * @create: 2021-09-22 08:56
 **/
public class LineCrytalStyle extends CrystalStyle{
    @Override
    public void show() {
        System.out.println("Here is a line diagram in crystal style");
    }
}
package Abstract factory.experiment;

/**
 * @program: Design pattern learning
 * @description: Flat style of line graph
 * @author: chenzou
 * @create: 2021-09-22 08:58
 **/
public class LineFlatStyle extends FlatStyle{
    @Override
    public void show() {
        System.out.println("Here is a flat line chart");
    }
}

Test:

package Abstract factory.experiment;

/**
 * @program: Design pattern learning
 * @description: Test Abstract Factory
 * @author: chenzou
 * @create: 2021-09-22 09:06
 **/
public class TestFactory {
    public static void main(String[] args) {
//       Histogram
        BarGraph barGraph = new BarGraph();
        barGraph.showCrastal();
        barGraph.showFlat();
//    Linear graph
        LineGraph lineGraph = new LineGraph();
        lineGraph.showCrastal();
        lineGraph.showFlat();
//     Pie chart
        PieGraph pieGraph = new PieGraph();
        pieGraph.showCrastal();
        pieGraph.showFlat();
    }
}

Keywords: Java Algorithm

Added by mikeym on Wed, 22 Sep 2021 04:04:13 +0300