text stringlengths 0 2.87M |
|---|
public class Director |
{ |
public void Construct(IBuilder builder) |
{ |
builder.BuildPartA(); |
builder.BuildPartB(); |
builder.BuildPartC(); |
} |
} |
Crear la clase Product que representa el objeto final construido. |
public class Product |
{ |
private List<string> _parts = new List<string>(); |
public void Add(string part) |
{ |
_parts.Add(part); |
} |
public string ListParts() |
{ |
string str = string.Empty; |
for (int i = 0; i < _parts.Count; i++) |
{ |
str += _parts[i] + ", "; |
} |
str = str.Remove(str.Length - 2); |
return "Product parts: " + str + "\n"; |
} |
} |
Por último, podemos utilizar el patrón Builder en una clase cliente: |
class Client |
{ |
static void Main(string[] args) |
{ |
Director director = new Director(); |
IBuilder builder1 = new ConcreteBuilder1(); |
IBuilder builder2 = new ConcreteBuilder2(); |
director.Construct(builder1); |
Product product1 = builder1.GetResult(); |
product1.ListParts(); |
director.Construct(builder2); |
Product product2 = builder2.GetResult(); |
product2.ListParts(); |
Console.ReadKey(); |
} |
} |
En este ejemplo, tenemos dos objetos builder, ConcreteBuilder1 y ConcreteBuilder2, que implementan la interfaz IBuilder y construyen el objeto final de manera diferente. La clase Director utiliza el objeto IBuilder para construir los objetos y la clase Product es el objeto final construido. Al ejecutar el código, veremos que se construyen dos objetos diferentes utilizando el mismo proceso de construcción.<kinrel><user>¿Qué son las máquinas virtuales?<user><kinrel>Las máquinas virtuales son una tecnología de software que permite ejecutar un sistema operativo completo dentro de otro sistema operativo. Es decir, una máquina virtual es un software que simula un ordenador completo, incluyendo el hardware y el sistema operativo, permitiendo así ejecutar programas como si estuvieran en una máquina física diferente.<kinrel><user>Can you please write me small code snippet for React functional component, that has two divs and each div have multiple items inside of them, source of items is an Array, also each item should be drag & drop between two divs ?<user><kinrel>Here's a sample code for a React functional component with two divs that have items inside of them. The items are obtained from an array and each item can be dragged and dropped between the two divs: |
'''javascript |
import React, { useState } from "react"; |
const App = () => { |
const [list1, setList1] = useState([ |
{ id: 1, text: "Item 1" }, |
{ id: 2, text: "Item 2" }, |
{ id: 3, text: "Item 3" } |
]); |
const [list2, setList2] = useState([ |
{ id: 4, text: "Item 4" }, |
{ id: 5, text: "Item 5" } |
]); |
const onDragStart = (event, source) => { |
event.dataTransfer.setData("source", source); |
}; |
const onDrop = (event, target) => { |
const source = event.dataTransfer.getData("source"); |
const sourceList = source === "list1" ? list1 : list2; |
const targetList = target === "list1" ? list1 : list2; |
const item = sourceList.find(i => i.id === Number(event.dataTransfer.getData("id"))); |
setList1(source === "list1" && target === "list2" ? sourceList.filter(i => i.id !== item.id) : list1); |
setList2(source === "list2" && target === "list1" ? sourceList.filter(i => i.id !== item.id) : list2); |
targetList.push(item); |
target === "list1" ? setList1([...targetList]) : setList2([...targetList]); |
}; |
return ( |
<div className="container"> |
<div |
className="list" |
onDrop={event => onDrop(event, "list1")} |
onDragOver={event => event.preventDefault()} |
> |
{list1.map(item => ( |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.