Factory Pattern Demo
This tool demonstrates the Factory Design Pattern. Select an animal type, and the 'factory' will create an instance of that animal, showing its characteristics.
Results will appear here...
Please select an animal type first.
'; currentAnimal = null; currentAnimalType = ''; currentAnimalDetails = ''; downloadBtn.disabled = true; return; } const factory = new AnimalFactory(); const animal = factory.createAnimal(selectedType); if (animal) { currentAnimal = animal; // Store instance if needed later currentAnimalType = selectedType.charAt(0).toUpperCase() + selectedType.slice(1); // Capitalize currentAnimalDetails = animal.details(); // Get details string // Display results in the output div const titleElement = document.createElement('p'); titleElement.innerHTML = `Factory Created: ${currentAnimalType}`; const detailsElement = document.createElement('p'); detailsElement.innerHTML = `Details: ${currentAnimalDetails}`; outputDiv.appendChild(titleElement); outputDiv.appendChild(detailsElement); // Enable download button downloadBtn.disabled = false; } else { outputDiv.innerHTML = `Could not create animal of type: ${selectedType}. Type not supported by the factory.
`; currentAnimal = null; currentAnimalType = ''; currentAnimalDetails = ''; downloadBtn.disabled = true; } }); downloadBtn.addEventListener('click', () => { if (!currentAnimalType || !currentAnimalDetails) { alert("No results to download. Please create an animal first."); return; } try { const doc = new jsPDF(); const pageHeight = doc.internal.pageSize.height || doc.internal.pageSize.getHeight(); const pageWidth = doc.internal.pageSize.width || doc.internal.pageSize.getWidth(); let currentY = 20; // Start position from top // --- PDF Styling (Matching the Tool) --- // Header doc.setFillColor(primaryColor); // Use primary color for header background doc.rect(0, 0, pageWidth, 25, 'F'); // Draw filled rectangle for header doc.setFontSize(16); doc.setTextColor(buttonTextColor); // Use button text color (white) for header text doc.setFont(undefined, 'bold'); doc.text("Factory Pattern Result", pageWidth / 2, 15, { align: 'center' }); // Reset font for body doc.setFont(undefined, 'normal'); doc.setFontSize(12); doc.setTextColor(textColor); // Use standard text color currentY += 25; // Move below header // Content Section Title doc.setFontSize(14); doc.setFont(undefined, 'bold'); doc.setTextColor(primaryColor); // Use primary color for section titles doc.text("Generated Animal Details", 15, currentY); currentY += 8; // Reset font for content doc.setFontSize(12); doc.setFont(undefined, 'normal'); doc.setTextColor(textColor); // Add Selected Type doc.setFont(undefined, 'bold'); doc.text("Requested Type:", 15, currentY); doc.setFont(undefined, 'normal'); doc.setTextColor(secondaryColor); // Use secondary color for the value doc.text(currentAnimalType, 55, currentY); currentY += 7; // Add Details doc.setTextColor(textColor); // Reset to main text color doc.setFont(undefined, 'bold'); doc.text("Details:", 15, currentY); currentY += 7; // Add details text, potentially multi-line doc.setFont(undefined, 'normal'); doc.setTextColor(secondaryColor); // Use secondary color for the value // Use splitTextToSize for wrapping long detail strings const detailsLines = doc.splitTextToSize(currentAnimalDetails, pageWidth - 30); // 15 margin each side doc.text(detailsLines, 15, currentY); currentY += (detailsLines.length * 5) + 5; // Adjust Y based on number of lines // Footer (Optional) const footerText = `Generated on: ${new Date().toLocaleDateString('en-US')} ${new Date().toLocaleTimeString('en-US')}`; doc.setFontSize(10); doc.setTextColor(secondaryColor); // Use secondary color for footer doc.text(footerText, pageWidth / 2, pageHeight - 10, { align: 'center' }); // --- Save the PDF --- doc.save(`factory_pattern_${currentAnimalType.toLowerCase()}_result.pdf`); } catch (error) { console.error("Error generating PDF:", error); alert("An error occurred while generating the PDF. Please check the console for details."); outputDiv.innerHTML += `PDF Generation Error: ${error.message}
`; } }); // Initialize - Set initial state for download button downloadBtn.disabled = true;