Pattern development with C #

0

They passed me a program in C # in POO, I have my notions that they are classes and objects, but I wanted to know why in PRODUCTOMODEL first instance Producto pro=new Producto(); and then create Product p to later match.

Then I see that in frmPRODUCTO they do pr = model.find (); .

I can not understand why they do those things (maybe it is poorly implemented, but if compiled) , I do not know if it will be pattern design.

This is the code:

PRODUCT CLASS

public class Producto
{
    // propiedades
    public string IdProducto { get; set; }
    public string Nombre { get; set; }
    public DateTime Fecha { get; set; }
    public decimal Precio { get; set; }
    public int Cantidad { get; set; }
    //constructor
    public Producto()
    {
        IdProducto = "P001";
        Nombre = "Producto Inicial";
        Fecha = DateTime.Now;
        Precio = 120.50M;
        Cantidad = 100;
    }

PRODUCTOMODEL CLASS

public class ProductoModel
{

    Producto pro=new Producto();

    //constructor
    public ProductoModel()
    { 
    }
    // metodos para procesar
    public void create(Producto p)
    {

        pro = p;

    }

    //public Producto find()
    //{
    //    return pro;
    //}
    public Producto find(Producto o)
    {
        return pro;
    }

frmPRODUCT

public frmProducto()
    {
        InitializeComponent();
    }
    // instanciar objeto de la clase productocontroller
    ProductoController obj = new ProductoController();
    Producto pr=new Producto();
    ProductoModel model = new ProductoModel();


    private void btnMostrar_Click(object sender, EventArgs e)
    {
        pr = model.find();

        lblIdproducto.Text = pr.IdProducto;
        lblNombre.Text = pr.Nombre;
        lblFecha.Text = pr.Fecha.ToShortDateString();
        lblPrecio.Text = pr.Precio.ToString();
        lblCantidad.Text = pr.Cantidad.ToString();
        //pr = model.find(pr);
    }

    private void btnRegistrar_Click(object sender, EventArgs e)
    {

        // asignar valores al objeto pr
        pr.IdProducto =""+ txtIdproducto.Text;
        pr.Nombre = txtNombre.Text;
        pr.Fecha = dtpFecha.Value;
        pr.Precio = decimal.Parse(txtPrecio.Text);
        pr.Cantidad = (int)numCantidad.Value;
        // registrar producto
        model.create(pr);
        txtIdproducto.Text = pr.IdProducto;
        MessageBox.Show("Producto registrado con exito", "exito", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
    }
    
asked by FransMB 18.06.2017 в 01:43
source

1 answer

0
Producto pro=new Producto();

and

pro = p;

They do it in order to use the Product p that they pass as a parameter in the whole class. Since the variable pro has scope in the whole class because it is declared within its context.

pr = model.find();

The only thing it does is to return the Product that is in the instance model .
If you look closely that part is wrong, since the find() function needs a parameter, and in this part of the code they call it without passing the parameter to it.

    
answered by 18.06.2017 в 02:08