Hi,
I'm trying to create a common class for creating parties
As you can see i made a PartyCreatorFactory that detemines whether to call the PersonPartyCreator or OrganiztionPartyCreator.
I also created IPartyCreator interface.
but as you can see the fields needed in person are different than the fields needed in organization so I had to use object field... what i did is working fine but my question is what is the best way to do it, because i feel like using an object in both classes then checking the type is not the good approach.. so was using interfaces in this case wrong? how would you guys implement it?
class Party
{
public PartyNumber createCustomerParty(CustomerReqContract _custDetails)
{
DictEnum dictEnum = new DictEnum(enumNum(DirPartyType));
IPartyCreator partyCreator = PartyCreatorFactory::createParty(dictEnum.symbol2Value(_custDetails.Customer().DirPartyType()));
if(partyCreator is PersonPartyCreator)
{
return partyCreator.createParty(_custDetails.PersonDetails()).PartyNumber;
}
else
{
return partyCreator.createParty(_custDetails.OrgDetails()).PartyNumber;
}
}
}class PartyCreatorFactory
{
public static IPartyCreator createParty(DirPartyType _dirPartyType)
{
if(_dirPartyType == DirPartyType::Person)
{
return new PersonPartyCreator();
}
else if(_dirPartyType == DirPartyType::Organization)
{
return new OrganizationPartyCreator();
}
else
{
throw error('Invalid DirPartyType');
}
}
}public interface IPartyCreator
{
public DirPartyBaseEntity createParty(Object _partyDetails)
{
}
}class PersonPartyCreator implements IPartyCreator
{
public DirPartyBaseEntity createParty(Object _personDetailsContract)
{
DirPartyBaseEntity dirPartyBaseEntity;
if(_personDetailsContract is PersonDetailsContract)
{
PersonDetailsContract custPersonDetails = _personDetailsContract;
if(custPersonDetails)
{
//create Customer details based on DirPartyType which is Person
dirPartyBaseEntity.initValue();
dirPartyBaseEntity.PartyType = enum2Str(DirPartyType::Person);
dirPartyBaseEntity.PersonFirstName = custPersonDetails.FirstName();
dirPartyBaseEntity.PersonMiddleName = custPersonDetails.MiddleName();
dirPartyBaseEntity.PersonLastName = custPersonDetails.LastName();
dirPartyBaseEntity.PersonPersonalTitle = custPersonDetails.PersonalTitle();
RecId NameSequenceRecId = DirNameSequence::find(custPersonDetails.NameSequence()).RecId;
if(NameSequenceRecId != 0)
{
dirPartyBaseEntity.NameSequence = NameSequenceRecId;
}
if(custPersonDetails.BirthDate())
{
dirPartyBaseEntity.PersonBirthDay = custPersonDetails.BirthDate().BirthDay();
dirPartyBaseEntity.PersonBirthMonth = custPersonDetails.BirthDate().BirthMonth();
dirPartyBaseEntity.PersonBirthYear = custPersonDetails.BirthDate().BirthYear();
}
dirPartyBaseEntity.insert();
}
}
return dirPartyBaseEntity;
}
}class OrganizationPartyCreator implements IPartyCreator
{
public DirPartyBaseEntity createParty(Object _orgDetailsContract)
{
DirPartyBaseEntity dirPartyBaseEntity;
if(_orgDetailsContract is OrganizationDetailsContract)
{
OrganizationDetailsContract orgDetails = _orgDetailsContract;
if(orgDetails)
{
dirPartyBaseEntity.Name = orgDetails.PartyName();
dirPartyBaseEntity.PartyType = enum2Str(DirPartyType::Organization);
dirPartyBaseEntity.OrganizationNumber = orgDetails.OrgNumber();
dirPartyBaseEntity.OrganizationNumOfEmployees = orgDetails.NumberOfEmployees();
dirPartyBaseEntity.insert();
}
}
return dirPartyBaseEntity;
}
}