用Java是一件不算困难而且颇具回报的事情,笔者对此就是有一点亲身经历的,虽说在别的方面(例如在C的指针优势方面)Java是由不足,而且就效率来说,一般也是不算高,但是在帮助社区文档方面,笔者还是觉着Java做的确实很好,所以说现在看着Java的新的包的帮助文档就像在阅读一首诗一般美妙。
好了,闲话不多说,最近自己学习Java设计模式,就之前的编程来说,很少会去想到要重复利用的,可是最近越来越觉得Java中这一点倒是很重要的,首先,比如最近用的语音功能,就可以打包成一个jar文件,然后在下一次用的时候就只需要一个简单的import就可以了,而不是传统的复制代码,粘贴这么麻烦。
虽说编写Java程序有了好长的时间,但是真正去用到Java设计的思想还是不多,而现在眼前的23种,23种!设计模式让我大开眼界。
首先,创建product接口,可以定义一个方法,但是其实现是交给使用接口者使用的,这更像是一种策略,告诉你要做啥,怎么做是你自己的事。
而工厂的实现,就可以根据上面的描述进行,只不过product是一个工厂,在工厂中制定的方法就是生产,即得到要生产的物件,然后进行具体的生产。
*roduct
public interface Work {
void doWork();
}
ConcreteProduct
public class StudentWork implements Work {
public void doWork() {
System.out.println("学生*作业!");
}
}
public class TeacherWork implements Work {
public void doWork() {
System.out.println("老师审批作业!");
}
}
Creator
public interface IWorkFactory {
Work get*ork();
}
Concre*eCreator
pu*lic class StudentWorkFactory implements IWorkFactory {
public Work getWork() {
*eturn new StudentWork();
}
}
public class TeacherWorkFactory implements IWorkFactory {
public Work getWork() {
return new TeacherWork();
}
}
Test
public class Test {
public static void m*in(Strin*[] args) {
IWorkFactory studentWorkFactory = new StudentWorkFactory();
studentWorkFactory.getWork().d*Work();
IWorkFactory teacherWorkFactory * new TeacherWorkFactory();
teacherWorkFactory.g*tWork().*oWork();
}
}
result
学生做作业!
老师审批作业!
在工厂模式中还有一个是抽象工厂模式,抽象工厂更像是对工厂进行分类,可以有不同的类型的工厂,这样还是生产物件,但是不同的工厂生产的东西自然会有一些不同了。
例子代码如下:(白猫,黑猫)
public interface IAn*malFactory {
ICat createCat();
IDog cre*teDog();
}
ConcreteFactory
p*blic class BlackAnimalFactory implem*nts IAnimalFactory {
public ICat createCat() {
retur* new BlackCat();
}
public IDog createDog() {
return new BlackDog();
}
}
public class WhiteAnimalFac*ory imp*ements IAnimalFactory {
public ICat createCat() {
return new WhiteCat();
}
public IDog cre*teDog() {
return new WhiteDog();
}
}
Abstrac*Product
public interface ICat {
void eat();
}
public interface IDog {
void eat();
}
Concrete*roduct
public class Black*at implements ICat {
public void eat() {
System.out.println("The bl*ck cat is eating!");
}
}
public class WhiteCat implements *Cat {
public void eat() {
Sy*tem.out.prin*ln("The w*ite cat is eating!*);
}
}
public class BlackDog implements IDog {
public void eat() {
System.out.println("The black dog is eating");
}
}
public class WhiteDog implements IDog {
public void eat() {
System.out.println("The white dog is eat*ng!");
}
}
Client
public static void main(String[] args) {
IAnimalFactory blackAnimalFa*tory = new BlackAnimalFactory();
ICat blackCat = blackAnimalFactory.createCat();
blackCat.eat();
IDog blackD*g = blackAnimalFactory.createDog();
blackDog.eat();
IAnimalFactory whiteAnimalF*ctory = new WhiteAnimalFactory();
ICat whiteCat = whiteAnimalFactory.createCat();
whiteCat.eat();
IDog *hiteDog = whiteAnimalFactory.createDog();
whiteDog.eat();
}
res*lt
The bla*k cat is eating!
Th* black dog is eatin*!
The white cat is eating!
The white dog is *ating!
示例代码:
Buil*er
public interface PersonBuilder {
void buildHead();
v*id buildBody();
void buildFoot()*
Person buildPerson();
}
ConcreteBuilder
public class ManBuilder implements PersonB*ilder {
Person person;
public ManBuilder() {
person = ne* Man();
}
publ*c void build*ody() {
perso*.setBody("建造男人的身体");
}
public void buildFoot() {
person.setFo*t("建造男人的脚");
}
public void buildHead() {
pers*n.setHead("建造*人的头");
}
*ublic Person buildPerson() {
retur* person;
}
}
Dir*ctor
public class PersonDirec*or {
public Person constructPerson(PersonBuilder pb) {
pb.buildHead();
pb.buildBody();
pb.buildFoot();
return pb.buildPerson();
}
}
Product
public class Person {
private String head;
private String body;
private String foot;
public String getH*ad() {
return head;
}
public void setHead(String hea*) {
this.head = head;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.b*dy = body;
}
public String getFoot() {
return foot;
}
public void setFoot(String foot) {
t*is.foot = foot;
}
}
public class Man extends Person {
}
Test
publ*c class Test{
public static void main(String[] ar*s) {
PersonDirector pd = new PersonDirector();
Person person = pd.constructPerson(new ManBuilder());
System*out.println(person.getBody());
System.out.println(person.getFoot());
System.out.println(person.getHead());
}
}
result
建造男人*身体
建造男*的脚
建造男人的头
Singleton
public class Singleton {
private static Singleton sing;
private Singleton() {
}
public st*tic Singleton get*nstance() {
if (sing == null) {
sing = new Singleto*();
}
return sing;
}
}
Test
public class Test {
public static void *ain(*tring[] args) {
Singleton sing = Singleton.getInstance();
Singleton si*g2 = Singleton.getI*stance();
System.out.println(sing);
System.out.pr*ntln(sing2);
}
}
result
singleton.Singleton@1c78e57
singleton.Singleton@1c78e57
原型模式也比较容易理解,也就是在设计时候首先加入肯定能用到的功能的声明,然后对其进行实现,最主要的是可以提供一份copy,也就是通过clone进行实现,这样在原基础上进行改动会比重新建立更为方便。
Prototype
public class Prototype implements Cloneable {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public Object clone(){
try {
return super.clone();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
ConcretePrototype
publ*c class ConcretePrototype extend* Prototype {
public ConcretePrototype(String name) {
setName(name);
}
}
Client
public clas* Test {
public static void main(String[] args) {
Prototype pro = new ConcretePrototy*e("prototype");
Prototype pro2 = (Prototype)pro.clone();
*ystem.out.println(pro.getName()*;
System.out.println(pro2.getName());
}
}
result
prototype
prototype
文章评论(0条评论)
登录后参与讨论