Created
December 29, 2019 01:34
-
-
Save AlaaAttya/ef54ef566cb771687384443f02f001b8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package payment; | |
interface PaymentProvider{ | |
public void pay(); | |
} | |
class Balea implements PaymentProvider{ | |
@Override | |
public void pay() { | |
// TODO Auto-generated method stub | |
System.out.println("Pay baela"); | |
} | |
} | |
class CreditCard implements PaymentProvider{ | |
@Override | |
public void pay() { | |
// TODO Auto-generated method stub | |
System.out.println("Pay CreditCard"); | |
} | |
} | |
class Order { | |
private PaymentProvider obj; | |
public Order(PaymentProvider obj) { | |
this.obj = obj; | |
} | |
public void create() { | |
this.obj.pay(); | |
} | |
} | |
class PaymentProviderFactory { | |
public PaymentProvider getProvider(String country) throws Exception { | |
switch (country) { | |
case "EG": | |
return new Balea(); | |
case "AU": | |
return new CreditCard(); | |
} | |
throw new Exception("hello"); | |
} | |
} | |
public class main { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
PaymentProviderFactory factory = new PaymentProviderFactory(); | |
PaymentProvider bb = null; | |
try { | |
bb = factory.getProvider("AU"); | |
} catch (Exception e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
}; | |
Order orderObj = new Order(bb); | |
PaymentProvider cc = new CreditCard(); | |
// Order orderObjCC = new Order(cc); | |
// orderObj.create(); | |
orderObj.create(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment