diff --git a/.classpath b/.classpath deleted file mode 100644 index 63b7e89..0000000 --- a/.classpath +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/.project b/.project deleted file mode 100644 index 1ea1adf..0000000 --- a/.project +++ /dev/null @@ -1,17 +0,0 @@ - - - JavaDesignPattern - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index bb35fa0..0000000 --- a/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,11 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 -org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.8 -org.eclipse.jdt.core.compiler.debug.lineNumber=generate -org.eclipse.jdt.core.compiler.debug.localVariable=generate -org.eclipse.jdt.core.compiler.debug.sourceFile=generate -org.eclipse.jdt.core.compiler.problem.assertIdentifier=error -org.eclipse.jdt.core.compiler.problem.enumIdentifier=error -org.eclipse.jdt.core.compiler.source=1.8 diff --git a/src/com/anxpp/designpattern/abstractfactory/AbstractFactory.java b/src/com/anxpp/designpattern/abstractfactory/AbstractFactory.java index 75dc28c..24cbfea 100644 --- a/src/com/anxpp/designpattern/abstractfactory/AbstractFactory.java +++ b/src/com/anxpp/designpattern/abstractfactory/AbstractFactory.java @@ -1,72 +1,85 @@ -package com.anxpp.designpattern.abstractfactory; -//抽象工厂模式 -public class AbstractFactory { - public static void main(String args[]){ - IFactory bigfactory = new BigFactory(); - IFactory smallfactory = new BigFactory(); - bigfactory.producePhone().run(); - bigfactory.produceHeadset().play(); - smallfactory.producePhone().run(); - smallfactory.produceHeadset().play(); - } -} -//抽象产品*2 -interface Headset{ - void play(); -} -//抽象产品 -interface MeizuPhone{ - void run(); -} -//具体产品*2*2 -class PRO5 implements MeizuPhone{ - @Override - public void run() { - System.out.println("我是一台PRO5"); - } -} -class MX5 implements MeizuPhone{ - @Override - public void run() { - System.out.println("我是一台MX5"); - } -} -class EP21 implements Headset{ - @Override - public void play() { - System.out.println("我是一副EP21"); - } -} -class EP30 implements Headset{ - @Override - public void play() { - System.out.println("我是一台EP30"); - } -} -//抽象工厂 -interface IFactory{ - MeizuPhone producePhone(); - Headset produceHeadset(); -} -//具体工厂*2 -class BigFactory implements IFactory{ - @Override - public MeizuPhone producePhone() { - return new PRO5(); - } - @Override - public Headset produceHeadset() { - return new EP30(); - } -} -//具体工厂*2 -class SmallFactory implements IFactory{ - @Override - public MeizuPhone producePhone() { - return new MX5(); - } - @Override - public Headset produceHeadset() { - return new EP21(); - } +package com.anxpp.designpattern.abstractfactory; + +//抽象工厂模式 +public class AbstractFactory { + public static void main(String args[]) { + IFactory bigfactory = new BigFactory(); + IFactory smallfactory = new BigFactory(); + bigfactory.producePhone().run(); + bigfactory.produceHeadset().play(); + smallfactory.producePhone().run(); + smallfactory.produceHeadset().play(); + } +} + +//抽象产品*2 +interface Headset { + void play(); +} + +//抽象产品 +interface MeizuPhone { + void run(); +} + +//具体产品*2*2 +class PRO5 implements MeizuPhone { + @Override + public void run() { + System.out.println("我是一台PRO5"); + } +} + +class MX5 implements MeizuPhone { + @Override + public void run() { + System.out.println("我是一台MX5"); + } +} + +class EP21 implements Headset { + @Override + public void play() { + System.out.println("我是一副EP21"); + } +} + +class EP30 implements Headset { + @Override + public void play() { + System.out.println("我是一台EP30"); + } +} + +//抽象工厂 +interface IFactory { + MeizuPhone producePhone(); + + Headset produceHeadset(); +} + +//具体工厂*2 +class BigFactory implements IFactory { + @Override + public MeizuPhone producePhone() { + return new PRO5(); + } + + @Override + public Headset produceHeadset() { + return new EP30(); + } +} + +//具体工厂*2 +class SmallFactory implements IFactory { + @Override + public MeizuPhone producePhone() { + return new MX5(); + } + + @Override + public Headset produceHeadset() { + return new EP21(); + } } \ No newline at end of file diff --git a/src/com/anxpp/designpattern/builder/DateBuilder1.java b/src/com/anxpp/designpattern/builder/DateBuilder1.java deleted file mode 100644 index 5ff5088..0000000 --- a/src/com/anxpp/designpattern/builder/DateBuilder1.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.anxpp.designpattern.builder; -//具体生成器 -public class DateBuilder1 implements IDateBuilder{ - private MyDate myDate; - public DateBuilder1(MyDate myDate){ - this.myDate = myDate; - } - @Override - public IDateBuilder buildDate(int y, int m, int d) { - myDate.date = y+"-"+m+"-"+d; - return this; - } - @Override - public String date() { - return myDate.date; - } -} diff --git a/src/com/anxpp/designpattern/builder/DateBuilder2.java b/src/com/anxpp/designpattern/builder/DateBuilder2.java deleted file mode 100644 index f32d4e7..0000000 --- a/src/com/anxpp/designpattern/builder/DateBuilder2.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.anxpp.designpattern.builder; -//具体生成器 -public class DateBuilder2 implements IDateBuilder{ - private MyDate myDate; - public DateBuilder2(MyDate myDate){ - this.myDate = myDate; - } - @Override - public IDateBuilder buildDate(int y, int m, int d) { - myDate.date = y+" "+m+" "+d; - return this; - } - @Override - public String date() { - return myDate.date; - } -} diff --git a/src/com/anxpp/designpattern/builder/Derector.java b/src/com/anxpp/designpattern/builder/Derector.java deleted file mode 100644 index b665d11..0000000 --- a/src/com/anxpp/designpattern/builder/Derector.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.anxpp.designpattern.builder; -//指挥者 -public class Derector { - private IDateBuilder builder; - public Derector(IDateBuilder builder){ - this.builder = builder; - } - public String getDate(int y,int m,int d){ - builder.buildDate(y, m, d); - return builder.date(); - } -} diff --git a/src/com/anxpp/designpattern/builder/IDateBuilder.java b/src/com/anxpp/designpattern/builder/IDateBuilder.java deleted file mode 100644 index bc08fbf..0000000 --- a/src/com/anxpp/designpattern/builder/IDateBuilder.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.anxpp.designpattern.builder; -//抽象生成器 -public interface IDateBuilder { - IDateBuilder buildDate(int y,int m,int d); - String date(); -} diff --git a/src/com/anxpp/designpattern/builder/MyDate.java b/src/com/anxpp/designpattern/builder/MyDate.java deleted file mode 100644 index 75d656c..0000000 --- a/src/com/anxpp/designpattern/builder/MyDate.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.anxpp.designpattern.builder; -//产品 -public class MyDate { - String date; -} diff --git a/src/com/anxpp/designpattern/builder/Test.java b/src/com/anxpp/designpattern/builder/Test.java new file mode 100644 index 0000000..bd42473 --- /dev/null +++ b/src/com/anxpp/designpattern/builder/Test.java @@ -0,0 +1,125 @@ +package com.anxpp.designpattern.builder; + +/** + * ģʽʵǰĹ setterнųĹ࣬ÿδʱֱʹöӦĹ࣬ + * Ϳ԰ԤƵIJõ¹ĶȥˣʡȥʹsetterȥһһԵĹʵģʽ; + */ +public class Test { + //ʵ + public static void main(String[] args) { + Waiter waiter = new Waiter(); + //ʵ1 + PizzaBuilder hawaiian_pizzabuilder = new HawaiianPizzaBuilder(); + waiter.setPizzaBuilder(hawaiian_pizzabuilder); + waiter.constructPizza(); + Pizza pizza = waiter.getPizza(); + System.out.println(pizza.toString()); + //ʵ2 + PizzaBuilder spicy_pizzabuilder = new SpicyPizzaBuilder(); + waiter.setPizzaBuilder(spicy_pizzabuilder); + waiter.constructPizza(); + pizza = waiter.getPizza(); + System.out.println(pizza.toString()); + } +} + +//Pizza +class Pizza { + // + private String dough = ""; + // + private String sauce = ""; + // + private String topping = ""; + + public void setDough(String dough) { + this.dough = dough; + } + + public void setSauce(String sauce) { + this.sauce = sauce; + } + + public void setTopping(String topping) { + this.topping = topping; + } + + @Override + public String toString() { + return "Pizza{" + + "dough='" + dough + '\'' + + ", sauce='" + sauce + '\'' + + ", topping='" + topping + '\'' + + '}'; + } +} + + +//pizza +abstract class PizzaBuilder { + protected Pizza pizza; + + public Pizza getPizza() { + return pizza; + } + + public void createNewPizzaProduct() { + pizza = new Pizza(); + } + + public abstract void buildDough(); + + public abstract void buildSauce(); + + public abstract void buildTopping(); +} + +//pizza +class HawaiianPizzaBuilder extends PizzaBuilder { + public void buildDough() { + pizza.setDough("cross"); + } + + public void buildSauce() { + pizza.setSauce("mild"); + } + + public void buildTopping() { + pizza.setTopping("ham+pineapple"); + } +} + + +class SpicyPizzaBuilder extends PizzaBuilder { + public void buildDough() { + pizza.setDough("pan baked"); + } + + public void buildSauce() { + pizza.setSauce("hot"); + } + + public void buildTopping() { + pizza.setTopping("pepperoni+salami"); + } +} + +//pizza +class Waiter { + private PizzaBuilder pizzaBuilder; + + public void setPizzaBuilder(PizzaBuilder pb) { + pizzaBuilder = pb; + } + + public Pizza getPizza() { + return pizzaBuilder.getPizza(); + } + + public void constructPizza() { + pizzaBuilder.createNewPizzaProduct(); + pizzaBuilder.buildDough(); + pizzaBuilder.buildSauce(); + pizzaBuilder.buildTopping(); + } +} diff --git a/src/com/anxpp/designpattern/builder/TestUse.java b/src/com/anxpp/designpattern/builder/TestUse.java deleted file mode 100644 index d9290c3..0000000 --- a/src/com/anxpp/designpattern/builder/TestUse.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.anxpp.designpattern.builder; -public class TestUse { - public static void main(String args[]){ - MyDate date = new MyDate(); - IDateBuilder builder; - builder = new DateBuilder1(date).buildDate(2066, 3, 5); - System.out.println(builder.date()); - builder = new DateBuilder2(date).buildDate(2066, 3, 5); - System.out.println(builder.date()); - } -} \ No newline at end of file