Implement a design pattern into the following code that isn\'t the singleton or
ID: 3717724 • Letter: I
Question
Implement a design pattern into the following code that isn't the singleton or observer patterns. Why would you use the pattern you chose?
C# code:
using UnityEngine;
public class HeatSeekingMissile : Projectile
{
private GameObject closetMissle;
private Transform target;
public string searchTag;
private new void Start()
{
closetMissle = FindClosestEnemy();
if (closetMissle)
target = closetMissle.transform;
}
private void Update()
{
transform.LookAt(target);
transform.Translate(Vector3.forward * 5.0f * Time.deltaTime);
}
private GameObject FindClosestEnemy()
{
GameObject[] gos;
gos = GameObject.FindGameObjectsWithTag(searchTag);
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject go in gos)
{
Vector3 diff = go.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
}
Explanation / Answer
The pattern that can be used here is AbstractFactory.
The calss Projectile can be made an abstract class with pure virtual function:
The reason of using this pattern is to ensure that there can be more classes like Missile which implements the Projectile and provide their own algorithms.
Project in itself is abstract and that there cannot be a implementation of its own.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.