One thing you could do is to create a common function that contains the DoX(), if(value), and DoY() logic, and takes a delegate as a parameter. That way you have the common logic encapsulated in one place but can still specify the piece specific to each method. For example:
//making value a property so it's defined somewhere :)
public bool Value { get; set; }
//defining all of the methods called above
public string GetA() { return ""; }
public string GetB() { return ""; }
public void DoA1() { }
public void DoA2(string input) { }
public void DoB(string input) { }
public void DoX() { }
public void DoY() { }
//this delegate will hold the part of the original Method1/Method2 that is inside the if statement
public delegate void DoDelegate();
//this is the part inside the if statement of the original Method1
public void Method1Specific() {
var input = GetA();
DoA1();
DoA2(input);
}
//this is the part inside the if statement of the original Method2
public void Method2Specific() {
var input = GetB();
DoB(input);
}
//this is the common part of both methods
//it takes a parameter indicating what to run for the specific part inside the if statement
public void Method(DoDelegate Do) {
DoX();
if (Value) {
Do();
}
DoY();
}
//this would replace the existing Method1
public void Method1() {
Value = true;
Method(Method1Specific);
}
//this would replace the existing Method2
public void Method2() {
Value = false;
Method(Method2Specific);
}
I commented this code to show what is actually happening here.
In this code, you will still call Method1 and Method2, just like before, but each of those methods just calls the Method method passing in the method appropriate for each. To extend this for more methods, just create (for example) a Method3Specific method that would do the logic specific to the new method, and then create a Method3 that passes in Method3Specific as the delegate. As you can see, once this structure is set up, it is easy to extend it to any number of methods.
It's okay to copy once, if you copy twice it is time to refactor.
\$\endgroup\$