设计模式

设计模式

2017-09-14  本文已影响17人  Ludwigvan

1.对象模式三剑客:建设者模式  工厂模式  抽象工厂模式

1、建设者模式:

public class User {

private String name;

private static int test=1;

private String pwd;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPwd() {

return pwd;

}

public void setPwd(String pwd) {

this.pwd = pwd;

}

private User(String name,String pwd){

this.name = name;

this.pwd = pwd;

}

@Override

public String toString() {

return test+name+pwd;

}

public static class UserBuilder{

private String name;

private String pwd;

public UserBuilder setName(String name) {

this.name = name;

return this;

}

public UserBuilder setPwd(String pwd) {

this.pwd = pwd;

return this;

}

public User build(){

System.out.println(toString());

return new User(name,pwd);

}

@Override

public String toString() {

return test+name+pwd;

}

}

}

链式调用:

User user =new User.UserBuilder().setName("ss").setPwd(".").build();

user.toString();

2、工厂模式:

Meal fruit = Meal.valueOf("banana");

Meal vegetable = Meal.valueOf("carrot");

class Meal {

private String type;

public Meal(String type) {

this.type = type;

}

public String getType() {

return this.type;

}

// Example of factory method - different object is created depending on current context

public static Meal valueOf(String ingredient) {

if (ingredient.equals("banana")) {

return new Meal("fruit");

}

return new Meal("vegetable");

}

}

3.抽象工厂:

public class FactoryTest {

// Test method which is the client

@Test

public void test() {

Kitchen factory = new KitchenFactory();

KitchenMeal meal = factory.getMeal("P.1");

KitchenMeal dessert = factory.getDessert("I.1");

assertTrue("Meal's name should be 'protein meal' and was '"+meal.getName()+"'", meal.getName().equals("protein meal"));

assertTrue("Dessert's name should be 'ice-cream' and was '"+dessert.getName()+"'", dessert.getName().equals("ice-cream"));

}

}

// abstract factory

abstract class Kitchen {

public abstract KitchenMeal getMeal(String preferency);

public abstract KitchenMeal getDessert(String preferency);

}

// concrete factory

class KitchenFactory extends Kitchen {

@Override

public KitchenMeal getMeal(String preferency) {

if (preferency.equals("F.1")) {

return new FastFoodMeal();

} else if (preferency.equals("P.1")) {

return new ProteinMeal();

}

return new VegetarianMeal();

}

@Override

public KitchenMeal getDessert(String preferency) {

if (preferency.equals("I.1")) {

return new IceCreamMeal();

}

return null;

}

}

// abstract product

abstract class KitchenMeal {

public abstract String getName();

}

// concrete products

class ProteinMeal extends KitchenMeal {

@Override

public String getName() {

return "protein meal";

}

}

class VegetarianMeal extends KitchenMeal {

@Override

public String getName() {

return "vegetarian meal";

}

}

class FastFoodMeal extends KitchenMeal {

@Override

public String getName() {

return "fast-food meal";

}

}

class IceCreamMeal extends KitchenMeal {

@Override

public String getName() {

return "ice-cream";

}

}

上一篇下一篇

猜你喜欢

热点阅读