From 6ba6913e098b2914ce2f35583988e812dd8658e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Santanch=C3=A8?= Date: Wed, 4 Aug 2021 17:23:21 -0300 Subject: [PATCH] feat (lab 2/3): component factory --- .../components-chart-3-factory.ipynb | 666 ++++++++++++++++++ 1 file changed, 666 insertions(+) create mode 100644 labs/2021/02-dataflow_mvc/notebooks/components-chart-3-factory.ipynb diff --git a/labs/2021/02-dataflow_mvc/notebooks/components-chart-3-factory.ipynb b/labs/2021/02-dataflow_mvc/notebooks/components-chart-3-factory.ipynb new file mode 100644 index 000000000..d8dbd536d --- /dev/null +++ b/labs/2021/02-dataflow_mvc/notebooks/components-chart-3-factory.ipynb @@ -0,0 +1,666 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Componentes com Múltiplas Classes\n", + "\n", + "# Componente `MathSequence`" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.ISequence" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public interface ISequence {\n", + " public int first();\n", + " public int next();\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.ISeqInitialProperties" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public interface ISeqInitialProperties {\n", + " public int getInitial();\n", + " public void setInitial(int initial);\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.ISeqRatioProperties" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public interface ISeqRatioProperties {\n", + " public int getRatio();\n", + " public void setRatio(int ratio);\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.IMathSequence" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public interface IMathSequence\n", + " extends ISequence, ISeqInitialProperties {\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.IMathRatioSequence" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public interface IMathRatioSequence\n", + " extends IMathSequence, ISeqRatioProperties {\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.GeometricProgression" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public class GeometricProgression implements IMathRatioSequence {\n", + " private int initial,\n", + " ratio;\n", + " private int current;\n", + " \n", + " public GeometricProgression() {\n", + " initial = 1;\n", + " ratio = 2;\n", + " current = initial;\n", + " }\n", + " \n", + " public int getInitial() {\n", + " return initial;\n", + " }\n", + " \n", + " public void setInitial(int initial) {\n", + " this.initial = initial;\n", + " }\n", + " \n", + " public int getRatio() {\n", + " return ratio;\n", + " }\n", + " \n", + " public void setRatio(int ratio) {\n", + " this.ratio = ratio;\n", + " }\n", + " \n", + " public int first() {\n", + " current = initial;\n", + " return current;\n", + " }\n", + " \n", + " public int next() {\n", + " current *= ratio;\n", + " return current;\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.ArithmeticProgression" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public class ArithmeticProgression implements IMathRatioSequence {\n", + " private int initial,\n", + " ratio;\n", + " private int current;\n", + " \n", + " public ArithmeticProgression() {\n", + " initial = 1;\n", + " ratio = 1;\n", + " current = initial;\n", + " }\n", + " \n", + " public int getInitial() {\n", + " return initial;\n", + " }\n", + " \n", + " public void setInitial(int initial) {\n", + " this.initial = initial;\n", + " }\n", + " \n", + " public int getRatio() {\n", + " return ratio;\n", + " }\n", + " \n", + " public void setRatio(int ratio) {\n", + " this.ratio = ratio;\n", + " }\n", + " \n", + " public int first() {\n", + " current = initial;\n", + " return current;\n", + " }\n", + " \n", + " public int next() {\n", + " current += ratio;\n", + " return current;\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.Fibonacci" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public class Fibonacci implements IMathSequence {\n", + " private int initialCurrent, initialNext;\n", + " private int current, next;\n", + " \n", + " public Fibonacci() {\n", + " initialCurrent = 0;\n", + " initialNext = 1;\n", + " current = 0;\n", + " next = 1;\n", + " }\n", + " \n", + " public int getInitial() {\n", + " return initialCurrent;\n", + " }\n", + " \n", + " public void setInitial(int initial) {\n", + " current = 0;\n", + " next = 1;\n", + " while (initial > current) {\n", + " int sum = current + next;\n", + " current = next;\n", + " next = sum;\n", + " }\n", + " initialCurrent = current;\n", + " initialNext = next;\n", + " }\n", + " \n", + " public int first() {\n", + " current = initialCurrent;\n", + " next = initialNext;\n", + " return current;\n", + " }\n", + " \n", + " public int next() {\n", + " int sum = current + next;\n", + " current = next;\n", + " next = sum;\n", + " return current;\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.SequenceFactory" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public class SequenceFactory {\n", + " @SuppressWarnings(\"unchecked\")\n", + " public static IType createSequence(String type) {\n", + " IType sequence = null;\n", + " \n", + " try {\n", + " if (type.equalsIgnoreCase(\"fibonacci\"))\n", + " sequence = (IType)new Fibonacci();\n", + " else if (type.equalsIgnoreCase(\"arithmetic\"))\n", + " sequence = (IType)new ArithmeticProgression();\n", + " else if (type.equalsIgnoreCase(\"geometric\"))\n", + " sequence = (IType)new GeometricProgression();\n", + " } catch (ClassCastException e) {\n", + " e.printStackTrace();\n", + " }\n", + " \n", + " return sequence;\n", + " }\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Componente `BarChart`" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.IChart" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public interface IChart {\n", + " public void plot();\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.IRSequence" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public interface IRSequence {\n", + " public void connect(ISequence sequence);\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.IBarChartProperties" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public interface IBarChartProperties {\n", + " public boolean isFilled();\n", + " public void setFilled(boolean filled);\n", + "\n", + " public int getN();\n", + " public void setN(int n);\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.IBarChart" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public interface IBarChart\n", + " extends IChart, IRSequence, IBarChartProperties {\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.BarChart" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public abstract class BarChart implements IBarChart {\n", + " protected boolean filled;\n", + " protected int n;\n", + " \n", + " protected ISequence sequence;\n", + " \n", + " public BarChart() {\n", + " filled = true;\n", + " n = 3;\n", + " }\n", + "\n", + " public boolean isFilled() {\n", + " return filled;\n", + " }\n", + "\n", + " public void setFilled(boolean filled) {\n", + " this.filled = filled;\n", + " }\n", + "\n", + " public int getN() {\n", + " return n;\n", + " }\n", + "\n", + " public void setN(int n) {\n", + " this.n = n;\n", + " }\n", + "\n", + " public void connect(ISequence sequence) {\n", + " this.sequence = sequence;\n", + " }\n", + " \n", + " public abstract void plot();\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.ConsoleBarChart" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public class ConsoleBarChart extends BarChart {\n", + " private char character;\n", + " \n", + " public ConsoleBarChart() {\n", + " super();\n", + " character = '*';\n", + " }\n", + "\n", + " public char getCharacter() {\n", + " return character;\n", + " }\n", + "\n", + " public void setCharacter(char character) {\n", + " this.character = character;\n", + " }\n", + "\n", + " public void plot() {\n", + " if (sequence != null) {\n", + " int value = sequence.first();\n", + " for (int s = 1; s <= n; s++) {\n", + " if (value > 0) {\n", + " for (int v = 1; v < value; v++)\n", + " System.out.print((filled) ? character : ' ');\n", + " System.out.print(character);\n", + " }\n", + " System.out.println();\n", + " value = sequence.next();\n", + " }\n", + " }\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "com.twosigma.beaker.javash.bkr674cdd1c.BarChartFactory" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "public class BarChartFactory {\n", + " \n", + " public static IBarChart create(String type) {\n", + " IBarChart chart = null;\n", + " \n", + " if (type.equalsIgnoreCase(\"console\"))\n", + " chart = new ConsoleBarChart();\n", + " \n", + " return chart;\n", + " }\n", + "\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Aplicação Exemplo" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Geometric Progression:\n", + "*\n", + "**\n", + "****\n", + "********\n", + "****************\n", + "\n", + "Fibonacci Sequence:\n", + "*\n", + "*\n", + "**\n", + "***\n", + "*****\n" + ] + }, + { + "data": { + "text/plain": [ + "null" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "System.out.println(\"Geometric Progression:\");\n", + "\n", + "IMathRatioSequence gp = SequenceFactory.createSequence(\"geometric\");\n", + "gp.setInitial(1);\n", + "gp.setRatio(2);\n", + "\n", + "IBarChart bcg = BarChartFactory.create(\"console\");\n", + "bcg.setFilled(true);\n", + "bcg.setN(5);\n", + "\n", + "bcg.connect(gp);\n", + "bcg.plot();\n", + "\n", + "System.out.println(\"\\nFibonacci Sequence:\");\n", + "\n", + "IMathSequence fb = SequenceFactory.createSequence(\"fibonacci\");\n", + "fb.setInitial(1);\n", + "\n", + "IBarChart bcf = BarChartFactory.create(\"console\");\n", + "bcf.setFilled(true);\n", + "bcf.setN(5);\n", + "\n", + "bcf.connect(fb);\n", + "bcf.plot();" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Java", + "language": "java", + "name": "java" + }, + "language_info": { + "codemirror_mode": "text/x-java", + "file_extension": ".java", + "mimetype": "", + "name": "Java", + "nbconverter_exporter": "", + "version": "1.8.0_152-release" + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": false, + "sideBar": false, + "skip_h1_title": false, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": false, + "toc_position": {}, + "toc_section_display": false, + "toc_window_display": false + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}