How do I create single object that changes functionality based on programmer input?

I have several classes that all behave according to one interface. But in each class there are additional functions defined that give each class slightly different behavior. What I want to do is have th programmer using this API to simply create one instance based on some parameter which generates the correct class type. And then he/she would only see the methods available to that type while programming

So for example

My interface

interface IChange
{
    string Change(string inputString);
}

And say I have two classes that implement this interface

class Changer1 : IChange
{

  public string Change(string input){

  ...
  }

  public void DoSomethingSpecial1(){

  ///code that does something specific to Changer1 class
  }

}

class Changer2 : IChange{

  public string Change(string input){

  ...
  }

  public void DoSomethingSpecial2(){

  ///code that does something specific to Changer2 class
  }


}

Then I would have a wrapper that sets the appropriate Changer based on an enumeration or some other user input.

public enum ChangerType
{
    Type1,
    Type2
}

class MasterObject{

        public MasterObject(ChangerType type)
        {
            if (type == ChangerType.Type1)
                SetChanger(new Changer1());
            if (type == ChangerType.Type2)
                SetChanger(new Changer2());
        }

        private IChange _change;

        private void SetChanger(IChange change)
        {
            this._change = change;
        }


}

Then as a user of this, in my main program I want to do something like the following where the user creates the main object but it gets created in such a way that they only have access to the appropriate functions. I.e. Have available only DoSomethingSpecial1 or only DoSomethingSpecial2 based on the type you created.

class Program
{
    static void Main(string[] args)
    {
        MasterObject obj1 = new MasterObject(ChangerType.Type1);
        obj1.DoSomethingSpecial1();
        string result1 = obj1.Change("test");

        /// OR

        MasterObject obj2 = new MasterObject(ChangerType.Type2);
        obj2.DoSomethingSpecial2();
        string result2 = obj2.Change("test");

    }

}

I have a feeling this is Factory type pattern or maybe Abstract factory, but I’m not sure how to fix my code to implement it correctly.

Is there a reason why the “some parameter” that the programmer using the API has to supply can not be the name of the class. They could instantiate an object of Changer1 or Changer2. They are only going to have to remember a third “MasterObject” class name and the parameter “Type1” and “Type2” anyway. And any methods that require you to pass in a parameter of either object can use the interface class.

1

You are doing Factory pattern here with “MasterObject”. However, if it is the correct way depends on how this is used. Generally, with the Factory class your user is providing some information and the code is selecting the correct class to use to represent the information. You are very close on this point. However, your MasterObject is wrong in that it is storing the implementing class in itself and returning itself. Generally, the factory is only to receive arguments and then select the best object class to return to you. In your case the object you should be getting back is Changer1 or Changer2. I noticed that you are handling the object type correctly using the interface name as the type.

Now, you have a instance of Changer1 or Changer2, which implements the common interface which is good, along with supporting methods that are unique to the class (such as the example of classic “bark” or “wag_tail” methods).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật