From d738580b7c942b1b364a93c0722f26d7f95314c4 Mon Sep 17 00:00:00 2001 From: bavly morcos Date: Fri, 19 Jun 2020 22:40:18 +0200 Subject: [PATCH] Documentation of the code --- README.md | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 43e497f..7383b5f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,83 @@ # Replace-Conditional-with-Polymorphism -just to talk about refactoring of code + + +● just to talk about refactoring of code + +● If we have a class full of Male & female students ,they went to pee we want to present that in a code. + it will be something like that. + +## Example using switch case: + +``` +class Human { + // ... + String goPee() { + switch (typeOfPee) { + case Male: + return "He should standup"; + case Female: + return "She should Sitdown"; + } + } +} +``` + +● To refoctor this code correctly think of it like we have more than ONE if condition, In real life applications use + more than 8 conditions, I have seen this before. + +● It was very hard to understand every if condition. + +● To reduce the code & make it maintianble, we could refactor it. + +## First create abstract class called human + +``` +abstract class Human { + // ... + abstract String goPee(); +} +``` + +In this abstract method when you want to use it, you must inherate it from class Human then use it, like that + +## Create class for Male + +``` +public class Male extends Human{ +... + @Override + public String goPee(){ + return ("Stand Up"); + } +} +``` +## class for Female + +``` +public class Female extends Human{ + @Override + public void goPee(){ + System.out.println("Sit Down"); + } +} +``` + +To check it for all the classroom + +## Main Method + +``` +public static void main(String[] args){ + ArrayList group = new ArrayList(); + group.add(new Male()); + group.add(new Female()); + // ... add more... + + // tell the class to take a pee break + for (Human person : group) person.goPee(); +} + +``` + +Please if someone wants to modify it, very welecome. +