public interface IColorDemo { IColorDemo Clone(); int Red { get; set; } int Green { get; set; } Quote quoteInstance{get;set} } publc class Quote{}
2.具体类的实现代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public class RedColor : IColorDemo { private int red; private int green; Quote quoteInstance{get;set} public int Red { get { return this.red; } set { this.red = value; } } public int Green { get { return this.green; } set { this.green = value; } } }
//浅复制
public Override IColorDemo Clone() { return (IColorDemo)this.MemberwiseClone(); } //深复制,获取浅表副本的同时复制引用。
1 2 3 4 5 6 7
public Override IColorDemo Clone() { IColorDemo color = this.MemberwiseClone(); Quote q = new Quote(); color.quoteIntance = q; return color; }
public class VegBurger extends Burger { public float price() { return 25.0f; } public String name() { return "Veg Burger"; } }
public class ChickenBurger extends Burger { public float price() { return 50.5f; } public String name() { return "Chicken Burger";} } public class Coke extends ColdDrink { public float price() { return 30.0f;} public String name() { return "Coke";} }
public class Pepsi extends ColdDrink { public float price() {return 35.0f; } public String name() { return "Pepsi";} }
5.建一个 Meal 类,带有上面定义的 Item 对象,类似定义一餐所包含的类目。
1 2 3 4 5 6 7 8 9 10 11 12
public class Meal { private List<Item> items = new ArrayList<Item>(); public void addItem(Item item){ items.add(item); }
public interface Shape { void draw(); } public class Rectangle: Shape { public void draw() { System.out.println("Inside Rectangle::draw() method."); } } public class Square: Shape { public void draw() { System.out.println("Inside Square::draw() method."); } }
2.为颜色创建接口,创建实现接口的实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
public interface Color { void draw(); } public class Red: Color { public void draw() { System.out.println("Inside Red::draw() method."); } } public class Green: Color { public void draw() { System.out.println("Inside Green::draw() method."); } }
3.为 Color 和 Shape 对象创建抽象类来获取工厂。AbstractFactory
public abstract class AbstractFactory { abstract Color getColor(String color); abstract Shape getShape(String shape) ; } 4.创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。
public class Rectangle: Shape { public void draw() { System.out.println("Inside Rectangle::draw() method."); } } public class Square: Shape { public void draw() { System.out.println("Inside Square::draw() method."); } }
public class Son{ //关联关系中作为成员变量的类一般会在类中赋值 Father father = new Father(); public void getGift(){ System.out.println(“从”+father.getName()+”获得礼物”); } } public class Father{ Son son = new Son(); public void giveGift(){ System.out.println(“送给”+son.getName()+“礼物”); } }
public class People{ Car car; House house; //聚合关系中作为成员变量的类一般使用set方法赋值 public void setCar(Car car){ This.car = car; } public void setHouse(House house){ This.house = house; } public void driver(){ System.out.println(“车的型号:”+car.getType()); } public void sleep(){ System.out.println(“我在房子里睡觉:”+house.getAddress()); } }
Public class People{ Soul soul; Body body; //组合关系中的成员变量一般会在构造方法中赋值 Public People(Soul soul, Body body){ This.soul = soul; This.body = body; } Public void study(){ System.out.println(“学习要用灵魂”+soul.getName()); } Public void eat(){ System.out.println(“吃饭用身体:”+body.getName()); } }