다른 유형의 파트 (C#)를 사용하여 로봇을 빌드하는 프로그램에 대한 솔루션을 만들고 있습니다. 이 경우에는 몇 가지 특성을 공유하는 두 가지 유형이 있으며 두 가지 유형의 부분이 추상 클래스 Part에서 상속됩니다.매개 변수가있는 팩토리 메서드의 구현 수정
지금까지는 인터페이스 윈도우의 실제 버튼 코드에서 new를 호출하고있었습니다.
if (type == acuatic)
Part pieceA = new AcuaticPart(type,name,price,maxDepth);
else
Part pieceB = new TerrestrialPart(type,name,price,terrain,maxTemp);
저는 이것이 완전히 잘못된 디자인이며 Factory Method를 구현해야한다는 것을 알고 있습니다. 인터페이스 창에서
: 콘크리트 공장에서
Part piece = _partFactory.CreatePart(type,name,price,maxDepth,terrain,maxTemp);
:
public class ConcretePartFactory : PartFactory
{
public override Part CreatePart(type,name,price,maxDepth,terrain,maxTemp)
{
Part myPart = default(Part);
switch (type)
{
case "actuatic":
myPart = new AquaticPart(type,name,price,maxDepth);
break;
case "terrestrial":
myPart = new TerrestrialPart(type,name,price,terrain,maxTemp);
break;
}
return myPart;
}
}
것은 그냥 이런 공장으로 모든 매개 변수를 전송 할 수있는 상태인지 모르겠어요입니다
문제는 여전히 모든 속성을 전달해야하며 이것이 공개/종결 된 원칙을 따르지 않는다고 생각합니다.이 문제를 해결하려면 어떻게해야합니까? 감사!
assignign myPart = default (myPart)는 case 문 기본값에 있어야합니다. – CSharpie