CC150 chapter 8 OOD object-oriented design

本文介绍了设计模式中的工厂模式和单例模式,通过实例展示了如何创建不同类型的对象并确保类只有一个实例。此外,还简要提及了建造者模式和适配器模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

OOD is quite difficult, especially for the green programmer like me.

When I go to tutorial here 

http://www.javaworld.com/article/2073352/core-java/simply-singleton.html

I find there are several design patterns here to help programmer to design better pattern to make their code more fludent.


The easist ones are factor pattern and the sigleton pattern

And the factor pattern is that we need to create a object based on different parameters.

for exmple apple and orange are all fruites.

可能写的不对

//The interface just provide the method
public interface food{
	public int getPrice()
	public food(int price);
}
public class apple implements food {
	Color color;
	public apple(int price, Color green){
		super(price);
		this.color = green;	
	}
}	
public class orange implements food{
	Color color;
	public organe(int price){
		super(price);
		this.color = orange;
	}
}
public class foodFactor{
	public Food getFood(String type){
		if(type == "apple")
			return new apple(15);
		else if(typle == "orange")
			return new orange(15);	
	}
}


public class SingletonPatternDemo {
   public static void main(String[] args) {

      //illegal construct
      //Compile Time Error: The constructor SingleObject() is not visible
      //SingleObject object = new SingleObject();

      //Get the only object available
      SingleObject object = SingleObject.getInstance();

      //show the message
      object.showMessage();
   }
}


I also go to the builder and adapter pattern. They are kind of complex.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值