CC150 chapter 8 OOD object-oriented design

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.



你可能感兴趣的:(cc150,OOP)