lundi 26 juin 2017

Factory pattern with shared functions

I am trying to create some sort of factory pattern. However the thing I am stuck on, is that I need to call on some methods in each of the objects created by the factory. I wanna try to prevent this because it removes the single point of responsibility thingie.

My code looks like this:

class ActivityFactory : IActivityFactory
{
    public IActivityType CreateActivity(ActivityType type)
    {
        IActivityType activityType;
        switch (type)
        {
            case ActivityType.Customer:
                activityType = new CustomerActivity();
                break;
            case ActivityType.Lead:
                activityType = new LeadActivity();
                break;
            case ActivityType.Opportunity:
                activityType = new OpportunityActivity();
                break;
            case ActivityType.Appointment:
                activityType = new AppointmentActivity();
                break;
            default:
                throw new ArgumentException("Unsupported activity type.");
        }

        return activityType;
    }
}

            public ProcessingStatus SendActivity()
    {
        GetContactPersons();
        GetCustomers();
        EditContactPerson();
        SaveCustomer();
        SaveContactPerson();
        SaveCustomerContactPersonCombination();

        return ProcessingStatus.Succeed;
    }

    private void SaveCustomerContactPersonCombination()
    {

    }

    private void SaveContactPerson()
    {

    }

    private void SaveCustomer()
    {

    }

    private void EditContactPerson()
    {

    }

    private void GetCustomers()
    {

    }

    private void GetContactPersons()
    {

    }

Where the part starting at GetContactPersons() till SaveCustomerCombination is basically always the same, besides that they are talking with a different part of a webapplication.

Which is also the reason that I can't just put those methods before the factory even creates the objects.

For example: if I choose to create a Lead the getcontactpersons method will talk to url example/contactperson1 and when I choose customer it will talke to url example/contactperson2. The reason why I can't put those methods together is because of the different methods and objects they use provided by a third party.

Any ideas for a nice and pretty solution? Also thought about using a different design pattern. Just couldn't find a nice one for this problem.

Aucun commentaire:

Enregistrer un commentaire