Created
July 15, 2017 05:54
-
-
Save vnnvanhuong/f2568c4cc5dda22a421954d50019f0ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Which the same way to solve a problem | |
//Template Method uses Inheritance | |
//Strategy uses Composition/Delegation | |
abstract class TemplateMethod{ | |
public final void execute() { | |
doFirstThing(); | |
doSecondthing(); | |
} | |
//modifier can be public, private, protected | |
abstract void doFirstThing(); | |
abstract void doSecondthing(); | |
} | |
class FirstConcreateTemplateMethod extends TemplateMethod{ | |
@Override | |
void doFirstThing() {} | |
@Override | |
void doSecondthing() {} | |
} | |
class SecondConcreateTemplateMethod extends TemplateMethod{ | |
@Override | |
void doFirstThing() {} | |
@Override | |
void doSecondthing() {} | |
} | |
class Strategy{ | |
private Strategable strategable; //the concrete implementation is interchangeable | |
public void execute() { | |
strategable.doFirstThing(); | |
strategable.doSecondthing(); | |
} | |
} | |
interface Strategable{ | |
//modifier always is public | |
void doFirstThing(); | |
void doSecondthing(); | |
} | |
class FirstConcreteStrategy implements Strategable{ | |
@Override | |
public void doFirstThing() {} | |
@Override | |
public void doSecondthing() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment