好久没来了。散分,23种设计模式。必须要有代码说明。
每人最多写一种,帮顶也给分。
[解决办法]
看出来了。。。纯散分的~
[解决办法]
呵呵 飘过 接分
[解决办法]
我就来接接分。。。。那东西三二次话就想明白难。。
[解决办法]
工厂模式。路过绝代不经常来了
[解决办法]

foreach(var item in (new List<int>(){ 1 , 2 , 3 }))
{
Console.WriteLine(item);
}
public class Button
{
public event EventHandler Click;
}
public static singlet getinstance{
return instance;}
[解决办法]

public class Singleton{
public static Singleton instance;
private Singleton(){}
public static Singleton getInstance(){
if(instance == null)
instance = new Singleton();
return instance ;
}
}
[解决办法]
看起来还有好多要学~~~
[解决办法]
来一个适配器模式~
/**
* A interface
*/
public interface Shape {
public void Draw();
public void Border();
}
/**
* The Adaptee in this sample
*/
public class Text {
private String content;
public Text() {
}
public void SetContent(String str) {
content = str;
}
public String GetContent() {
return content;
}
}
/**
* The Object Adapter in this sample
*/
public class TextShapeObject implements Shape {
private Text txt;
public TextShapeObject(Text t) {
txt = t;
}
public void Draw() {
System.out.println("Draw a shap ! Impelement Shape interface !");
}
public void Border() {
System.out.println("Set the border of the shap ! Impelement Shape interface !");
}
public void SetContent(String str) {
txt.SetContent(str);
}
public String GetContent() {
return txt.GetContent();;
}
public static void main(String[] args) {
Text myText = new Text();
TextShapeObject myTextShapeObject = new TextShapeObject(myText);
myTextShapeObject.Draw();
myTextShapeObject.Border();
myTextShapeObject.SetContent("A test text !");
System.out.println("The content in Text Shape is :" + myTextShapeObject.GetContent());
}
}
/**
* The Class Adapter in this sample
*/
public class TextShapeClass extends Text implements Shape {
public TextShapeClass() {
}
public void Draw() {
System.out.println("Draw a shap ! Impelement Shape interface !");
}
public void Border() {
System.out.println("Set the border of the shap ! Impelement Shape interface !");
}
public static void main(String[] args) {
TextShapeClass myTextShapeClass = new TextShapeClass();
myTextShapeClass.Draw();
myTextShapeClass.Border();
myTextShapeClass.SetContent("A test text !");
System.out.println("The content in Text Shape is :" + myTextShapeClass.GetContent());
}
}
/**
* Facade pattern
*/
import java.io.*;
class Wall {
public Wall() {
System.out.println("Create a wall !");
}
}
class Door {
public Door() {
System.out.println("Create a door !");
}
}
class FacadeRoom {
public void CreateRoom() {
Wall wall1 = new Wall();
Wall wall2 = new Wall();
Wall wall3 = new Wall();
Wall wall4 = new Wall();
Door door = new Door();
}
}
public class Test {
public static void main(String[] args) {
FacadeRoom room = new FacadeRoom();
room.CreateRoom();
}
}
//Singleton.java
public class Singleton {
private static Singleton instance;
private Singleton() {
}
public static Singleton GetInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
//main.java
public class main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Singleton s1 = Singleton.GetInstance();
Singleton s2 = Singleton.GetInstance();
if (s1 == s2)
System.out.println("两个对象是相同的实例");
}
}
void remove(Component component);
}
class Leaf implements Component {
public void operation() {
System.out.println("an operation");
}
public void add(Component component) {
throw new UnsupportedOperationException();
}
public void remove(Component component) {
throw new UnsupportedOperationException();
}
}
class Composite implements Component {
List components = new ArrayList();
public void operation() {
Component component = null;
Iterator it = components.iterator();
while (it.hasNext()) {
//不知道此component对象是leaf还是composite,
//如果是leaf则直接实现操作,如果是composite则继续递归调用
component = (Component) it.next();
component.operation();
}
}
public void add(Component component) {
components.add(component);
}
public void remove(Component component) {
components.remove(component);
}
}
//Decorator
//对一个类的功能进行扩展时,我可以使用继承,但是不够灵活,所以选用了
//另外的一种形式,引用与继承都可活得对对象的一定的使用能力,而使用引用将更灵活
//我们要保证是对原功能的追加而不是修改,否则只能重写方法,或使用新的方法
//注意concrete的可以直接new出来,
//而decorator的则需要用一个另外的decorator对象才能生成对象
//使用对象封装,和公用接口
//Decorator链上可以有多个元素
interface Componenta {
void operation();
}
class ConcreteComponent implements Componenta {
public void operation() {
System.out.println("do something");
}
}
class Decorator implements Componenta {
private Componenta component;
public Decorator(Componenta component) {
this.component = component;
}
public void operation() {
//do something before
component.operation();
//do something after
}
}
//Facade
//非常实用的一种设计模式,我可以为外部提供感兴趣的接口
class Obj1 {
public void ope1() {}
public void ope2() {}
}
class Obj2 {
public void ope1() {}
public void ope2() {}
}
class Facade {
//我得到了一个简洁清晰的接口
public void fdMethod() {
Obj1 obj1 = new Obj1();
Obj2 obj2 = new Obj2();
obj1.ope1();
obj2.ope2();
}
}
//Flyweight
//空
[解决办法]
帮顶
up
[解决办法]
职责链模式:
//ConcreteHandler1.java
public class ConcreteHandler1 extends Handler {
@Override
public void HandleRequest(int request) {
// TODO Auto-generated method stub
if(request>=0&&request<10)
{
System.out.println("处理请求{1}"+this.getClass().getName()+request);
}
else if(Successor!=null)
{
Successor.HandleRequest(request)
;
}
}
}
//ConcreteHandler2.java
public class ConcreteHandler2 extends Handler{
@Override
public void HandleRequest(int request) {
// TODO Auto-generated method stub
if(request>=10&&request<20){
System.out.println("处理请求"+this.getClass().getName()+request);
}
else if(Successor!=null)
{
Successor.HandleRequest(request);
}
}
}
//ConcreteHandler3.java
public class ConcreteHandler3 extends Handler {
@Override
public void HandleRequest(int request) {
// TODO Auto-generated method stub
if(request>=20&&request<30)
{
System.out.println("处理请求"+this.getClass().getName()+request);
}
else if(Successor!=null){
Successor.HandleRequest(request);
}
}}
/
//Handler.java
public abstract class Handler {
protected Handler Successor;
public void SetSuccessor(Handler successor)
{
this.Successor=successor;
}
public abstract void HandleRequest(int request);
}
//main.java
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Handler h1=new ConcreteHandler1();
Handler h2=new ConcreteHandler2();
Handler h3=new ConcreteHandler3();
h1.SetSuccessor(h2);
h2.SetSuccessor(h3);
int requests[]={2,5,14,22,18,3,27,20};
for(int request:requests)
{
h1.HandleRequest(request);
}
}
}
set { name = value; }
}
}
interface Songliwu{//追求者的行为
void Songhua();
void SongJieZhi();
}
class Zuiqiuzhe:Songliwu//追求者类
{
Ren re;
public Zuiqiuzhe(Ren ren) {
this.re = ren;
}
#region Songliwu 成员
public void Songhua()//追求者的行为
{
Console.WriteLine(re.Name+"送你花");
}
public void SongJieZhi()//追求者的行为
{
Console.WriteLine(re.Name + "送你戒指");
}
#endregion
}
class Daili:Songliwu//代理类
{
Zuiqiuzhe zuiqiu;//追求者
public Daili(Ren ren)
{
zuiqiu = new Zuiqiuzhe(ren);//谁让代理
}
#region Songliwu 成员
public void Songhua()
{
zuiqiu.Songhua();//代理的行为
}
public void SongJieZhi()
{
zuiqiu.SongJieZhi();//代理的行为
}
#endregion
//总结:代理模式 就是本类属于自己的方法,可以间接的让代理帮我们完成,WebService 我们先把WebServices引入项目中,在项目中就会生成一个类,就是代理,他代理了WebServics的所有方法
}
}
[解决办法]
好贴,你懂的。
[解决办法]
有意思
没想到design patterns 还都有中文名的
[解决办法]
这几天也在看,帮UP
[解决办法]
帮顶+节分 嘿嘿
[解决办法]
一直想自己看,还没机会,帮顶了,接点分,呵呵。
[解决办法]
捧场。。。
[解决办法]
ding............
[解决办法]
帮顶,纯接分,
[解决办法]
顶~~~~~~~~~~~~~~~
[解决办法]
就用过工厂,单例。。。
[解决办法]
该回复于2012-08-28 09:17:14被版主删除
[解决办法]
学习了
[解决办法]
没学设计模式呢 不过有次看斗地主棋牌游戏MFC代码 看到 stdafx.h 里 Managers manager; 感觉这就是总管模式
一个manager掌握全部~
[解决办法]
跟着学习
[解决办法]
精华帖,来看看。
[解决办法]
看看的